Android bluetooth ACTION_DISCOVERY_FINISHED not working

I wrote my first Android app, and everything works very well, except that ... in the subroutine below, ACTION_DISCOVERY_FINISHED never seems to be called (either sent or received, or otherwise). No matter what, the block of code is that the "else if" does not work.

I tested only on my Motorola Atrix , so I wonder if this is a problem. Since I'm testing bluetooth functionality, I don’t think I can use the Android emulator for effective testing.

Thoughts?

private BluetoothAdapter mBtAdapter; mBtAdapter.startDiscovery(); private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { //do something } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { //do something else } } } 
+6
source share
2 answers

2 possible solutions:

  • Instead of creating an anonymous receiver, subclass BroadcastReceiver with the same implementation, then declare it in the project manifest (do not forget to declare that your recipient receives the actions you need).

  • Dynamically register it from your activity / service, this way:

     IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mReceiver, filter); 

I'm not sure that you need to unregister when registering from a service / service (I know what you need when registering from the application context), so check it out.

+17
source

You need to add a line

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

to your manifest

-1
source

Source: https://habr.com/ru/post/899885/


All Articles