In Core Bluetooth, after connecting to a device, I turn off the device and the device is disconnected. But when I turn on the device again, there is no didDiscoverPeripheral
called again. How can I reconnect to the device again?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
When you disconnect a device with
cancelPeripheralConnection
thedidDisconnectPeripheral
delegate method will be invoked. However from iOS 6.0 the device remains connected for about 40-50 seconds (or more), so nodidDiscoverPeripheral
will be invoked in that timeframe. If you want to "discover" it again just call theretrieveConnectedPeripherals
method and you will get the reference indidRetrieveConnectedPeripherals
.However, the best solution is to save the device's UUID and use that to reconnect with the
retrievePeripherals
method. This will invokedidRetrievePeripherals
and you can reconnect withconnectPeripheral
. This is the fastest way to reconnect to a device, no scanning is required in this case.In CoreBluetooth all management is done by application layer. In your case, what I would do it is to listen for disconnect event than in same event, reconnect the peripheral. The connection method is an inexpensive one and assure you to reconnect to your device when it is back in range.
Note that if you explicitly disconnect the device, you received the same disconnect event, but you haven't to call the reconnect method.
When you do a scan with
scanForPeripheralsWithServices
, it will normally only notify you once for a particular device address. You can change this to report duplicates by specifying the optionCBCentralManagerScanOptionAllowDuplicatesKey
. Or you can have your app detect that the other device disconnected using a timeout, and restart your scan.