通过蓝牙连接设备

时间:2019-09-27 08:14:02

标签: javascript reactjs react-native mobile-application

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

我编写了这段代码,但我不明白为什么有时有时会失败,而有时却能正常工作。

错误在哪里,这意味着我的代码有时会失败,但在其他情况下却可以正常运行?

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

$(function() {
   info = [];
   $(document).on('click', '.rowChk', function() {
     var id;
     if (this.checked) {

     } else {
       console.log(id); <--- Showing as undefined
       var index = info.findIndex(function(item) {
           return item.id === id;
       });
       if (index !== -1) {
          info.splice(index, 1);
          if (info.length === 0) {
             $('#rowClk').css('display', 'none');
          }
       }
     }
    });
  });

非常感谢您:)

1 个答案:

答案 0 :(得分:2)

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

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

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

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");
              });
          }
}