LeandroSQ / android-ble-made-easy

An Android Library for handling Bluetooth Low Energy on Android Easy
MIT License
88 stars 29 forks source link

How do I read multiple characteristics ? #49

Open black opened 4 days ago

black commented 4 days ago

I am using the below code to read the multiple characteristics but only the first characteristics gets observed . When I remove the first observer then I started receiving from the next and so on. Basically it is only reading one characteristics at a time.

ble.connect(device)?.let { connection ->
                connection.requestMTU(bytes = 64)
                binding.connectionStatus.text = if(connection.isActive) "Connected" else "not Connected"
                connection.apply {
                    observeString(characteristic = CHARACTERISTIC_UUID_ECG, charset = Charsets.UTF_8) { value: String ->
                        Log.d(TAG, "ECG::: $value")
                        binding.dataEcg.text = "ECG::: $value"
                    }
                    observeString(characteristic = CHARACTERISTIC_UUID_IMU, charset = Charsets.UTF_8) { value: String ->
                        Log.d(TAG, "IMU::: $value")
                        binding.dataImu.text = "IMU::: $value"
                    }
                    observeString(characteristic = CHARACTERISTIC_UUID_BATTERY, charset = Charsets.UTF_8) { value: String ->
                        Log.d(TAG, "BAT::: $value")
                        binding.dataBattery.text = "BAT::: $value"
                    }
                }

            }
soheilkooklan commented 2 days ago

Hi There! The issue is that only one characteristic is being observed at a time because multiple observers are set up for conflicting traits. To fix this, try setting up each observer sequentially with a slight delay between each one to give them time to establish properly. Here's a suggestion to modify your code using coroutines and launch to handle multiple observers without interference:

Give this a try, and it should help you read from multiple characteristics at the same time!

ble.connect(device)?.let { connection ->
    connection.requestMTU(bytes = 64)
    binding.connectionStatus.text = if (connection.isActive) "Connected" else "Not Connected"

    connection.apply {
        // Launch a coroutine to observe each characteristic sequentially
        GlobalScope.launch {
            observeString(characteristic = CHARACTERISTIC_UUID_ECG, charset = Charsets.UTF_8) { value: String ->
                Log.d(TAG, "ECG::: $value")
                binding.dataEcg.text = "ECG::: $value"
            }
        }

        GlobalScope.launch {
            observeString(characteristic = CHARACTERISTIC_UUID_IMU, charset = Charsets.UTF_8) { value: String ->
                Log.d(TAG, "IMU::: $value")
                binding.dataImu.text = "IMU::: $value"
            }
        }

        GlobalScope.launch {
            observeString(characteristic = CHARACTERISTIC_UUID_BATTERY, charset = Charsets.UTF_8) { value: String ->
                Log.d(TAG, "BAT::: $value")
                binding.dataBattery.text = "BAT::: $value"
            }
        }
    }
}
black commented 16 hours ago

@soheilkooklan I tried this already. This was the first response from claudeAi and ChatGPT both. It didn't work.

LeandroSQ commented 15 hours ago

Hello @black thanks for the interest in the library :)

black commented 15 hours ago

@LeandroSQ Hey, Can you help me with this one please?

LeandroSQ commented 15 hours ago

Quick question, do these characteristics have notify enabled? you can check this using connection. notifiableCharacteristics

black commented 15 hours ago

Yes, they do. Basically, I’ve written a small program in Arduino for the Seeed Xiao NRF54320 to transmit fake data like ECG, IMU, and battery status. The issue is that when I comment out the ECG observer, the IMU observer receives data and when both the ECG and IMU observers are commented out, I receive the battery status data. I can only receive data from one observer at a time.

LeandroSQ commented 15 hours ago

And all of these read correctly if done one-by-one?

LeandroSQ commented 15 hours ago

Another thing, are you able to put a breakpoint here and check if this gets called?

black commented 15 hours ago

And all of these read correctly if done one-by-one?

Yes, that correct.

black commented 15 hours ago

Another thing, are you able to put a breakpoint here and check if this gets called?

Hey This isn't getting called.

soheilkooklan commented 11 hours ago

@soheilkooklan I tried this already. This was the first response from claudeAi and ChatGPT both. It didn't work.

That code was rewritten with ChatGPT. Another way is to check these options:

black commented 2 hours ago

@soheilkooklan Pleases stop posting ChatGPT response. If I want, I could take help from ChatGPT myself. Stop spamming.

soheilkooklan commented 1 hour ago

@soheilkooklan Pleases stop posting ChatGPT response. If I want, I could take help from ChatGPT myself. Stop spamming.

Your device may not be working with the features you want. I rewrote the code with ChatGPT just to save time and help you. There's no need to be rude—everyone here has problems they're trying to solve, just like you. This isn’t spam; it’s an attempt to offer assistance.