How to List Discoverable Bluetooth Devices and Already Paired Devices in iOS, Which Method to Use in Swift

How to List discoverable Bluetooth devices and already paired devices in iOS, which method to use in swift?

CoreBluetooth only allows you to access Bluetooth Low Energy devices. You can pair these devices if you need encryption but typically you don't.

However, there is no API to list the paired devices - you need to discover the device(s) that are offering the service you are interested in and present a list, if necessary using your own UI in your app. Once you have identified the target device you can initiate a connection.

Core Bluetooth: Find out if device is available for reconnection

The "Device available?" box is referring to centralManager(_:didConnect:) having been called. If you look at the box above, it's "Try to connect." If it's successful, then the device is available.

This is almost always the correct approach. Don't ask "is this likely to succeed?" Just try. If it works, it works.

If you really want to know nearby but unconnected devices, then you can scan for them if they're advertising using scanForPeripherals(withServices:options:). You can then check if any discovered peripherals are the one you're looking for.

Of course a device might be nearby and not advertising. It might be connected to another device, in which case you can't see it. It might be connected to your device, in which case you'll need retrieveConnectedPeripherals to see it. It might just be not advertising (many devices stop advertising after a period of time). In that case you can't see it without trying to connect to it.

But generally the right answer is to just try to connect if you know the device already. Set a timeout, and if it times out, it's probably not nearby. Or don't set a timeout, and just let it try to connect until it works.



Related Topics



Leave a reply



Submit