0

My mobile phone app is supposed to be the central device and the BLE kit is supposed to be the peripheral device. I have managed to write to Kit, as in send data. The kit itself according to it documentation has this: enter image description here

According to the documentation: The Rx (receive) and Tx (transmit) Characteristics are named from the point of view of the Peripheral BGX device.

To send a string to be received by the Peripheral serial interface, write to the Rx Characteristic.

To read a string transmitted from the Peripheral serial interface, enable Notifications or Indications from the Tx Characteristic then wait for subsequent Notification or Indication Events to occur.

So how exactly am I supposed to Read data from the device mentioned if the characteristic for receiving data doesn't have READ as one of its properties?

When I sent data as in writing data to the device I used the writeCharacteristic function.

  fun write(message:String){
    val bytes = BigInteger(message.replace("\\s".toRegex(), ""), 16).toByteArray()
    Timber.i("Bytes value ---> ${bytes.toHexString()}")
    val device = getBleDevice()
    val characteristicRX = getBleCharacteristic()
    writeCharacteristic(device, characteristicRX, bytes)
}

Then I would call this function to send:

fun sendMessage(message:String){
    Timber.i("Check if isConnected = true --> ${isConnected.value}")
    if(isConnected.value == true){
        write(message)
    }else{
       Timber.e("Make sure that you connected and paired with the desired device.")
    }
}

So how do I go on about receiving data instead from the BLE device? Wouldn't the readCharacteristic function come into play here? I ask this because the code I am working on was original designed to exchange data using classical Bluetooth and I was tasked with converting it into BLE instead. But when I used a serial monitor to see the bytes being sent I found that the buttons that are supposed to trigger receiving data are instead sending it to the Kit. This caught my attention as I haven't started working on the reading mechanism and the screenshot I posted here also has me puzzled, as I have thought that the TX characteristic would be a readable one not writeable.

In my app each parameter has a code. If it's a write command it looks like this:

  enum class WriteCommandCodes(val value: String) {
    TOOL_ADDRESS("08 00 00 00 20 30 04 27"),
    RPM_THRESHOLD("08 00 00 00 20 30 04 13"),
    BACKLASH_1("08 00 00 00 20 30 04 22"),
    BACKLASH_2("08 00 00 00 20 30 04 23"),
    DELAY("08 00 00 00 20 30 04 20"),

    BATTERY1_CAPACITY("08 00 00 00 20 30 0F"),
    BATTERY2_CAPACITY("08 00 00 00 20 30 10")}

the payload that is created later on has both the parameter's code and the data being sent.

for the Read parameters I have this:

 enum class ReadRequestCodes(val value: String) {
    KEY_ADDRESS("08 00 00 00 20 30 05 11 00 00 00 00 00"),
    TOOL_ADDRESS("08 00 00 00 20 30 05 27 00 00 00 00 00"),
    RPM_THRESHOLD("08 00 00 00 20 30 05 13 00 00 00 00 00"),
    BACKLASH("08 00 00 00 20 30 05 22 00 00 00 00 00"),

    POWER_SRC_TYPE("08 00 00 00 20 30 05 26 00 00 00 00 00"),
    BATTERY1_PERCENTAGE("08 00 00 00 20 30 11 00 00 00 00 00 00"),
    BATTERY2_PERCENTAGE("08 00 00 00 20 30 12 00 00 00 00 00 00")}

The same is supposed to happen only difference is I'll be receiving this time around.

1 Answers1

0

Yes you override the default onCharacteristicChanged method and implement what you want to do with the new data. You can see someone doing something similar here

BLE receiving GATT notifications from a characteristic

lewisleman
  • 56
  • 5
  • yes but how does it work in my code? What I mean is after enabling notifications from a particular characteristic where do I receive the payload from the BLE device? – Mohamed El Kayal Jan 08 '22 at 14:26