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
Swiftui on MACos - Handle Single-Click and Double-Click at the Same Time
Swift: Search Bar Created at Auto Focus
How to Pass Binding to Child View in the New Navigationstack.Navigationdestination
Integrating Congnito User Pools with Amazon Cognito Identity
Differences Generic Protocol Type Parameter VS Direct Protocol Type
Swift Generics Error: Cannot Convert Value of Type 'Type<T>' to Expected Argument Type 'Type<_>'
Stack Overflow When Defining Subscript on Ckrecord in Swift
Charts Not Plotting in Tableviewcell
Swift: Uploading Image to Firebase Cloud Storage When User Closes App
Swiftui Behavior of .Frame(Height: Nil)
Implementing a Hash Combiner in Swift
Why Does My @Lazy Property Crash, But If I Make It Non Lazy It Works
Swift 5, How to Execute Code After Fetching All Childadded from Firebase
Firebase Access Keys in Queryorderby
How Have Multiple Init() with Swift
How to Draw Dashed Line in Arkit (Scenekit) Like in the Measure App