问题描述
我目前正在尝试为 Ionic BLE 中的特性编写一些内容。如果写入成功,我预计会收到一个响应数据包。但是我不知道如何接收这个数据包。每当我写入时,我都会收到成功回调,但是我不知道在哪里可以找到数据包。
这是我的代码:
this.ble.write(id,service,characteristic,buffer).then(
(data)=>{
if(data!= null){
this.setStatus(data)
}else{
this.setStatus("didn't work")
}
在成功回调中,传入的参数是一个'OK'的字符串。我该去哪里。
解决方法
您使用的插件会在成功或失败的情况下为 write method 提供回调。
在用例中使用它们的一种可能方法是:
var success = function() {
console.log("success");
};
var failure = function() {
alert("failure");
};
this.ble.write(id,service,characteristic,buffer,success,failure)
,
当您尝试写入 ble 特性(使用响应 api 写入)时,您会收到确认响应。如果您想要响应中的数据,您可以使用通知 api。startNotification 注册一个回调,每次特征值更改时都会调用该回调。
ble.startNotification(device_id,service_uuid,characteristic_uuid,成功,失败);
您可以参考 here 的 startNotification api 示例。