0

Hí,

I have an java application that makes use bluetooth to search for devices. When the user presses the native Bluetooth button to turn it off my application shows the bluetooth status offline. When the user presses the native Bluetooth button to turn it on, my application should go back to work but it does not. For my scan back to work I need to close and open my application.

How to fix this programmatically ?

matheuslf
  • 309
  • 1
  • 9
  • Did you see this: http://stackoverflow.com/questions/8188277/error-checking-if-bluetooth-is-enabled-in-android-request-enable-bt-cannot-be-r – DigitalNinja Nov 25 '15 at 01:42

1 Answers1

0

Make sure you have the following permissions in your manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

You can enable bluetooth by using the following in an Activity or Service class:

BluetoothAdapter.getDefaultAdapter().enable();

If you are trying to listen for when Bluetooth is enabled, register a BroadcastReceiver listening for the BluetoothAdapter.ACTION_STATE_CHANGED action:

final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
if (state == BluetoothAdapter.STATE_OFF) {
    // Bluetooth is turned off
} else if (state == BluetoothAdapter.STATE_ON) {
    // Do your scan
}
mWhitley
  • 453
  • 7
  • 14