简体   繁体   中英

Using cordova bluetoothSerial plug-in with a BLE device

I am working on a app to send very simple message to a Bluetooth Low Energy device.

I tested using my development platform (Galaxy S5 Android 4.4) that a correct communication could be established between the device (Arduino board and nRF8001 shield) thanks to the nRF UART v2.0 app from Nordic Semi. It worked pretty well as I could send and receive messages from one device to the other, so I am very confident in this side of the product.

My app, which is based on bluetoothSerial plugin for Cordova does not succeed to do the connection with the BLE device. Here is the code:

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();

The 'messagebox' element is only here to check at each step where the app stopped.

I tried the method .list and .discoverUnpaired. Both are working (the .list only lists already paired device) and with the discoverPaired method I can see my device with its MAC address, but the .connect never worked, even in connectInsecure mode and I did not succeed to pair my device by the Android OS in the bluetooth parameters (I don't know if it is required prior to attempting any connection with the app).

Could someone tell me if I am misunderstanding the way bluetootSerial works or (even BLE)? Do I have to pass an other argument than MAC address to connect method?.

Many thanks

The BluetoothSerial plugin uses Bluetooth Classic on Android so it won't work for this use case. (The BluetoothSerial plugin uses BLE on iOS.)

Try the Bluetooth Low Energy Cordova plugin to connect to the nRF8001 from Android or iOS.

Look at the bluefruitle example . It connects to the Nordic BLE UART Service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM