Bluetooth 使用JavaScript读取可调用的特征数据

Bluetooth 使用JavaScript读取可调用的特征数据,bluetooth,Bluetooth,我有一个BLE(Bluetooth 4.0)计步器设备,我想使用Evothings插件(使用Javascript API与设备交互)从中读取特征数据 我正在使用以下代码- 设备连接时的回调= function deviceConnected(device) { console.log('Connected to device: ' + device.name) console.log('Reading services... Have patience!') device

我有一个BLE(Bluetooth 4.0)计步器设备,我想使用Evothings插件(使用Javascript API与设备交互)从中读取特征数据

我正在使用以下代码- 设备连接时的回调=

function deviceConnected(device) {
    console.log('Connected to device: ' + device.name)
    console.log('Reading services... Have patience!')
    device.readServices(
        null, // null means "read all services".
         readAllServicesCharacteristicsAndNotifications,
        // listAllServicesCharacteristicsDescriptors,
        readServicesError)
}
ReadAllServicesCharacteristics和Notifications的回调代码-

function readAllServicesCharacteristicsAndNotifications(device) {
    // Get data for 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 = "";
            for (var i = 0; i < test.length; i++) {
                console.log('data  element is ' + test[i]);
                console.log('data  element string is ' + String.fromCharCode(parseInt(test[i], 2)));
                result += String.fromCharCode(parseInt(test[i], 2));
            }

            console.log('data  is ' + result);
        },
        function (errorCode) {
        console.log('BLE readCharacteristic error: ' + errorCode);
     });
}
函数ReadAllServicesCharacteristics和Notifications(设备){
//获取每个服务的数据
console.log('number of services'+device.\u services.length)
console.log('number of characteristic'+device.\u services[0]。\u\u characteristics.length)
//var特性=服务。\特性[characteristicUUID]
设备读取特性(
设备._服务[0]。_特征[8]['uuid'],
功能(数据){
var测试=新的UINT8阵列(数据);
var结果=”;
对于(变量i=0;i
我认为可格式化数据表明数据是以二进制代码字节交换的。 我看到字节数组中的值,但不知道如何解释它


是否有人使用Evothings Javascript与BLE设备交互?

H i,通过BLE在Javascript中接收的数据是一个字节缓冲区

看看你是否能找到计步器的文档。查找从计步器发送的数据格式。然后相应地访问缓冲区中的数据

这里有一些例子

访问数据缓冲区中的字节值:

var buf = new Uint8Array(data);
var value1 = buf[0];
var value2 = buf[1];
要获取16位值,请使用按位或,例如:

var value = buf[0] | (buf[1] << 8);
希望这有帮助

// Get the signed int value in the buffer thats starts at
// byte index 2 (the int value represented by byte 2 and 3).
var value = evothings.util.littleEndianToInt16(data, 2)