javascript - 如何查看设备是否已连接

标签 javascript react-native bluetooth

我有一个应用程序,用于使用 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。

但是我不明白如何使用它。

最佳答案

我找到了解决方案:

我可以使用:

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

关于javascript - 如何查看设备是否已连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218842/

相关文章:

javascript - 如何在内部图像使用宽度 : 100% 的 div 上使用 height()

javascript - 在 Question2Answer 中特定帖子的第 n 段后添加 javascript

javascript - 向 SOAP WSDL 发送请求

javascript - 将参数从屏幕传递到 Header 组件 react 导航

javascript - 错误 : StaticInjectorError(AppModule)[HomePage -> BluetoothSerial]

java - 扫描蓝牙设备

javascript - 将卫星轨道添加到 WebGLEarth map (例如使用 Leafletjs 或 Cesiumjs)

react-native - 导航器更改返回按钮颜色

css - 如何将现有组件添加到 react-native-router-flux

蓝牙调试工具