I'm trying to determine the preferred way for programmatically enabling bluetooth on Android. I've found that either of the following techniques works (at least on Android 4.0.4...):
public class MyActivity extends Activity {
public static final int MY_BLUETOOTH_ENABLE_REQUEST_ID = 6;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, MY_BLUETOOTH_ENABLE_REQUEST_ID);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_BLUETOOTH_ENABLE_REQUEST_ID) {
if (resultCode == RESULT_OK) {
// Request granted - bluetooth is turning on...
}
if (resultCode == RESULT_CANCELED) {
// Request denied by user, or an error was encountered while
// attempting to enable bluetooth
}
}
}
or...
BluetoothAdapter.getDefaultAdapter().enable();
The former asks the user for permission prior to enabling while the latter just silently enables bluetooth (but requires the "android.permission.BLUETOOTH_ADMIN" permission). Is one or the other old/obsolete and/or is one technique only available on some devices? or is it just a matter of personal preference as to which I use?