Bluetooth background scanning not working when the app is background in cordova application

185 Views Asked by At

I am initializing the blueeothle and peripheral using the foll code:-

bluetoothle.initialize(function initializeResult(result) {
        console.log(result);
      },  {
        request: true,
        statusReceiver: true,
        restoreKey: "bluetoothleplugin",
      });
     bluetoothle.initializePeripheral(
        function initializeResult(result) {
          console.log("initialize peripheral");
          console.log(result);
        },
        function error(result) {
          console.log("init peripheral error");
          console.log(result);
        },
        {
          request: true,
          restoreKey: "bluetoothleplugin",
        }
      );
          

and I started the scan using the foll options:-

bluetoothle.startScan(
        (result: BluetoothScanResult) => {
          console.log("result in searching");
          // console.log(result);
          if (result.name) {
            bluetoothresult.push(result);
            resulllt.push(result);
            bluetoothresultconst.push(result);
            resullltconst.push(result);
          }
        },
        function startScanErrorCallback(err) {
          console.log("printing err");
          console.log(err);
        },
        { serviceUuids: [],allowDuplicates:true }
      );

      setTimeout(() => {
        bluetoothle.stopScan(
          (result) => {
            console.log("stopping the scanning");
            console.log(bluetoothresult);
            if (bluetoothresult.length != 0) {
              console.log("got result length is more than zero");
                }
          },
          function stopScanErrorCallback(result) {
            console.log("came error in stopping the scanning ");
            console.log(result);
          }
        );
      }, 40000);

I am getting the scan results fine but when i moved the application to background using the following code:- cordova.plugins.backgroundMode.moveToBackground();

i was getting empty results for the scan .Ive kept the interval for scan as 60000ms and 40000ms timeout for stopping but immediately when the app is made to run in foreground scan results were fine.

Note:-I am cheking the applicaiton in a Android 9 device ive even added permission for foreground service in manifest.xml of the cordova and plugin.xml also

0

There are 0 best solutions below