D
D
Dmitry Argus Mobile2021-11-01 11:31:29
Bluetooth
Dmitry Argus Mobile, 2021-11-01 11:31:29

How to get bluetooth working in cordova mobile app?

Hello. Need help with bluetooth connection in cordova mobile app. Tried plugins: cordova-plugin-bluetoothle, cordova-plugin-ble-central, @iotize/cordova-plugin-iotize-ble,

I'm doing something wrong. Nothing wants to work. Tell me who did it. The task is to scan the environment and measure the signal level to another device. Thanks in advance.

new Promise(function (resolve) {
    bluetoothle.initialize(resolve, { request: true, statusReceiver: false });
  }).then(initializeSuccess, handleError);

  function initializeSuccess(result) {
    if (result.status === "enabled") {
      //myApp.alert("Bluetooth is enabled.");
      // myApp.alert(result);
      startScan();
    }
    else {
    }
  }

  function handleError(error) {
    var msg;
  }

  // function log(msg, level) {
  //   level = level || "log";
  //   if (typeof msg === "object") {
  //     msg = JSON.stringify(msg, null, "  ");
  //   }
  //   console.log(msg);
  //   if (level === "status" || level === "error") {
  //     var msgDiv = document.createElement("div");
  //     msgDiv.textContent = msg;
  //     if (level === "error") {
  //       msgDiv.style.color = "red";
  //     }
  //     msgDiv.style.padding = "5px 0";
  //     msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px";
  //     document.getElementById("output").appendChild(msgDiv);
  //   }
  // }

  var foundDevices = [];

  function startScan() {
    bluetoothle.startScan(startScanSuccess, handleError, { services: [] });
  }

  function startScanSuccess(result) {
    alert("startScanSuccess(" + result.status + ")");
    if (result.status === "scanStarted") {
      alert("Scanning for devices (will continue to scan until you select a device)...", "status");

    }
    else if (result.status === "scanResult") {
      if (!foundDevices.some(function (device) {
          return device.address === result.address;
        })) {
        addDevice(result.name, result.address);
      }
    }
  }

  // function retrieveConnectedSuccess(result) {
  //   log("retrieveConnectedSuccess()");
  //   log(result);
  //   result.forEach(function (device) {
  //     addDevice(device.name, device.address);
  //   });
  // }

  function addDevice(name, address) {
   alert("name = " + name + ' address = ' + address);
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question