首页 > 解决方案 > Nativescript蓝牙读取连续数据

问题描述

我们正在使用一种 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?

标签: nativescriptnativescript-angular

解决方案


推荐阅读