Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peripheral.write() not sending request #794

Closed
chungchungdev opened this issue Nov 12, 2024 · 6 comments
Closed

Peripheral.write() not sending request #794

chungchungdev opened this issue Nov 12, 2024 · 6 comments

Comments

@chungchungdev
Copy link

chungchungdev commented Nov 12, 2024

  • kable version: 0.35.0-rc
  • platform: Android only

I have a device like the one below. The custom characteristic accepts write requests that allows reading, writing, subscribing and unsubscribing different data. The corresponding data is emitted through notification.

class MyDevice(private val peripheral: Peripheral) {

  private val customCharacteristic = characteristicOf("some-uuid1", "some-uuid2")

  private val myData: Flow<ByteArray> = peripheral.observe(customCharacteristic)

  val sensor1Flow = myData.map { /*filter sensor1 data*/ }
  
  val state1Flow = myData.map { /*filter state1 data*/ }

  lateinit var state1: String

  suspend fun subscribeSensor1() =
      peripheral.write(customCharacteristic, byteArrayOf(/*command1*/), WriteType.WithResponse)

  suspend fun readState1(): String {
    val deferred = peripheral.async { state1Flow.first() }
    peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    Timber.d("request sent")
    return deferred.await()
  }

  suspend fun connect() {
    peripheral.connect()
    subscribeSensor1()
    Timber.d("block?")
    state1 = readState1()
    Timber.d("didn't block")
  }
}

However, the await() in readState1() is blocking the coroutine scope. It seems that the request is not sent to the device. So no matching data is emitted from state1Flow and the coroutine scope is stuck forever. But when I start a coroutine and collect the source data flow or its derivative outside (e.g. connect() method or viewmodelScope), the peripheral scope is not blocked and it runs without problem. I discover this by chance but I don't know what makes the difference.

suspend fun connect() {
    myData.launchIn(peripheral) // or sensor1Flow or state1Flow
    peripheral.connect()
    subscribeSensor1()
    state1 = readState1()
  }

I also tried adding that line inside the method, but it is still blocking.

suspend fun readState1(): String {
    myData.launchIn(peripheral) // or sensor1Flow or state1Flow
    val deferred = peripheral.async { state1Flow.first() }
    peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    Timber.d("request sent")
    return deferred.await()
  }
@twyatt
Copy link
Member

twyatt commented Nov 12, 2024

Can you provide logs, please?

@chungchungdev
Copy link
Author

chungchungdev commented Nov 12, 2024

The data related to state1 is B0 70 01 03 81

the blocking version

---------------------------- PROCESS STARTED (23185) for package com.chungchungdev.myproject ----------------------------
2024-11-13 04:49:04.796 23185-23185 BluetoothAdapter        com.chungchungdev.myproject          I  BluetoothAdapter() : com.chungchungdev.myproject
2024-11-13 04:49:04.833 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          I  XX:XX:XX:XX:B1:27 Connecting
2024-11-13 04:49:04.835 23185-23252 BluetoothAdapter        com.chungchungdev.myproject          I  BluetoothAdapter() : com.chungchungdev.myproject
2024-11-13 04:49:04.835 23185-23252 BluetoothAdapter        com.chungchungdev.myproject          D  getBleEnabledArray(): ON
2024-11-13 04:49:04.835 23185-23252 BluetoothGatt           com.chungchungdev.myproject          D  connect() - device: XX:XX:XX:XX:B1:27, auto: false
2024-11-13 04:49:04.839 23185-23252 BluetoothGatt           com.chungchungdev.myproject          D  registerApp()
2024-11-13 04:49:04.840 23185-23252 BluetoothGatt           com.chungchungdev.myproject          D  registerApp() - UUID=d8bbaa77-8a6d-47eb-a4f8-36eae5aaaa6a
2024-11-13 04:49:04.851 23185-23213 BluetoothGatt           com.chungchungdev.myproject          D  onClientRegistered() - status=0 clientIf=12
2024-11-13 04:49:04.860 23185-23213 BluetoothAdapter        com.chungchungdev.myproject          D  getBleEnabledArray(): ON
2024-11-13 04:49:05.384 23185-23213 BluetoothGatt           com.chungchungdev.myproject          D  onClientConnectionState() - status=0 clientIf=12 device=XX:XX:XX:XX:B1:27
2024-11-13 04:49:05.386 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onConnectionStateChange
                                                                                                    status: Success
                                                                                                    newState: Connected
