蓝牙外设连接到iOS';我不在后台工作
我的蓝牙外围设备在使用后台iOS应用程序时遇到问题。我需要应用程序实时响应所有蓝牙通知。我一直在跟踪,我的应用程序有10%的时间是在后台运行的,但其余90%的时间是每10分钟将事件排队并分批发送到我的应用程序 我只需要在蓝牙事件后通过wifi快速启动一个本地子网网络请求,这样在10秒以下iOS应用程序才能在后台工作 至于CoreBluetooth设置,我遵循了这封信的指南,它偶尔在后台工作。但它也会批量发送通知,并每隔十分钟重新启动我的应用程序,这让我觉得它配置正确 我可以确认应用程序没有被杀死。我可以让调试器保持连接,并继续运行。事实上,调试器显示通知排队,应用程序每十分钟进入前台一次。在我使用该应用程序后,它确实可以工作几分钟。但它不可避免地得到了充分的支持 我希望应用程序在收到蓝牙通知后立即被唤醒。检查iOS设置>蓝牙屏幕中的已连接设备列表,会显示已连接的外围设备。很明显,iOS正在捕获通知。但它很少唤醒应用程序来发送通知蓝牙外设连接到iOS';我不在后台工作,ios,objective-c,bluetooth,core-bluetooth,Ios,Objective C,Bluetooth,Core Bluetooth,我的蓝牙外围设备在使用后台iOS应用程序时遇到问题。我需要应用程序实时响应所有蓝牙通知。我一直在跟踪,我的应用程序有10%的时间是在后台运行的,但其余90%的时间是每10分钟将事件排队并分批发送到我的应用程序 我只需要在蓝牙事件后通过wifi快速启动一个本地子网网络请求,这样在10秒以下iOS应用程序才能在后台工作 至于CoreBluetooth设置,我遵循了这封信的指南,它偶尔在后台工作。但它也会批量发送通知,并每隔十分钟重新启动我的应用程序,这让我觉得它配置正确 我可以确认应用程序没有被杀死
2016年8月10日更新: 所以问题不在设备本身。它保持连接,甚至iOS的设置应用程序也显示设备仍然连接。不过我意识到我在
应用程序willresignactive
上运行了beginBackgroundTaskWithExpirationHandler
,这给了我伪后台活动。当我对此发表评论时,应用程序不再在我的设备发出的蓝牙通知上取消挂起
所有蓝牙通知都已排队,并在手动重新启动应用程序时立即触发。当应用程序处于后台时,调试应用程序不会显示任何活动。因此,现在看起来我还没有将应用程序配置为在后台正确使用蓝牙
因此,我的直觉是,在遵循CoreBooth后台文档的过程中出现了一些配置错误的情况。但这里只有几个步骤,我已经实现了每个步骤。我希望launchOptions?[UIApplicationLaunchActionsBluetoothCentralKey]
会被发送到应用程序(\uuUiDidFinishLaunchingWithOptions:)
,但它永远不会被发送。并且永远不会调用centralManager(uux0:willRestoreState:)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]
if centralManagerIdentifiers != nil {
print(" ---> centralManagerIdentifiers: \(centralManagerIdentifiers)")
}
...
}
我已启用“使用蓝牙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)
}
}
}
...
}
ui应用程序aunchoptionsblueouthcentralskey
从未被调用
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]
if centralManagerIdentifiers != nil {
print(" ---> centralManagerIdentifiers: \(centralManagerIdentifiers)")
}
...
}
最后,centralManager(uquo: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()
应该在应用程序中(uu2;:didfishlaunchingwithoptions:)
这就成功了,现在我的蓝牙设备在后台运行。在原始问题的更新中添加了代码示例。嗨,Samuel Clay。我第一次在灯塔上工作。在我的例子中,“didDiscover peripheral”委托方法在应用程序处于后台时不起作用?我还启用了“bluetooth central”,所以您能解释一下我想在5月份做什么吗?如果你不介意的话,你能给我你的电子邮件地址吗?我还是不太明白为什么,但这实际上解决了我在iOS 10上的背景问题!有趣的是,上面的代码允许我的应用程序在iOS 11上的后台工作,而在iOS 10上它立即被挂起