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

Failed to read characteristic #22

Closed
Octavelscx opened this issue Dec 13, 2022 · 9 comments
Closed

Failed to read characteristic #22

Octavelscx opened this issue Dec 13, 2022 · 9 comments
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers

Comments

@Octavelscx
Copy link

Hi there,

I'm pretty new with kotlin and bluetooth and I'm trying your nice library, scan device works perfectly, but when I want to connect to a device and read a characteristic I get a null return and this is what I get in log : (failed to read characteristic)

image

Here is my code :

 * Connect BLE device
 */
@SuppressLint("MissingPermission")
public fun connectBLE(ble:BLE, device: BLEDevice) {
    var name: String = device.name.toString()
    lifecycleScope.launch {
            ble.connect(device)?.let { connection ->
                // For watching bytes
                val value = connection.read("00002A29-0000-1000-8000-00805F9B34FB")
                Log.i("BLEDATA", "$value")

        }
}

I read that for reading 16 bits uuid with bluetooth low energy we have make uuid this form :
0000XXXX-0000-1000-8000–00805f9b34fb
But maybe I'm wrong ?

I got the uuid 0x180A with nrf Connect App and this is marked Read and correspond to the manufacturer brand name string.
I bet I'm doing something wrong but dont know what...

Thanks a lot !

@Octavelscx
Copy link
Author

I think the issue comes from the BluetoothCharacteristic class

The method read uses gatt.readCharacteristic() of android library and this method is asynchronous :

https://developer.android.com/reference/android/bluetooth/BluetoothGatt

I tried with debugging and when I do breakpoints, it works and I get return value of the reading of characteristic. If I just run the app without breakpoint, it returns null value.

So I think it's about a delay of the read() method of the BluetoothCharacteristic.

But I'm too new in kotlin to provide you an correction but I hope someone will see this and help ! :) Thanks

@LeandroSQ LeandroSQ added bug Something isn't working enhancement New feature or request good first issue Good for newcomers labels Dec 14, 2022
@LeandroSQ
Copy link
Owner

Hello there 🙂
First of all, thank you for the interest. Awesome findings, these issues seem to be related to #13.

I will take a look on your suggestions as soon as possible, thanks.

@LeandroSQ
Copy link
Owner

FYI, your initial guess was spot on!
I missed completely the async behavior for read();

Thank you :)

@LeandroSQ
Copy link
Owner

You can check out a fix for it on #23

@LeandroSQ
Copy link
Owner

Please try v1.8.0

@Octavelscx
Copy link
Author

Thanks a lot man,

I'm testing v1.8.0 right now but I get no return after the onCharacteristicRead()

This is how I call it :

image

The execution never go in the if else statement...

With the debugger I can see that it get the characteristic value :
image

But the execution get stuck at this line :

image

As if it never returns to my method and therefore never fits in the if else statement above

Maybe I'm calling it wrong, but I tried the other method you give and I got the same issue
For your information I attach this method to a button with a setOnClickListener like this :

image

Thanks for your help

@LeandroSQ
Copy link
Owner

LeandroSQ commented Dec 23, 2022

Are you using observe too? In non-notifiable characteristics, the library fallback to a legacy observe, which uses read.
Also one limitation as per today, is only one read per characteristic at-a-time.

So when you call read you should wait until it returns to call read for the same characteristic again. Other characteristics will work fine.

Are any of these edge scenarios affecting you?

@LeandroSQ LeandroSQ reopened this Dec 23, 2022
@LeandroSQ
Copy link
Owner

Would you be so kind to provide a minimum reproducible sample project?

@Octavelscx
Copy link
Author

My bad, it's perfectly working when I read notifiable characteristic, but when I want to read a non notifiable characteristic it's not working, don't really know why, but pretty sure it's on my side

Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants