Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What problem are you having? My guess is that you need to add both the BLUETOOTH and BLUETOOTH_ADMIN permissions to your app.</p> <p>Note that the preferred solution is to use an intent to prompt is the user wants to enable bluetooth:</p> <pre><code> Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(enableIntent); </code></pre> <p>Also note that the call to <code>enable</code> is asynchronous, it will return immediately and enable Bluetooth in the background. Hence Bluetooth may not actually be enabled yet, it may still be getting ready. See also <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html" rel="nofollow">the Android guidelines on Bluetooth</a></p> <p><strong>EDIT</strong> added disable/wait/enable/wait sample code</p> <p>This is sample code for requesting Bluetooth to turn off, then waiting for it to turn on. It <strong>must</strong> be run in a separate thread and <strong>not</strong> in the UI thread. It would be possible to encapsulate this in a <code>Runnable</code> Class which sets a (preferably <code>volatile</code>) flag to to true if it completes successfully.</p> <p><strong>Note:</strong> this sample code is known to have issues on some older devices that do not allow diable/enable to be called from user applications. </p> <pre><code> BluetoothAdapter.getDefaultAdapter().disable(); while (BluetoothAdapter.getDefaultAdapter().isEnabled()) { try { Thread.sleep(100L); } catch (InterruptedException ie) { // unexpected interruption while disabling Bluetooth Thread.currentThread().interrupt(); // restore interrupted flag return; } } // disabled, re-enabling Bluetooth BluetoothAdapter.getDefaultAdapter().enable(); while (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { try { Thread.sleep(100L); } catch (InterruptedException ie) { // unexpected interruption while enabling bluetooth Thread.currentThread().interrupt(); // restore interrupted flag return; } } </code></pre>
 

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