2024-11-13 04:49:05.387 23185-23252 Kable/Connection        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Discovering services
2024-11-13 04:49:05.388 23185-23253 BluetoothGatt           com.chungchungdev.myproject          D  discoverServices() - device: XX:XX:XX:XX:B1:27
2024-11-13 04:49:05.392 23185-23213 BluetoothGatt           com.chungchungdev.myproject          D  onSearchComplete() = Device=XX:XX:XX:XX:B1:27 Status=0
2024-11-13 04:49:05.392 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onServicesDiscovered
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 04:49:05.393 23185-23252 Kable/Connection        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Discovered 5 services
2024-11-13 04:49:05.393 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Configuring characteristic observations
2024-11-13 04:49:05.394 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          I  XX:XX:XX:XX:B1:27 Connected
2024-11-13 04:49:05.395 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 00001800-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a00-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.634 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 00001800-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a00-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.635 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a24-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.694 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a24-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.695 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a25-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.754 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a25-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.755 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a26-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.813 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a26-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.814 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a27-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.872 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a27-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.873 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a28-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.933 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a28-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.934 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a29-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:05.992 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a29-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 04:49:05.994 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    writeType: WithResponse
                                                                                                    data: B0 70 03 01 32 10
2024-11-13 04:49:06.082 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 04:49:06.083 23185-23185 MyDevice                com.chungchungdev.myproject          D  block?
2024-11-13 04:49:06.084 23185-23185 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    writeType: WithResponse
                                                                                                    data: B0 70 01 03 81 20
2024-11-13 04:49:06.084 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 Starting observation
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
2024-11-13 04:49:06.085 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 setCharacteristicNotification
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    value: true
2024-11-13 04:49:06.085 23185-23252 BluetoothGatt           com.chungchungdev.myproject          D  setCharacteristicNotification() - uuid: 1610aa01-0111-0899-2503-732905714219 enable: true
2024-11-13 04:49:06.091 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Writing ENABLE_NOTIFICATION_VALUE to CCCD
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
2024-11-13 04:49:06.091 23185-23252 Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
                                                                                                    data: 01 00
2024-11-13 04:49:06.142 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 04:49:06.143 23185-23185 MyDevice                com.chungchungdev.myproject          D  request sent
2024-11-13 04:49:06.203 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onDescriptorWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 04:49:06.234 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.326 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.449 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.538 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.658 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.748 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.867 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:06.957 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.078 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.166 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.287 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.378 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.501 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.587 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.711 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.796 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:07.921 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.006 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.136 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.218 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.343 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.429 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 04:49:08.550 23185-23253 Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
---------------------------- PROCESS ENDED (23185) for package com.chungchungdev.myproject ----------------------------


non-blocking version

---------------------------- PROCESS STARTED (5608) for package com.chungchungdev.myproject ----------------------------
2024-11-13 05:22:02.596  5608-5608  BluetoothAdapter        com.chungchungdev.myproject          I  BluetoothAdapter() : com.chungchungdev.myproject
2024-11-13 05:22:02.628  5608-5841  Kable/Peripheral        com.chungchungdev.myproject          I  XX:XX:XX:XX:B1:27 Connecting
2024-11-13 05:22:02.630  5608-5841  BluetoothAdapter        com.chungchungdev.myproject          I  BluetoothAdapter() : com.chungchungdev.myproject
2024-11-13 05:22:02.630  5608-5841  BluetoothAdapter        com.chungchungdev.myproject          D  getBleEnabledArray(): ON
2024-11-13 05:22:02.631  5608-5841  BluetoothGatt           com.chungchungdev.myproject          D  connect() - device: XX:XX:XX:XX:B1:27, auto: false
2024-11-13 05:22:02.635  5608-5841  BluetoothGatt           com.chungchungdev.myproject          D  registerApp()
2024-11-13 05:22:02.636  5608-5841  BluetoothGatt           com.chungchungdev.myproject          D  registerApp() - UUID=a819377c-483b-4450-8ada-102872b84da6
2024-11-13 05:22:02.638  5608-5677  BluetoothGatt           com.chungchungdev.myproject          D  onClientRegistered() - status=0 clientIf=12
2024-11-13 05:22:02.644  5608-5677  BluetoothAdapter        com.chungchungdev.myproject          D  getBleEnabledArray(): ON
2024-11-13 05:22:02.811  5608-5677  BluetoothGatt           com.chungchungdev.myproject          D  onClientConnectionState() - status=0 clientIf=12 device=XX:XX:XX:XX:B1:27
2024-11-13 05:22:02.816  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onConnectionStateChange
                                                                                                    status: Success
                                                                                                    newState: Connected
2024-11-13 05:22:02.817  5608-5841  Kable/Connection        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Discovering services
2024-11-13 05:22:02.818  5608-5847  BluetoothGatt           com.chungchungdev.myproject          D  discoverServices() - device: XX:XX:XX:XX:B1:27
2024-11-13 05:22:02.828  5608-5677  BluetoothGatt           com.chungchungdev.myproject          D  onSearchComplete() = Device=XX:XX:XX:XX:B1:27 Status=0
2024-11-13 05:22:02.828  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onServicesDiscovered
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 05:22:02.830  5608-5841  Kable/Connection        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Discovered 5 services
2024-11-13 05:22:02.830  5608-5841  Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Configuring characteristic observations
2024-11-13 05:22:02.830  5608-5841  Kable/Peripheral        com.chungchungdev.myproject          I  XX:XX:XX:XX:B1:27 Connected
2024-11-13 05:22:02.917  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 00001800-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a00-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.101  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 00001800-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a00-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.102  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a24-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.160  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a24-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.162  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a25-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.221  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a25-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.222  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a26-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.281  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a26-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.282  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a27-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.344  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a27-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.345  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a28-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.406  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a28-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.408  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a29-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.464  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180a-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a29-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 
2024-11-13 05:22:03.467  5608-5608  MyDevice                com.chungchungdev.myproject          D  collect data flow
2024-11-13 05:22:03.468  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    writeType: WithResponse
                                                                                                    data: 
2024-11-13 05:22:03.468  5608-5843  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 Starting observation
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
2024-11-13 05:22:03.468  5608-5843  Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 setCharacteristicNotification
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    value: true
2024-11-13 05:22:03.468  5608-5843  BluetoothGatt           com.chungchungdev.myproject          D  setCharacteristicNotification() - uuid: 1610aa01-0111-0899-2503-732905714219 enable: true
2024-11-13 05:22:03.472  5608-5843  Kable/Peripheral        com.chungchungdev.myproject          V  XX:XX:XX:XX:B1:27 Writing ENABLE_NOTIFICATION_VALUE to CCCD
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.472  5608-5843  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
                                                                                                    data: 01 00
2024-11-13 05:22:03.524  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 05:22:03.527  5608-5608  MyDevice                com.chungchungdev.myproject          D  block?
2024-11-13 05:22:03.529  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    writeType: WithResponse
                                                                                                    data: B0 70 01 03 81 20
2024-11-13 05:22:03.614  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onDescriptorWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    descriptor: 00002902-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 05:22:03.673  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 05:22:03.675  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:03.676  5608-5608  MyDevice                com.chungchungdev.myproject          D  request sent
2024-11-13 05:22:03.677  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 01 03 81 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:03.689  5608-5608  MyDevice                com.chungchungdev.myproject          D  didn't block
2024-11-13 05:22:03.690  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 write
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    writeType: WithResponse
                                                                                                    data: B1 10 7D 13 22 00 64 00
2024-11-13 05:22:03.734  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicWrite
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    status: GATT_SUCCESS(0)
2024-11-13 05:22:03.736  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B1 10 7D 13 22 00 64 00 00 00 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:03.740  5608-5608  Kable/Peripheral        com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 read
                                                                                                    service: 0000180f-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a19-0000-1000-8000-00805f9b34fb
2024-11-13 05:22:03.765  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:03.793  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicRead
                                                                                                    service: 0000180f-0000-1000-8000-00805f9b34fb
                                                                                                    characteristic: 00002a19-0000-1000-8000-00805f9b34fb
                                                                                                    status: GATT_SUCCESS(0)
                                                                                                    data: 39
2024-11-13 05:22:03.883  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:03.972  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.094  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.184  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.338  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.397  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.513  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.604  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
2024-11-13 05:22:04.725  5608-5847  Kable/Callback          com.chungchungdev.myproject          D  XX:XX:XX:XX:B1:27 onCharacteristicChanged
                                                                                                    service: 1610aa00-0111-0899-2503-732905714219
                                                                                                    characteristic: 1610aa01-0111-0899-2503-732905714219
                                                                                                    data: B0 70 03 01 32 10 FF FF FF FF 00 00 00 00 00 00 00 00 00 00
---------------------------- PROCESS ENDED (5608) for package com.chungchungdev.myproject ----------------------------


@chungchungdev
Copy link
Author

I try to add a delay, it works. But how much time should be delayed depends on devices. My old device need more delay than my new device to work properly.

suspend fun readState1(): String {
    val deferred = peripheral.async { state1Flow.first() }
    delay(1000)
    peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    Timber.d("request sent")
    return deferred.await()
  }

@twyatt
Copy link
Member

twyatt commented Nov 13, 2024

Oh, if a delay is fixing it, then I suspect your async is not spinning up quickly enough and the response is being missed.

You should use onSubscription instead of async for this, something similar to:

suspend fun readState1(): String =
    state1Flow.onSubscription {
        peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    }.first()

More details can be found here.

@chungchungdev
Copy link
Author

I tried this but it didn't work.

suspend fun readState1(): String =
    state1Flow
        .shareIn(peripheral, SharingStarted./*tried all of them*/)
        .onSubscription {
            peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    }.first()

I use async(start = CoroutineStart.UNDISPATCHED) as mentioned in your given link and it works! Thanks for the help..

@twyatt
Copy link
Member

twyatt commented Nov 13, 2024

For the shareIn, you'll want to start it prior to function invocation (so that it has ample time to spin up), for example:

private val incoming = state1Flow
    .shareIn(peripheral, SharingStarted.Eagerly)

suspend fun readState1(): String =
    incoming.onSubscription {
        peripheral.write(customCharacteristic, byteArrayOf(/*command2*/), WriteType.WithResponse)
    }.first()

Using async(start = CoroutineStart.UNDISPATCHED) should also work just fine. 👍

@twyatt twyatt closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants