Retrieve connected peripherals #469
-
Hello everybody. I'd like to known if there's a way to grab all connected peripherals, i couldn't find any method or explanation in the docs related to this task. In Google's Bluetooth API an instance of BluetoothManager can call .getAllConnectedDevices(), I'm wondering if there's a method like this in Kable. For sure I could scan again for devices and check the state of each one, but in my case the ESP32, that is my BLE server, is designed to advertise only if there's no device connected (and I guess many other servers are designed to do that was well). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There currently isn't such a feature in Kable. It is generally assumed that the consumer will keep track (references) to peripherals. For example, you might hold a list of peripherals: val peripherals = mutableListOf<Peripheral>() Finding the connected ones could be done with something like: val connectedPeipherals = peripherals.filter { it.state.value == Connected } Alternatively, you could keep a reference to peripherals in a val peripherals = mutableMapOf<String, Peripheral>() Then you could use val addresses = BluetoothManager.getConnectedDevices(..).map { it.address }
val connectedPeripherals = peripherals.filterKeys { it in addresses }.values There may be cleaner and more efficient ways to do this, but those should be good starting points? I'm hesitant to have Kable track peripherals for consumers, as the organizational structure that consumers might want to use is likely to vary a lot. |
Beta Was this translation helpful? Give feedback.
There currently isn't such a feature in Kable. It is generally assumed that the consumer will keep track (references) to peripherals.
For example, you might hold a list of peripherals:
Finding the connected ones could be done with something like:
Alternatively, you could keep a reference to peripherals in a
Map
(with the key being the MAC address):Then you could use
BluetoothManager.getConnectedDevices
to cross reference with your list: