简体   繁体   中英

BLE Scan in background service?

I am trying to run a background service which runs all the time and scans for BLE advertisements.

I started a service. It works as expected and runs all the time. When restarting the background service (after closing the activity), I am starting the BLE scan:

mBluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();

        ScanFilter.Builder builder = new ScanFilter.Builder();
        builder.setManufacturerData(0x0590,new byte[]{});
        ScanFilter filter = builder.build();
        filters = new ArrayList<ScanFilter>();
        filters.add( filter );
        ScanSettings settings = new ScanSettings.Builder()
                .setScanMode( ScanSettings.SCAN_MODE_LOW_LATENCY )
                .build();
  //      mBluetoothLeScanner.stopScan(mScanCallback);
        mBluetoothLeScanner.startScan(filters, settings, mScanCallback);

It works for 30-50 seconds. After that time, the scan callback does not send advertisement data.

Do you have any idea why it is stopped and what can I do? Is iBeacon or Eddystone a better solution? My intention is to continuesly listen for a message from a BLE device in the background while the app is closed. I am using android 8.1 with Xiaomi Redmi 5.

After android 8 google add some Background limits you can read it

As i know you have two option one is use JobScheduler an other way use ForgroundServices (I use this)

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