将Cordova应用程序与Arduino Nano 33 BLE连接

时间:2019-12-22 00:50:38

标签: cordova arduino bluetooth-lowenergy

我正在尝试获取一个iOS Cordova应用程序以连接到Arduino Nano 33 BLE。我正在处理下面的Arduino BLE LED示例和Cordova Redbear BLE-Central示例。我的Arduino出现在应用程序中的设备列表中,但是当我尝试连接时,出现以下错误:

  

尝试在无对象引用上调用虚拟方法'java.util.UUID android.bluetooth.BluetoothGattCharacteristic.getUuid()'。

有人可以告诉我我做错了什么或如何使它正常工作吗?谢谢。

Arduino草图:

    #include <ArduinoBLE.h>

    BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

    // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
    BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

    const int ledPin = LED_BUILTIN; // pin to use for the LED

    void setup() {
      Serial.begin(9600);
      while (!Serial);

      // set LED pin to output mode
      pinMode(ledPin, OUTPUT);

      // begin initialization
      if (!BLE.begin()) {
        Serial.println("starting BLE failed!");

        while (1);
      }

      // set advertised local name and service UUID:
      BLE.setLocalName("LED");
      BLE.setAdvertisedService(ledService);

      // add the characteristic to the service
      ledService.addCharacteristic(switchCharacteristic);

      // add service
      BLE.addService(ledService);

      // set the initial value for the characeristic:
      switchCharacteristic.writeValue(0);

      // start advertising
      BLE.advertise();

      Serial.println("BLE LED Peripheral");
    }

    void loop() {
      // listen for BLE peripherals to connect:
      BLEDevice central = BLE.central();

      // if a central is connected to peripheral:
      if (central) {
        Serial.print("Connected to central: ");
        // print the central's MAC address:
        Serial.println(central.address());

        // while the central is still connected to peripheral:
        while (central.connected()) {
          // if the remote device wrote to the characteristic,
          // use the value to control the LED:
          if (switchCharacteristic.written()) {
            if (switchCharacteristic.value()) {   // any value other than 0
              Serial.println("LED on");
              digitalWrite(ledPin, HIGH);         // will turn the LED on
            } else {                              // a 0 value
              Serial.println(F("LED off"));
              digitalWrite(ledPin, LOW);          // will turn the LED off
            }
          }
        }

        // when the central disconnects, print it out:
        Serial.print(F("Disconnected from central: "));
        Serial.println(central.address());
      }
    }

科尔多瓦index.js:

    'use strict';
    // ASCII only
    function bytesToString(buffer) {
        return String.fromCharCode.apply(null, new Uint8Array(buffer));
    }

    // ASCII only
    function stringToBytes(string) {
        var array = new Uint8Array(string.length);
        for (var i = 0, l = string.length; i < l; i++) {
            array[i] = string.charCodeAt(i);
        }
        return array.buffer;
    }

    // this is RedBear Lab's UART service
    var redbear = {
        serviceUUID: "19B10000-E8F2-537E-4F6C-D104768A1214",
        txCharacteristic: "19B10001-E8F2-537E-4F6C-D104768A1214", // transmit is from the phone's perspective
        rxCharacteristic: "19B10002-E8F2-537E-4F6C-D104768A1214"  // receive is from the phone's perspective
    };

    var app = {
        initialize: function() {
            this.bindEvents();
            detailPage.hidden = true;
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
            refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
            sendButton.addEventListener('click', this.sendData, false);
            disconnectButton.addEventListener('touchstart', this.disconnect, false);
            deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
        },
        onDeviceReady: function() {
            app.refreshDeviceList();
        },
        refreshDeviceList: function() {
            deviceList.innerHTML = ''; // empties the list
            if (cordova.platformId === 'android') { // Android filtering is broken
                ble.scan([], 5, app.onDiscoverDevice, app.onError);
            } else {
                ble.scan([redbear.serviceUUID], 5, app.onDiscoverDevice, app.onError);
            }
        },
        onDiscoverDevice: function(device) {
            var listItem = document.createElement('li'),
                html = '<b>' + device.name + '</b><br/>' +
                    'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
                    device.id;

            listItem.dataset.deviceId = device.id;
            listItem.innerHTML = html;
            deviceList.appendChild(listItem);
        },
        connect: function(e) {
            var deviceId = e.target.dataset.deviceId,
                onConnect = function() {
                    // subscribe for incoming data
                    ble.startNotification(deviceId, redbear.serviceUUID, redbear.rxCharacteristic, app.onData, app.onError);
                    sendButton.dataset.deviceId = deviceId;
                    disconnectButton.dataset.deviceId = deviceId;
                    app.showDetailPage();
                };

            ble.connect(deviceId, onConnect, app.onError);
        },
        onData: function(data) { // data received from Arduino
            console.log(data);
            resultDiv.innerHTML = resultDiv.innerHTML + "Received: " + bytesToString(data) + "<br/>";
            resultDiv.scrollTop = resultDiv.scrollHeight;
        },
        sendData: function(event) { // send data to Arduino

            var success = function() {
                console.log("success");
                resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "<br/>";
                resultDiv.scrollTop = resultDiv.scrollHeight;
            };

            var failure = function() {
                alert("Failed writing data to the redbear hardware");
            };

            var data = stringToBytes(messageInput.value);
            var deviceId = event.target.dataset.deviceId;
            ble.writeWithoutResponse(deviceId, redbear.serviceUUID, redbear.txCharacteristic, data, success, failure);

        },
        disconnect: function(event) {
            var deviceId = event.target.dataset.deviceId;
            ble.disconnect(deviceId, app.showMainPage, app.onError);
        },
        showMainPage: function() {
            mainPage.hidden = false;
            detailPage.hidden = true;
        },
        showDetailPage: function() {
            mainPage.hidden = true;
            detailPage.hidden = false;
        },
        onError: function(reason) {
            alert("ERROR: " + reason); // real apps should use notification.alert
        }
    };

0 个答案:

没有答案