使用Cordova bluetoothSerial插件与BLE设备

问题描述:

我正在开发一个应用程序,以发送非常简单的消息给蓝牙低功耗设备。使用Cordova bluetoothSerial插件与BLE设备

得益于Nordic Semi的nRF UART v2.0应用程序,我使用我的开发平台(Galaxy S5 Android 4.4)测试了设备(Arduino主板和nRF8001屏蔽)之间可以建立正确的通信。它可以很好地工作,因为我可以从一台设备向另一台设备发送和接收消息,所以我对此产品的这一方面非常有信心。

我的应用程序,它是基于bluetoothSerial插件Cordova没有成功与BLE设备进行连接。这里是代码:

var app = { 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    onDeviceReady: function() { 
     document.getElementById('messagebox').innerHTML = "Device Ready"; 
     app.isBluetoothEnabled(); 
    }, 
    isBluetoothEnabled: function() { 
     document.getElementById('messagebox').innerHTML = "is Bluetooth Enabled"; 
     bluetoothSerial.isEnabled(
      function() { 
       document.getElementById('messagebox').innerHTML = "Bluetooth is enabled"; 
       app.deviceConnect(); 
      }, 
      app.bluetoothEnable() 
    ); 
    }, 
    bluetoothEnable: function() { 
     bluetoothSerial.enable(app.deviceConnect(), 
      function() { 
      document.getElementById('messagebox').innerHTML = "User did not want to connect."; 
      }); 
    }, 
    deviceConnect: function() { 
     bluetoothSerial.connect('DB:A2:49:84:F9:32', 
      function() { 
      document.getElementById('messagebox').innerHTML = "Connected to device"; 
      }, 
      function() { 
      document.getElementById('messagebox').innerHTML = "Not connected to device"; 
      }); 
    } 
}; 

app.initialize(); 

'messagebox'元素只在这里检查应用程序停止的每一步。

我试过.list和.discoverUnpaired方法。两者都在工作(.list只列出已经配对的设备),并使用discoverPaired方法,我可以看到我的设备的MAC地址,但.connect从来没有工作,即使在connectInsecure模式下,我也没有成功配对我的设备蓝牙参数中的Android操作系统(我不知道在尝试与应用程序建立任何连接之前是否需要它)。

有人可以告诉我,如果我误解bluetootSerial作品或(甚至是BLE)的方式吗?我是否必须通过MAC地址以外的其他参数来连接方法?

非常感谢

BluetoothSerial plugin在Android上使用蓝牙经典,所以它不会用于该用途的情况下工作。 (BluetoothSerial插件在iOS上使用BLE。)

尝试使用Bluetooth Low Energy Cordova plugin从Android或iOS连接到nRF8001。

看看bluefruitle example。它连接到北欧BLE UART服务。