Reconnect a CBPeripheral faster

后端 未结 2 690
青春惊慌失措
青春惊慌失措 2021-02-06 17:11

I\'m developing an app which connects and disconnects several times to different bluetooth low energy peripherals. After a lot of research on the internet, I didn\'t have found

相关标签:
2条回答
  • 2021-02-06 17:40

    I've tried caching peripheral, service and characteristic objects in memory and manual caching does not work. Once you have disconnected from the peripheral, the service and characteristic objects are no longer valid for use. In fact, even the CBPeripheral can change out from under you – CoreBluetooth has an internal behavior where the device UDID will change every few minutes (if the CBPeripheral is another iOS device).

    However, if you are running developing for iOS 6, there is a way to speed things up. If you watch the Advanced Bluetooth talk at WWDC 2012, you'll see a slide towards the end about caching services and characteristics. Essentially, the OS can cache them all for you but only for paired devices. To pair, you need to respond to a write request with an insufficient authentication error. For example, for an iOS peripheral you would write something like:

    - (void)peripheralManager:(CBPeripheralManager *)peripheralManager didReceiveWriteRequests:(NSArray *)requests {
          ...
    
          [peripheralManager respondToRequest:request withResult:CBATTErrorInsufficientAuthentication];
    
          ...
    }
    

    That will pop up a pairing dialog on the iOS peripheral, after which point you will be paired. Other than that you don't have to change your code – just call discoverServices etc as normal and they will respond more quickly (ie instantly).

    I have also tested this behavior on 10.8.3 and it does not appear to work. So, I don't know of a way to speed things up on OS X (other than staying connected to the peripheral).

    0 讨论(0)
  • 2021-02-06 17:45

    iOS 7 adds additional caching (iOS 7: What's New in Bluetooth LE). I'm seeing times less than 100ms (~80ms average) total to reconnect to a peripheral, discover services, discover characteristics, and read the value of a single characteristic.

    My test configuration was an iPad Air connecting to an iPad 3.

    0 讨论(0)
提交回复
热议问题