bluetooth - Using Evothings JavaScript to read BLE characteristic data -
i have ble (bluetooth 4.0)pedometer device want read characteristic data using evothings plugin (which uses javascript api interact device)
i using following code - callback when device connected =
function deviceconnected(device) { console.log('connected device: ' + device.name) console.log('reading services... have patience!') device.readservices( null, // null means "read services". readallservicescharacteristicsandnotifications, // listallservicescharacteristicsdescriptors, readserviceserror) }
callback code readallservicescharacteristicsandnotifications -
function readallservicescharacteristicsandnotifications(device) { // data each service console.log('number of services ' + device.__services.length) console.log('number of characteristic ' + device.__services[0].__characteristics.length) //var characteristic = service.__characteristics[characteristicuuid] device.readcharacteristic( device.__services[0].__characteristics[8]['uuid'], function (data) { var test = new uint8array(data); var result = ""; (var = 0; < test.length; i++) { console.log('data element ' + test[i]); console.log('data element string ' + string.fromcharcode(parseint(test[i], 2))); result += string.fromcharcode(parseint(test[i], 2)); } console.log('data ' + result); }, function (errorcode) { console.log('ble readcharacteristic error: ' + errorcode); }); }
i think ble data formatsuggests data exchanged binary code bytes. see value in byte array don't know how interpret it.
has used evothings javascript interact ble device?
h i, data received in javascript on ble byte buffer.
see if can find documentation pedometer. lookup @ format of data sent pedometer. access data in buffer accordingly.
here examples.
access byte values in data buffer:
var buf = new uint8array(data); var value1 = buf[0]; var value2 = buf[1];
to 16-bit value use bitwise or, example:
var value = buf[0] | (buf[1] << 8);
depending on data being little endian or big endian, bitwise or needs done in different ways.
here library can use has functions accessing values in byte buffer: https://github.com/evothings/evothings-examples/blob/master/resources/libs/evothings/util/util.js
here example:
// signed int value in buffer thats starts @ // byte index 2 (the int value represented byte 2 , 3). var value = evothings.util.littleendiantoint16(data, 2)
hope helps!
Comments
Post a Comment