bcsphere / bluetooth

Please support! Kindly "Star" this standard Bluetooth Smart API / SDK :) The "bcsphere/bluetooth" is a phonegap/cordova plugin. It's a Bluetooth JavaScript API from JUMA, a Bluetooth expert team. It supports both Bluetooth 4.0 GATT/Bluetooth Low Energy (BLE) interface in iOS/Android and Bluetooth 2.1 Classic Rfcomm / L2cap socket interface as Bluetooth Serial Port/SPP, as well as iBeacon!
Apache License 2.0
298 stars 12 forks source link

BC.DataValue to be used as ArrayBuffer in Uint8Array does not work #20

Closed sixbladeknife closed 10 years ago

sixbladeknife commented 10 years ago

hi, I'm trying to send binary data over bluetooth through RFCOMM. I subscribe a incoming data callback using:

BTMod.connectedDevice.rfcommSubscribe(rxCallback);

and I define my callback similarly as:

rxCallback: function(data)  //is data of type BC.DataValue?
    {
        var rawData = new Uint8Array(data.value);
...
        console.log("hex received: " + data.value.getHexString());
        console.log("raw received: " + JSON.stringify(rawData));
...

logcat report as follow:

07-01 16:26:50.875: I/Web Console(17384): hex received: c0035a172200160100000000010000010000000300017a5268c1:166
07-01 16:26:50.875: I/Web Console(17384): raw received: {"byteOffset":0,"byteLength":0,"buffer":{"byteLength":0},"length":0}:168

which tells me rawData is a correctly instantiated empty Uint8Array object, which is not what I expected to have, while variable data contains expected binary data. It seems that data, which should be of BC.DataValue type, does not return a ArrayBuffer object with the method value. What am I doing wrong?

thanks in advance

bye

sixbladeknife

dantiana commented 10 years ago

Looking into the issue. Let's check.

lizhijumacc commented 10 years ago

'data.value' is BC.DataValue and the 'data' contains more information. use 'data' like this:

device.rfcommSubscribe(onDataAvaliable);
function onDataAvaliable(data){
    $("#notifyValue_hex").html(data.value.getHexString());
    $("#notifyValue_unicode").html(data.value.getUnicodeString());
    $("#notifyValue_ascii").html(data.value.getASCIIString());
    $("#notifyDate").html(data.date);
} 
lizhijumacc commented 10 years ago

BTW: data.value.value is an ArrayBuffer in Uint8Array type. you can new the rawData like this:

var rawData = new Uint8Array(data.value.value);
sixbladeknife commented 10 years ago

thanks lizhijumacc, that did the trick, though documentation is pretty much unclear about a "custom" use of BC.DataValue. I humbly suggest to expand it a bit.

However, to make me understand fully:

lizhijumacc commented 10 years ago

Sorry for that. the rfcommSubscribe callback parameter 'data' is not type of the BC.DataValue , 'data' is just a common JSON object which contains the BC.DataValue field, the 'data.value' is BC.DataValue, so you can call the BC.DataValue function 'getHexString' for data.value. the data.value.value is an ArrayBuffer of Uint8Array~

you can dump the internal data structure about the 'data' object.

device.rfcommSubscribe(onDataAvaliable);
function onDataAvaliable(data){
    alert(JSON.stringify(data));
} 

Thanks for your advice, I will check the document later.

sixbladeknife commented 10 years ago

ok! once again thank you