Nativescript蓝牙读取连续数据

时间:2019-05-22 10:28:06

标签: nativescript nativescript-angular

我们正在使用EEG设备,该设备每秒提供一次原始数据。使用nativescript-bluetooth时,我们可以扫描并连接设备,但读取仅返回一个值。我们想要的是连续读取和存储数据。

我尝试用bluetooth.read读取,其中结果打印一次,但是我希望它每秒被调用一次。

```
  bluetooth.connect({
  UUID: uuid,
  onConnected: (peripheral) => {

    peripheral.services.forEach((service) => {
      console.log("service found: " + JSON.stringify(service));
      this.deviceServiceData = service;
    });
    setTimeout(() => {
      this.readDataFromDevice(this.deviceServiceData, uuid);
      this.notify(this.deviceServiceData, uuid);
    }, 1000);

  },
  onDisconnected: function (peripheral) {
    console.log("Periperhal disconnected with UUID: " + peripheral.UUID);
  }
});
}

notify(service, uuid) {

  bluetooth.startNotifying({
    peripheralUUID: uuid,
    serviceUUID: service.UUID,
    characteristicUUID: service.characteristics[0].UUID,
    onNotify: function (result) {
    console.log("read: " + JSON.stringify(result));
   }
  }).then(function () {
    console.log("subscribed for notifications");
  }, function (err) {
    console.log("notify error error: " + err);
 });
 }

 readDataFromDevice(service, uuid) {

 bluetooth.read({
  peripheralUUID: uuid,
  serviceUUID: service.UUID,
  characteristicUUID: service.characteristics[0].UUID,

 }).then((result) => {
  var data = new Uint8Array(result.value);
  console.log(data);
 }, function (err) {
  console.log("read error: " + err);
 });
}

How should I get the values generated by the device every second? Do I call `readDataFromDevice` at every time interval?

0 个答案:

没有答案