Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

BluetoothAdapter StartDiscovery does not work in Foreground Service

My application starts a service that detects phone shaking. When the service detects a shake, it searches for a BT device within range and if a suitable device is found, it connects to it and sends data to it.

To check if the system is killing the service, I turn on the vibration when it detects shaking.

Everything works fine when Activity is on the main screen. However, when I close Activity, the service detects shaking, but the device search does not start (BroadcastReceiver onReceive does not receive any Intent).

Are there any limitations to searching for Bluetooth devices from the background service? How can this be solved?

App Permissions:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Start Discovery Code in ForegroundService.java

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
cnt.registerReceiver(receiver, filter);
btAdapter.startDiscovery();

Broadcast Receiver

private final BroadcastReceiver receiver = new BroadcastReceiver()
{
 public void onReceive(Context context, Intent i)
 {
 String action = i.getAction();
 //Log.d("BluetoothFinder", "receiver action: " + action);
 if (BluetoothDevice.ACTION_FOUND.equals(action))
 {
 BluetoothDevice device = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 int rssi = i.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
 String name = "<brak nazwy urządzenia>";
 if (device.getName() != null) name = device.getName();
 BTDevice d = new BTDevice(device.getAddress(), name, true,
 (device.getBondState() != BluetoothDevice.BOND_BONDED) ? BTDevice.UNBONDED : BTDevice.BONDED, rssi);
 if (addDevIfDoesntExists(d))
 {
 newBTDevice = d;
 Log.d("BluetoothFinder", "Znaleziono nowe urządzenie: " + d.MAC + " rssi: " + rssi);
 try { methodParamOnFound.call(); } catch (Exception e) {}
 }
 }
 else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
 {
 Log.d("BluetoothFinder", "ACTION_DISCOVERY_FINISHED");
 try { methodParamOnEnd.call(); } catch (Exception e) { }
 }
 else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
 {
 Log.d("BluetoothFinder", "ACTION_DISCOVERY_FINISHED");
 btDevices.clear();
 }
 }
};

Answer*

Draft saved
Draft discarded
Cancel
3
  • In my case, this SOMETIMES works. How does it not work on Services? Commented Apr 25, 2022 at 10:50
  • You're right, sometimes from within the service, the discovering won't start. Commented Apr 26, 2022 at 11:56
  • My app scanned for BLE fine before i added location. After adding location I needed to do this. Commented Mar 28, 2023 at 1:07

lang-java

AltStyle によって変換されたページ (->オリジナル) /