Search code examples
androidbluetoothbluetooth-device-discovery

Android cancelDiscovery Bluetooth does not work


I want to stop Bluetooth discovery process by a button click. I use btAdapter.cancelDiscovery(), but my app still scans for devices. I already put BLUETOOTH_ADMINpermission in manifest. Do I miss something here?

Edit: Post from Logcat

DEBUG/TAG(2901): stop Discovery
ERROR/BluetoothEventLoop.cpp(98): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/2664/hci0
ERROR/BluetoothService.cpp(98): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
INFO/DTUN_CLNT(2664):     Client calling DTUN_METHOD_DM_START_DISCOVERY (id 2)
INFO/(2649): DTUN_ReceiveCtrlMsg: [DTUN] Received message [BTLIF_DTUN_METHOD_CALL] 4354
INFO/(2649): handle_method_call: handle_method_call :: received DTUN_METHOD_DM_START_DISCOVERY (id 2), len 0

Solution

  • I create a new project to test cancelDiscovery(). Once we call this method, ACTION_DISCOVERY_FINISHED is broadcasted. In my case, I use handler to repeat the discovery process, so I have to stop it as well.