cordova-plugin-ble-central icon indicating copy to clipboard operation
cordova-plugin-ble-central copied to clipboard

How to read Serial port data of Arduino Bluno?

Open stoneWeb opened this issue 3 years ago • 9 comments

I can successfully write data and read it from the Arduino. But this plug-in cannot read serial port data written from the Arduino. But I use LightBlue to get data both ways.

ble.startNotification(deviceId, "DFB0", "DFB1", reciveData, err => {
      alert(JSON.stringify(err, null, 4));
    });
ble.writeWithoutResponse(deviceId, "DFB0", "DFB1", buffer, function () {
        }, function (err) {
        });

stoneWeb avatar Apr 15 '22 13:04 stoneWeb

Hi @stoneWeb

On the surface, what you're doing looks ok there. Only thing is are you waiting for the notifications to finish subscribing before attempting to read the serial port data? What happens if you put a 1s delay there between startNotification and the writeWithoutReponse call?

peitschie avatar Apr 19 '22 00:04 peitschie

Adding a delay of 1s is the same, startNotification will not trigger reciveData. if I call this:

ble.read(deviceId, "180A", "2A28", reciveData, err => {
      alert(JSON.stringify(err, null, 4));
    });

it always get {}.

stoneWeb avatar Apr 20 '22 04:04 stoneWeb

What does your receiveData implementation look like? Is that an empty object there, or is it perhaps simply a non-printable type (e.g., many browsers show {} for an ArrayBuffer).

E.g., image

peitschie avatar Apr 20 '22 05:04 peitschie

Thank you so much! @peitschie Show {} is indeed the problem of this buffer, and I have confirmed that the data has been received.

But there are still two problems:

  1. I don't know why ble.startNotification doesn't trigger reciveData, only I can read data by calling ble.read actively
  2. Why can only read one byte of data per read?

This is arduino code, so I can only read "h".

if(Serial.available()) {
     Serial.write("hello world!");
     Serial.println();
   }

ble.startNotification is in onConnect function.

stoneWeb avatar Apr 20 '22 07:04 stoneWeb

Do you have any documentation regarding how the Arduino serial interface should be used via Bluetooth? I don't have a board locally, so can't really play myself to figure this out.

To double-check, what are you doing to decode the data from the notification and confirm it's only a single byte? Also, is this iOS or Android?

peitschie avatar Apr 20 '22 11:04 peitschie

Oh, I think this comment in a recently identified quirk probably hints why calling ble.read on iOS causes the notification to trigger.

peitschie avatar Apr 20 '22 11:04 peitschie

I tested my code and the notification was never triggered. if I run:

onConnect() {
    ble.startNotification(deviceId, "DFB0", "DFB1", reciveData, err => {
      alert(JSON.stringify(err, null, 4));
    });
}
onClick() {
    ble.read(deviceId, "DFB0", "DFB1", readReciveData, err => {
      alert(JSON.stringify(err, null, 4));
    });
}

It's going to go into readReciveData function, but I can only get first one byte of send data. and startNotification never triggered.

readReciveData(buffer){
    conse.log(String.fromCharCode.apply(null, new Uint8Array(buffer)));
}

I ried LightBlue app and could receive the notification normally.

stoneWeb avatar Apr 21 '22 04:04 stoneWeb

Does https://devdocs.io/dom/textdecoder give you any different results here? E.g.,

console.log(new TextDecoder().decode(new Uint8Array(buffer)))

peitschie avatar Apr 21 '22 04:04 peitschie

A strange behavior: When I connect the computer with USB and run the app on the phone with Xcode, my phone can normally trigger startNotification and receive the correct data. But when I unplug The USB and restart The app, startNotification says "The attribute could not be found." I use ble.autoConnect onDeviceReady.

stoneWeb avatar Apr 21 '22 06:04 stoneWeb

@stoneWeb I've encountered a similar issue here, and it was caused by an bug on the Arduino-based firmware side, where the BLE2902 descriptor was not being added to the characteristic that should support notify.

To address the firmware issue and avoid the error, the following snippet was added to the firmware side:

pCharacteristic->addDescriptor(new BLE2902());

This is based of these examples from the Arduino library itself (here) and the BLE C++ Guid.pdf.

I'll close this out, but should the issue persist, feel free to re-open and we can keep digging.

peitschie avatar Jan 29 '24 03:01 peitschie

Same underlying issue occurring here as https://github.com/randdusing/cordova-plugin-bluetoothle/issues/405

peitschie avatar Jan 29 '24 23:01 peitschie