This is the code that I’m trying to use. Could you please tell me what the problem is with it??
My code:
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
}else{
mBluetoothAdapter.enable();
} ;
} {
if (mChatService == null) setupChat();
}
What problem are you having? My guess is that you need to add both the BLUETOOTH and BLUETOOTH_ADMIN permissions to your app.
Note that the preferred solution is to use an intent to prompt is the user wants to enable bluetooth:
Also note that the call to
enableis 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 the Android guidelines on BluetoothEDIT added disable/wait/enable/wait sample code
This is sample code for requesting Bluetooth to turn off, then waiting for it to turn on. It must be run in a separate thread and not in the UI thread. It would be possible to encapsulate this in a
RunnableClass which sets a (preferablyvolatile) flag to to true if it completes successfully.Note: this sample code is known to have issues on some older devices that do not allow diable/enable to be called from user applications.