I appreciate that this may have already been answered but I'm unable to find a solution that works for me.
Tl;dr: How do make a function block?
I have the following BLE-related code written in Kotlin for Android API 28.
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
for (gattService: BluetoothGattService in gatt!!.services) {
for (gattChar: BluetoothGattCharacteristic in gattService.characteristics) {
if (gattChar.uuid.toString().contains(ADC_SAMPLESET_0) && !subscribed_0) {
subscribed_0 = true
gatt.setCharacteristicNotification(gattChar, true)
val descriptor = gattChar.getDescriptor(
UUID.fromString(BleNamesResolver.CLIENT_CHARACTERISTIC_CONFIG)
)
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
gatt.writeDescriptor(descriptor)
}
The if-statement above is repeated multiple times to facilitate subscription to multiple BLE characteristics. Unfortunately, the gatt.writeDescriptor()
function runs asynchronously. I need to wait for it to return before calling gatt.writeDescriptor()
for the next characteristic. How do I achieve this?
I've tried using runBlocking
and GlobalScope.launch
in kotlinx.coroutines.experimental.*
but I'm not entirely sure that they're the right thing.
Thanks, Adam