LeandroSQ / android-ble-made-easy

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

Read always return null while observe works as intended #13

Closed JouleGit closed 1 year ago

JouleGit commented 2 years ago

Hi, I'm trying to use this to make a simple app to get heartbeat and step values from a mi band 6,

However when i use ble.scanforasync to find and connect to the device, i find that i could only get data from notifiable characteristics like heart rate whenever they changed value , but when i try to use connection.read() on characteristics that is readable like device name , i always gets a null, is there something i missed or something else i should do?

LeandroSQ commented 2 years ago

Hello, hope this message finds you well.

Could you provide more context on this? And if possible, code as well.

JouleGit commented 2 years ago

Hi , sorry for missing the comment. I am new to android developement and found BLE a bit confusing when following google's guide, and I decided to find an online repo that does the work easier.

Background: I am hoping to make an app to 1. search for nearby BLE devices and allows for user to select which to connect, and 2. connect to the device and read the heartrate and other stuff.

However for the 2nd part I cant seem to find any way to just read a characteristics , but I can use notify just fine. For this part the code is something like this. As I implement this as a 2nd activity, I carried the macaddress of the device chosen in the first part using intent and into this new activity. The observe function on the FootstepUUID and HeartbeatUUID works as intended, but as I notice the FootstepUUID reads the cumlative steps, I want to read the original 1st reading of footstep data so i can calculate the difference in subsequent updates and display the steps taken for the current session.Thats when I find i can only read null.

// This is adopted from your example code in README.md
ble.scanForAsync(
    macAddress = mydevicemac,

    onFinish = { connection ->
        if (connection != null) {
          val temp =  connection.read(FootstepUUID) // this part returns null
         Log.i(TAG,temp.toString())// this part returns null

           connection.observe(characteristic = FootstepUUID) { value: ByteArray ->
        // reads the byte array and display on UI
                 }
           connection.observe(characteristic = HeartbeatUUID) { value: ByteArray ->
        // reads the byte array and display on UI
                 }
        } else {
            // Show an Alert or UI with your preferred error message about the device not being available
        }
    },
       onError = { errorCode ->
         // Show an Alert or UI with your preferred error message about the error
    }
)

To make sure , I used connection.readableCharacteristics to see if the UUID is correct and in the list, I used nRF connect to check if the characteristic is correct, and if it is readable. Both are correct and I can even read form the nRF connect app. Now I am stuck at here, knowing that UUID is readable , and I can get data when it update, but I can't manually read the data as I could in the nRF app.

Octavelscx commented 1 year ago

Same issue here : see my comment https://github.com/LeandroSQ/android-ble-made-easy/issues/22#issuecomment-1351009891

LeandroSQ commented 1 year ago

Fixed on v1.8.0