gpt4 book ai didi

ios - 连接到 iOS 的蓝牙外设在后台不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:31 26 4
gpt4 key购买 nike

我在使用后台 iOS 应用程序的蓝牙外围设备时遇到问题。我需要应用程序实时响应所有蓝牙通知。我关注了Apple's CoreBluetooth background process guide大约 10% 的时间我的应用程序在后台工作,但其他 90% 的时间事件排队并每 10 分钟分批发送到我的应用程序。

我只需要在蓝牙事件后通过 wifi 快速触发本地子网网络请求,因此 iOS 可以在 10 秒内让应用程序在后台运行。

至于 CoreBluetooth 设置,我严格按照指南进行操作,它偶尔会在后台运行。但它还会对通知进行批处理并每十分钟重新启动我的应用程序,这让我认为它配置正确。

我可以确认该应用没有被终止。我可以保持调试器连接并且它继续运行。事实上,调试器显示通知排队并且应用程序每十分钟进入一次前台。在我使用该应用程序后,它确实可以工作几分钟。但它不可避免地会完全背景化。

我希望应用程序在收到蓝牙通知后立即被唤醒。在 iOS 设置 > 蓝牙屏幕中检查已连接的设备列表会显示已连接的外围设备。很明显 iOS 正在捕获通知。但它很少唤醒应用程序发送通知。


2016 年 8 月 10 日更新:

所以问题不在设备本身。它保持连接状态,甚至 iOS 的“设置”应用程序也显示设备仍处于连接状态。我意识到我在 applicationWillResignActive 上运行了 beginBackgroundTaskWithExpirationHandler,这给了我伪后台事件。当我将其注释掉时,该应用程序不再根据我设备的蓝牙通知取消挂起。

所有蓝牙通知都已排队,并且在手动重新启动应用程序时会立即触发。当应用程序处于后台时,调试应用程序不会显示任何事件。所以现在看起来我还没有配置应用程序以在后台正确使用蓝牙。

所以我的直觉是在遵循 CoreBluetooth 后台文档时配置错误。但是只有几个步骤,我已经实现了每个步骤。我希望 launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey] 被发送到 application(_:didFinishLaunchingWithOptions:),但它从未发送过。并且永远不会调用 centralManager(_:willRestoreState:)

我启用了“使用蓝牙 LE 配件”。

Uses Bluetooth LE accessories

我正在使用 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/

26 4 0
文章推荐: ios - 在 iOS WKWebView 中单击带有 URL 的 Whatsapp 方案时出现不支持的 URL 错误
文章推荐: Android - 使用私钥编码和解码 RSA?
文章推荐: javascript - 将 jQuery 数据表 aLengthMenu 显示为 链接而不是