Before learning how to discover and enable Bluetooth in Android let’s know a few things about Bluetooth.
Android Platform includes Bluetooth framework support that allows the device to wirelessly exchange data with other Bluetooth devices
Android provides the Bluetooth API to perform these various functions.
- Scan other Bluetooth devices
- Fetch a list of paired devices
- Connect to the other devices
Let’s get started with the code :
Give necessary permissions within the app’s manifest –
The BLUETOOTH permission permits the app to connect, disconnect, and transfer data with another Bluetooth device.
The BLUETOOTH_ADMIN permission permits the app to discover new Bluetooth devices and change the device’s Bluetooth settings.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Create an object of Bluetooth adapter to interface with Bluetooth –
We can communicate with Bluetooth using the BluetoothAdapter class provided by Android . If the adapter returns null, this implies Bluetooth is not supported in the current device.
private BluetoothAdapter mObjBTAdapter;
mObjBTAdapter = BluetoothAdapter.getDefaultAdapter();
Discover Nearby BT Devices:
1.create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mObjBlueToothReceiver = new BroadcastReceiver() {
public void onReceive(Context pObjContext, Intent pObjIntent) {
static final String BLUETOOTH_ACTION = pObjIntent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(BLUETOOTH_ACTION)) {
// BT device found. Get the BluetoothDevice
// information from the Intent.
BluetoothDevice deviceInfo = pObjIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
static final String DEVICE_NAME = deviceInfo.getName();
static final String MAC_ADDRESS = deviceInfo.getAddress();
}
}
};
2. register the BroadcastReceiver
IntentFilter btFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mObjBlueToothReceiver, btFilter);
3. call startDiscovery to discover nearby BT devices
mObjBTAdapter.startDiscovery();
4. unregister the BroadcastReceiver inside onDestroy
@Override
protected void onDestroy() {
super.onDestroy();
...
// unregistering the receiver for ACTION_FOUND
unregisterReceiver(mObjBlueToothReceiver);
}
Enable Bluetooth:
We can use the provided code snippet to turn ON the Bluetooth –
We used startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter to enable Bluetooth. The second parameter requestCode is a locally defined integer that must be greater than 0.
if (!mObjBTAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, requestCode);
}
OR
if (!mObjBTAdapter.isEnabled()){ mObjBTAdapter.enable(); }
Disable Bluetooth:
We can use the provided code snippet to turn OFF the Bluetooth –
if (mObjBTAdapter.isEnabled()){ mObjBTAdapter.disable(); }
Does this code work anymore? I am getting “Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`” for the line “startActivityForResult(…”