-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Connectivity using the Bluetooth API
The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
- Scan for other Bluetooth devices
- Query the local Bluetooth adapter for paired Bluetooth devices
- Connect to other devices through service discovery
- Transfer data to and from other devices
- Manage multiple connections
In order to use Bluetooth features in your application, you must declare the appropriate permissions in your manifest. The permissions you need differ based on the API level your app targets.
For apps that target Android 12 (API level 31) or higher, declare the runtime permissions BLUETOOTH_SCAN (when looking for Bluetooth devices), BLUETOOTH_ADVERTISE (when making the device discoverable), and BLUETOOTH_CONNECT (when communicating with already-paired devices). The legacy BLUETOOTH and BLUETOOTH_ADMIN permissions should be declared with android:maxSdkVersion="30" so the system only requests them on older devices. See Bluetooth permissions for the full guidance, including when ACCESS_FINE_LOCATION is still required (e.g. when BLUETOOTH_SCAN is not annotated with usesPermissionFlags="neverForLocation").
<manifest ... >
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Needed only if your app looks for Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- Needed only if your app makes the device discoverable. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Needed only if your app communicates with already-paired devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</manifest>- Get the
BluetoothAdaptervia the systemBluetoothManager.BluetoothAdapter.getDefaultAdapter()is deprecated — go throughBluetoothManagerinstead. TheContext.getSystemService(Class)overload used in Google's snippet was added in API 23, so on projects withminSdkbelow 23 use the backwards-compatibleContextCompat.getSystemService(...)fromandroidx.core:core(shown below).
// Works on every supported API level — ContextCompat falls back to the
// String-based getSystemService(Context.BLUETOOTH_SERVICE) lookup below API 23.
BluetoothManager bluetoothManager =
ContextCompat.getSystemService(this, BluetoothManager.class);
BluetoothAdapter bluetoothAdapter =
bluetoothManager != null ? bluetoothManager.getAdapter() : null;
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}- Enable Bluetooth. Prefer the Activity Result API over the deprecated
startActivityForResult(...)/onActivityResult(...)pair.
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}A dialog appears requesting user permission to enable Bluetooth. If the user responds "Yes", the system begins to enable Bluetooth, and focus returns to your application once the process completes (or fails).
The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer that must be greater than 0. The system passes this constant back to you in your onActivityResult() implementation as the requestCode parameter.
If enabling Bluetooth succeeds, your activity receives RESULT_OK in the onActivityResult() callback. If Bluetooth was not enabled due to an error (or the user responded "No") then the result code is RESULT_CANCELED.
Check out these libraries for easy handling of Bluetooth in your apps:
- Android-BluetoothSPPLibrary - Hardware developer? This is an easy way to use the Bluetooth Serial Port Profile
- RxAndroidBle - Easily handle Bluetooth Low Energy
- https://developer.android.com/develop/connectivity/bluetooth/ble/ble-overview
- http://www.tutorialspoint.com/android/android_bluetooth.htm
- https://developer.android.com/develop/connectivity/bluetooth
- http://code.tutsplus.com/tutorials/create-a-bluetooth-scanner-with-androids-bluetooth-api--cms-24084
- http://www.intorobotics.com/how-to-develop-simple-bluetooth-android-application-to-control-a-robot-remote/
- https://github.com/palaima/AndroidSmoothBluetooth
- https://github.com/DeveloperPaul123/SimpleBluetoothLibrary
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.