我在使用后台 iOS 应用程序的蓝牙外围设备时遇到问题。我需要应用程序实时响应所有蓝牙通知。我关注了Apple's CoreBluetooth background process guide大约 10% 的时间我的应用程序在后台工作,但其他 90% 的时间事件排队并每 10 分钟分批发送到我的应用程序。
我只需要在蓝牙事件后通过 wifi 快速触发本地子网网络请求,因此 iOS 可以在 10 秒内让应用程序在后台运行。
至于 CoreBluetooth 设置,我严格按照指南进行操作,它偶尔会在后台运行。但它还会对通知进行批处理并每十分钟重新启动我的应用程序,这让我认为它配置正确。
我可以确认该应用没有被终止。我可以保持调试器连接并且它继续运行。事实上,调试器显示通知排队并且应用程序每十分钟进入一次前台。在我使用该应用程序后,它确实可以工作几分钟。但它不可避免地会完全背景化。
我希望应用程序在收到蓝牙通知后立即被唤醒。在 iOS 设置 > 蓝牙屏幕中检查已连接的设备列表会显示已连接的外围设备。很明显 iOS 正在捕获通知。但它很少唤醒应用程序发送通知。
所以问题不在设备本身。它保持连接状态,甚至 iOS 的“设置”应用程序也显示设备仍处于连接状态。我意识到我在 applicationWillResignActive 上运行了 beginBackgroundTaskWithExpirationHandler,这给了我伪后台事件。当我将其注释掉时,该应用程序不再根据我设备的蓝牙通知取消挂起。
所有蓝牙通知都已排队,并且在手动重新启动应用程序时会立即触发。当应用程序处于后台时,调试应用程序不会显示任何事件。所以现在看起来我还没有配置应用程序以在后台正确使用蓝牙。
所以我的直觉是在遵循 CoreBluetooth 后台文档时配置错误。但是只有几个步骤,我已经实现了每个步骤。我希望 launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey] 被发送到 application(_:didFinishLaunchingWithOptions:),但它从未发送过。并且永远不会调用 centralManager(_:willRestoreState:)。
我启用了“使用蓝牙 LE 配件”。
我正在使用 CBCentralManagerOptionRestoreIdentifierKey:
manager = CBCentralManager(delegate: self, queue: nil, options:
[CBCentralManagerOptionRestoreIdentifierKey: "TTcentralManageRestoreIdentifier",
CBConnectPeripheralOptionNotifyOnDisconnectionKey: NSNumber(bool: true),
CBConnectPeripheralOptionNotifyOnConnectionKey: NSNumber(bool: true),
CBConnectPeripheralOptionNotifyOnNotificationKey: NSNumber(bool: true)])
我正在订阅特征通知:
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
if service.UUID.isEqual(CBUUID(string: DEVICE_V2_SERVICE_BUTTON_UUID)) {
for characteristic: CBCharacteristic in service.characteristics! {
if characteristic.UUID.isEqual(CBUUID(string: DEVICE_V2_CHARACTERISTIC_BUTTON_STATUS_UUID)) {
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
let device = foundDevices.deviceForPeripheral(peripheral)
device?.buttonStatusChar = characteristic
} else if characteristic.UUID.isEqual(CBUUID(string: DEVICE_V2_CHARACTERISTIC_NICKNAME_UUID)) {
peripheral.readValueForCharacteristic(characteristic)
}
}
}
...
}
UIApplicationLaunchOptionsBluetoothCentralsKey 从未被调用。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]
if centralManagerIdentifiers != nil {
print(" ---> centralManagerIdentifiers: \(centralManagerIdentifiers)")
}
...
}
最后,永远不会调用 centralManager(_:willRestoreState:):
func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {
manager = central
let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as! [CBPeripheral]
print(" ---> Restoring state: \(peripherals)")
...
}
最佳答案
想通了。让我们来玩找漏洞...
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var bluetoothManager = TTBluetoothManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
return true
}
...
}
看到 var bluetoothManager = TTBluetoothManager() 行了吗?它应该是 var bluetoothManager: TTBluetoothManager! 并且 bluetoothManager = TTBluetoothManager() 应该在 application(_:didFinishLaunchingWithOptions:)
成功了,现在我的蓝牙设备在后台运行。
关于ios - 连接到 iOS 的蓝牙外设在后台不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38798961/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
使用Ruby1.9.2运行IDE提示说需要gemruby-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall