Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't cancel Bluetooth discovery process
    primarykey
    data
    text
    <p>I need to do a scan of bluetooth devices in the surrounding area for 6 to 12 seconds. After this time I need to stop the discovery of new devices.</p> <p>The following code should:</p> <ul> <li>Start scanning for bluetooth devices</li> <li>Print out any which are found</li> <li>After 6 seconds, cancel all discovery and repeat process</li> </ul> <p>The problem is that the bluetooth discovery is never cancelled. After this code runs for a minute or two, <strong>onReceive will get called tens of times in the same second...</strong> </p> <pre><code>public void startTrackingButton(View view) { Log.d("MAIN", "Track button pressed, isTracking: " + !isTracking); if (isTracking) { isTracking = false; } else { isTracking = true; Thread keepScanning = new Thread(new Runnable() { @Override public void run() { while (isTracking) { if (mBluetoothAdapter.isDiscovering()) { Log.d("MAIN", "Cancelling discovery!"); Log.d("MAIN", String.valueOf(mBluetoothAdapter.cancelDiscovery() + ":" + mBluetoothAdapter.getState())); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } startTracking(); try { Thread.sleep(6000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); keepScanning.start(); } } private void startTracking() { Log.d("MAIN", "Starting Discovery..."); mBluetoothAdapter.startDiscovery(); // Create a BroadcastReceiver for ACTION_FOUND BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { Log.d("MAIN", "Device Found..."); String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a // ListView Log.d("MAIN:", device.getName() + "\n" + device.getAddress()); } } }; // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister // during onDestroy } </code></pre> <p>Here is my logcat output:</p> <pre><code>//onReceive gets called many times in the same second??? 05-01 22:09:56.949: D/MAIN(3757): Cancelling discovery! 05-01 22:09:56.969: D/MAIN(3757): false:12 ///THIS SHOULD BE TRUE 05-01 22:09:56.969: D/MAIN(3757): Starting Discovery... 05-01 22:10:03.009: D/MAIN(3757): Starting Discovery... 05-01 22:10:03.579: D/MAIN(3757): Device Found... 05-01 22:10:03.579: D/MAIN:(3757): TOMSELLECK 05-01 22:10:03.579: D/MAIN:(3757): 06:07:08:09:A1:A1 05-01 22:10:03.579: D/MAIN(3757): Device Found... 05-01 22:10:03.579: D/MAIN:(3757): TOMSELLECK 05-01 22:10:03.579: D/MAIN:(3757): 06:07:08:09:A1:A1 05-01 22:10:03.589: D/MAIN(3757): Device Found... 05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK 05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1 05-01 22:10:03.589: D/MAIN(3757): Device Found... 05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK 05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1 05-01 22:10:03.589: D/MAIN(3757): Device Found... 05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK 05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1 </code></pre> <p><strong>Does anybody know how I can properly cancel all current and pending Bluetooth discovery??</strong></p> <p>Thanks for your help!</p> <p>P.S The reason I need to repeat the process is to get fresh signal strength values from nearby devices.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload