Esp32-snippets: WriteCharacteristics doesn't work through ESP but is working through noble.

Created on 8 Feb 2020  路  2Comments  路  Source: nkolban/esp32-snippets

Hi All,
I am trying to work on a BLE lock which is currently being operated through a mobile app using the react-native ble plx.
I was trying to make it work through the ESP32 so that it can be controlled remotely. I have prepared the data and is being sent to esp32 through aws iot. data is received but when trying to write to the lock it doesn't respond.
In order to see whether it is something wrong with command or not I installed the noble in my mac and passed the same command through the noble write method and it works and lock responds.

Command is created from a buffer in javascript for noble and this makes lock work

let buffer = encData.slice(0, 20);
      console.log(i + ": " + buffer.toString("hex"));
      //The buffer value in hex is aa5510f0988fbef125a97eee5e7ae2beeaff79
      foundCharacteristic.write(buffer, true, function(error) {
        console.log("set characteristic value ");
      });

I checked a BLE server using the BLE Scanner app in ios and cloned the same characteristics of the lock and could see the value 0xaa5510f0988fbef125a97eee5e7ae2beeaff79 in the app.

When doing for the ESP, I pass this hex value aa5510f0988fbef125a97eee5e7ae2beeaff79 to aws iot and is received in the ESP32 and then the ble scanning is done and the required service and characteristics are found and are written through the following method using the Ble_Client example.

String input = "aa5510f0988fbef125a97eee5e7ae2beeaff79"
if (connected) {
    Serial.println("Setting new characteristic value to \"" + input + "\"");

    // Set the characteristic's value to be the array of bytes that is actually a string.
    Serial.println(input.c_str());
    pRemoteCharacteristic->writeValue((uint8_t*)input.c_str(), input.length());
    connected = false;

  } 

This never works for me .Am I passing the correct way the value or something missing? I am able to scan and get the services and characteristcs but the write is a failure. Not sure whether some conversion issues or what is going wrong. Please do help me if there is something to be done. I have been trying for so long.
Thanks in advance

Most helpful comment

Try with uint8_t input[] = {...};, unless you need to get length from String. But for testing purpose you can test it that way.
Maybe you write to wrong characteristic?

All 2 comments

Try with uint8_t input[] = {...};, unless you need to get length from String. But for testing purpose you can test it that way.
Maybe you write to wrong characteristic?

@chegewara Thanks for the reply. I debugged and tested again with the BLE connect to check what comes from the ESP32 and from the noble.

In noble project

let buffer = encryptedData();// -> which returns a buffer
console.log(i + ": " + buffer.toString("hex")); //aa5510f0988fbef125a97eee5e7ae2beeaff79

The above aa5510f0988fbef125a97eee5e7ae2beeaff79 string value I pass and send it to aws iot to receive in the ESP and this is what is written to the ble device.

In the ble scanner app I could see the

  • HexValue-"6161353531306630393838666265663132356139376565653565376165326265656166663739"
  • String value - "aa5510f0988fbef125a97eee5e7ae2beeaff79"

But noble takes the write() a buffer as input so characteristics.write(buffer) is received as the following in the ble scanner

  • Hex Value- "aa5510f0988fbef125a97eee5e7ae2beeaff79".
  • String Value- "No Value".

I find the aa5510f0988fbef125a97eee5e7ae2beeaff79 has to reach as hex in the ble device when the esp32 characteristics.write(input) is called .The input is also string "aa5510f0988fbef125a97eee5e7ae2beeaff79".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tavo7995 picture Tavo7995  路  7Comments

vishnunaik picture vishnunaik  路  6Comments

minwinmin picture minwinmin  路  9Comments

JasXSL picture JasXSL  路  3Comments

wegunterjr picture wegunterjr  路  7Comments