Android 4.3 Bluetooth Low Energy unstable
Important implementation hints
(Perhaps some of those hints aren't necessary anymore due to Android OS updates.)
- Some devices like Nexus 4 with Android 4.3 take 45+ seconds to connect using an existing gatt instance. Work around: Always close gatt instances on disconnect and create a fresh instance of gatt on each connect.
- Don't forget to call
android.bluetooth.BluetoothGatt#close()
- Start a new thread inside
onLeScan(..)
and then connect. Reason:BluetoothDevice#connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback)
always fails, if called insideLeScanCallback() {...}.onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
in the same thread on Samsung Galaxy S3 with Android 4.3 (at least for build JSS15J.I9300XXUGMK6) - Most devices filter advertising
- Better not use
android.bluetooth.BluetoothAdapter#startLeScan(UUID[] serviceUuids, LeScanCallback callback)
with the parameter to filter for certain service UUIDs because this is broken completely in Samsung Galaxy S3 with Android 4.3 and doesn't work for 128bit UUIDs in general. - Gatt always can process one command at a time. If several commands get called short after another, the first one gets cancelled due to the synchronous nature of the gatt implementation.
- I often see even on modern devices with Android 5, that Wifi interferes withs bluetooth and vice versa. As a last resort, turn off wifi to stabilize bluetooth.
Tutorial for beginners
A pretty OK entry point for newcomers could be this video tutorial: Developing Bluetooth Smart Applications for Android http://youtu.be/x1y4tEHDwk0
The issue and work around described below is probably fixed now by OS updates
Work around: I could "stabilize" my app doing that...
- I provide the user a setting "Restart Bluetooth". If that setting is enabled, I restart Bluetooth at some points that indicate the begin of BLE stack becoming unstable. E.g. if startScan returns false. A good point may also be if serviceDiscovery failes. I just turn Bluetooth off and on.
- I provide another setting "Turn off WiFi". If that setting is enabled, my app turns off Wifi when the app is running (and turns it back on afterwards)
This work around is based on follwoing experiences...
- Restarting Bluetooth helps to fix problems with BLE in most cases
- If you turn off Wifi, the BLE stack gets much more stable. However, it also works fine on most devices with wifi turned on.
- If you turn off Wifi, restarting Bluetooth fully recovers the BLE stack without the need to reboot the device in most cases.
Bluetooth Low Energy (BLE) implementation below 4.3 (API 18)
This is possible on specific devices, with very heavy fragmentation.
See:
- https://github.com/cjhuo/Android-Samsung-Ble-APIs-Bluez for Samsung devices with Android 4.1 e.g. some Galaxy S3 with original OS version.
- http://developer.samsung.com/ble for Samsung devices with Android 4.2 (e.g. Galaxy S4 with original OS version)
- https://www.htcdev.com/devcenter/opensense-sdk/bluetooth-smart/htc-ble-installation/ for HTC devices with Android 4.x
There may be more for other vendors...
I would advise sticking with Android 4.3 as much as possible, using the specific SDKs below 4.3 is a mess.
Related Topics
Resolved Versions for App (22.0.0) and Test App (21.0.3) Differ
Connectivitymanager Getnetworkinfo(Int) Deprecated
Android Custom Dropdown/Popup Menu
Android Ratingbar Change Star Colors
Notifydatasetchange Not Working from Custom Adapter
Change the Color of a Checked Menu Item in a Navigation Drawer
How to Do a Simple Search in String in Firebase Database
Broadcast Receiver Within a Service
How to Strike Through Text in an App Widget
Software Keyboard Resizes Background Image on Android
How to Get Device Id for Admob
How to Create Empty Constructor for Data Class in Kotlin Android
How Permission Can Be Checked at Runtime Without Throwing Securityexception
Android: Force Edittext to Remove Focus
How to Compile the Android Aosp Kernel and Test It with the Android Emulator
How to Use Web Camera in Android Emulator to Capture a Live Image