【问题标题】:How to see if device is Connected如何查看设备是否已连接
【发布时间】:2019-10-03 12:11:05
【问题描述】:

我有一个使用 BLE-PLX 库通过蓝牙连接到外部设备的应用程序。

我在扫描和连接过程中遇到问题,因为它并不总是有效。

这是我用来通过蓝牙连接一台设备的代码:

// First Scan
    scans1() {
    this.manager.startDeviceScan(null, null, (error, device) => {
      if (error) {
          this.manager.stopDeviceScan();
          console.log("0.Error, retry connection.")
          this.scans1()
        //return;
      }
      if ((device.name == this.model_dx(this.props.Model)) || (device.name == this.model_sx(this.props.Model)))
      {
        this.manager.stopDeviceScan();
        console.log("1.Device Founded - ", device.name)
        this.setState({device1: device})
        this.manager.connectToDevice(device.id)
          .then(() => {
          console.log("2.Launch Scans 2") // this is for connect to the second device. 
          this.scan2();
          })
          .catch(() => {
          Alert.alert("Error " +  "Connection Failed.");
          Actions.homepage();
          })
      }
      else if ((device.name == null )) {
        this.manager.stopDeviceScan();
        console.log("3.Device is - null - retry scan")
        this.scans1();
      } else {
        this.manager.stopDeviceScan();
        console.log("4.Error: Device not found.")
        Actions.homepage();
      }
    });

现在我想知道的是,如何查看设备是否已连接? (我不知道是否可能是另一种错误,我已尝试解决任何问题)。

按照他们说的指南:

检查设备的连接状态。

isDeviceConnected(deviceIdentifier: DeviceId): Promise<boolean>

参数

deviceIdentifier (DeviceId) 设备标识符。

返回

Promise<boolean>:

如果设备已连接,则 Promise 发出 true,否则发出 false。

但我不明白如何使用它。

【问题讨论】:

    标签: javascript react-native bluetooth


    【解决方案1】:

    我找到了解决办法:

    我可以使用:

    this.manager.isDeviceConnected(device.id).then((res) => console.log(res))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 2013-07-09
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多