对于使用BLE(蓝牙低功耗)的Android应用程序,我正在尝试使用evothings插件订阅心率测量的混合设备通知。可以扫描设备,连接读出服务但无法订阅。
以下是一些代码片段:
app.connect = function(address, name)
{
app.stopLeScan();
console.log('connect('+address+')');
document.getElementById('deviceName').innerHTML = address + " " + name;
ble.connect(address, function(r)
{
app.deviceHandle = r.deviceHandle;
console.log('connect '+r.deviceHandle+' state '+r.state);
document.getElementById('deviceState').innerHTML = r.state;
if (r.state == 2) // connected
{
console.log('connected, requesting services...');
app.getServices(r.deviceHandle);
app.subscribe(r.deviceHandle);
}
}, function(errorCode)
{
console.log('connect error: ' + errorCode);
});
};
订阅功能:
app.subscribe = function(deviceHandle)
{
console.log('Write Descriptor..');
ble.writeDescriptor(deviceHandle,
"00002a37-0000-1000-8000-00805f9b34fb", // Characteristic
"00002902-0000-1000-8000-00805f9b34fb", // descriptor
new Uint8Array([1,0]),
function()
{
console.log('Status: writeDescriptor ok.');
},
function(errorCode)
{
// This error will happen on iOS, since this descriptor is not
// listed when requesting descriptors. On iOS you are not allowed
// to use the configuration descriptor explicitly. It should be
// safe to ignore this error.
console.log('Error: writeDescriptor: ' + errorCode + '.');
});
console.log('Subscribe..');
ble.enableNotification(deviceHandle,
"00002a37-0000-1000-8000-00805f9b34fb",
function(data)
{
console.log('byteLength: '+data.byteLength);
},
function(errorCode)
{
console.log('Error: enableNotification: ' + errorCode + '.');
});
}
日志的输出显示以下内容:
Write Descriptor.. index.js:44
Subscribe.. index.js:62
connected, requesting services... index.js:211
Error: enableNotification: JSON error. index.js:71
Status: writeDescriptor ok. index.js:51
s1: 0 00001800-0000-1000-8000-00805f9b34fb. 2 chars. index.js:231
c11: 00002a00-0000-1000-8000-00805f9b34fb. 0 desc. index.js:248
properties: PROPERTY_READ index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
c12: 00002a01-0000-1000-8000-00805f9b34fb. 0 desc. index.js:248
properties: PROPERTY_READ index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
s2: 0 00001801-0000-1000-8000-00805f9b34fb. 1 chars. index.js:231
c13: 00002a05-0000-1000-8000-00805f9b34fb. 1 desc. index.js:248
properties: PROPERTY_INDICATE index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
d26: 00002902-0000-1000-8000-00805f9b34fb index.js:268
s3: 0 0000180d-0000-1000-8000-00805f9b34fb. 3 chars. index.js:231
c14: 00002a37-0000-1000-8000-00805f9b34fb. 1 desc. index.js:248
properties: PROPERTY_NOTIFY index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
d27: 00002902-0000-1000-8000-00805f9b34fb index.js:268
c15: 00002a38-0000-1000-8000-00805f9b34fb. 0 desc. index.js:248
properties: PROPERTY_READ index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
c16: 00002a39-0000-1000-8000-00805f9b34fb. 0 desc. index.js:248
properties: PROPERTY_WRITE index.js:249
writeType: WRITE_TYPE_DEFAULT index.js:250
我对作为s3列出的炉灶率服务感兴趣。但启用通知会导致JSON错误。我希望有人能给出一些指示。
提前致谢
Maverick2k
答案 0 :(得分:1)
您似乎正在使用图书馆" easyble"来自evothings-例子。我是对的吗?
此外,功能" app.getServices"未在您的示例中列出,但似乎在您致电"订阅"之前无法完成。因此,在" subscribe"期间,UUID和特征和描述符的句柄不可用,并且任何使用它们的尝试都会失败。
我不知道" writeDescriptor"在这种情况下可能会成功;也许它的错误处理会以某种方式破坏。
无论如何,拨打电话"订阅"在" getServices"的最后打回来。这应该可以解决你的问题。