evothings / cordova-ble

Bluetooth Low Energy plugin for Cordova
http://www.evothings.com/
Apache License 2.0
242 stars 103 forks source link

writeCharacteristic problem #97

Closed angelorm closed 8 years ago

angelorm commented 8 years ago

Hey everyone!

I'm trying to write a characteristic with an ArrayBuffer but I'm always getting the same value inserted whether is the value that I write. I've also used the nRF Master Control Panel app and tried to write it in there (as a ByteArray) and it worked, so the problem isn't related with the device. This is what I'm doing to write it:

var ab = new ArrayBuffer(5);
var view = new Uint8Array(ab);
view[0] = 3;
view[1] = 2;
view[2] = 23;
view[3] = 76;
view[4] = 12;

 evothings.ble.writeCharacteristic(
             device.handle,
             char.handle,
             ab,
              function (win) {
                    console.log(win);
                     writeCharCallback.resolve(true);
              },
              function (errorCode) {
                     writeCharCallback.reject(errorCode);
              });

Am I doing something wrong? I probably am but I have no idea what.

nbezembinder1 commented 8 years ago

You seem to be writing an empty ArrayBuffer to your device instead of the Uint8Array.

angelorm commented 8 years ago

I've changed the ab to view.buffer and the same thing happens. The weird thing is that it isn't writing just zeros. It show this values {158,233,101,0,0} and I have no idea where they come from.

nbezembinder1 commented 8 years ago

Could you try writing the Uint8Array directly using writeCharacteristic?

Like so:

 evothings.ble.writeCharacteristic(
             device.handle,
             char.handle,
             new Uint8Array([3, 2, 23, 76, 12]),
              function (win) {
                    console.log(win);
                     writeCharCallback.resolve(true);
              },
              function (errorCode) {
                     writeCharCallback.reject(errorCode);
              }
);
angelorm commented 8 years ago

Thanks for the help, it worked. I thought I had to write the buffer instead of the Array, changed that and now it's working.

SOLUTION:

var view = new Uint8Array(5);
view[0] = 3;
view[1] = 2;
view[2] = 23;
view[3] = 76;
view[4] = 12;

 evothings.ble.writeCharacteristic(
             device.handle,
             char.handle,
             view,
              function (win) {
                    console.log(win);
                     writeCharCallback.resolve(true);
              },
              function (errorCode) {
                     writeCharCallback.reject(errorCode);
              });