【问题标题】:Connection device through bluetooth通过蓝牙连接设备
【发布时间】:2020-01-27 12:57:26
【问题描述】:

我正在使用ble-plx 库通过蓝牙连接到设备。

我写了这段代码,但我不明白为什么有时它会失败,而有时却完美无缺。

错误在哪里,这意味着我的代码有时会失败但其他方面可以正常工作?

(我正在连接两台设备,所以我不知道在扫描中首先找到哪个。)。

scansione1() {
    this.manager.startDeviceScan(null, null, (error, device) => {
      if (error) { 
        // This is used if failed immediately the scan 
        this.manager.stopDeviceScan();
          Alert.alert("Error.")
          Actions.homepageutente()
        return; }
      console.log("Start Scan 1");
      if ((device.name == this.model_dx(this.props.Model)) || (device.name == this.model_sx(this.props.Model))) 
      {
        this.manager.stopDeviceScan();
        this.setState({dispositivo1: device.name})
        // I set this variable device1 because I'll use it in other page. 
        this.setState({device1: device})
        device
          .connect()
          .then( () => {
          console.log("Launch scansione 2")
          this.scansione2();
          })
          .catch(() => {
          //  --> When My connection fails I receive this error <--
          Alert.alert("Connection  bluetooth device 1 not working");
          Actions.homepage();
          });
      }
      else if ((device.name == null )) {
        this.manager.stopDeviceScan();
        this.scansione1();
      } else {
        this.manager.stopDeviceScan();
        Alert.alert("Device " + device.name + " Not find.");
        Actions.homepage();
      }
    });
}

非常感谢:)

【问题讨论】:

  • 代码失败时会发生什么?有错误信息吗?会发生什么?
  • @ipsascientiapotestas 我写了一条评论,当它失败时,我收到进入 catch
  • 如果再次运行代码,报错后能否重新连接?你可能只是连接不好
  • 是的,如果我重试它可以工作(可能是第三次)。所以不是代码的问题?我不明白为什么它不应该工作
  • 我可能是错的,但我看不出代码本身有什么大问题,尤其是当你重试时它确实有效,所以它可能是外部因素

标签: javascript reactjs react-native mobile-application


【解决方案1】:

您的代码似乎有效!特别是您可以连接到设备,但只需要尝试几次。我会检查以下事项以解决您的问题。

  • 尝试不同的设备 - 这将告诉您问题出在您的连接还是您正在连接的设备上。
  • 更换您的设备或运行应用程序的设备中的蓝牙芯片 - 这将为您提供一个全新的开始,帮助您解决问题。
  • 将您的问题报告给设备问题,看看其他人是否有类似的连接问题。

或者,添加一种在失败后重试连接的方法。我会这样实现:

let i=1;
while (i=1) {  
    if ((device.name == this.model_dx(this.props.Model)) || (device.name == this.model_sx(this.props.Model))) 
        {
            this.manager.stopDeviceScan();
            this.setState({dispositivo1: device.name})
        // I set this variable device1 because I'll use it in other page. 
            this.setState({device1: device})
            device
              .connect()
              .then( () => {
                  console.log("Launch scansione 2")
                  this.scansione2();
                  i++

              })
              .catch(() => {
          //  --> When My connection fails I receive this error <--
              Alert.alert("Connection  bluetooth device 1 not working");
              });
          }
}

【讨论】:

  • 谢谢 :) 我只有一个问题!在您看来,为什么如果我收到错误消息我无法重试连接(因为它进入了循环)并且我必须重新启动应用程序?
  • 例如,当我输入第一个错误时,我需要重新启动应用程序才能尝试连接
  • 我添加了一个版本的代码,它将继续尝试连接,直到它通过一个 while 循环连接。其他方法看stackoverflow.com/questions/38213668/…
猜你喜欢
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多