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.
Android Bluetooth device scanned only one time
You should calling 'stopLeScan()' correctly. After adding stopLeScan your device should be detected correctly.
display Livedata using startLeScan()
Finally it works fine in AOSP Lollipop. This Question is closed
startLeScan with 128 bit UUIDs doesn't work on native Android BLE implementation
This is a reported bug at least in Android 4.3 JWR66Y:
- Filtering works, if I provide my 16bit UUID
- Filtering doesn't return any scan results, if I provide my 128bit UUID or if I provide both UUIDs
My setting: My device offers 2 UUIDs on advertising (1 16bit and 1 128bit) and 4 UUIDs on service discovery (1 128bit and 3 16bit).
Even if it gets fixed, I warn everybody against using the filter option provided by Android. For backward compatibility and since it's broken on Samsung Galaxy S3 with Android 4.3
Related Topics
How to Change Color of Vector Drawable Path on Button Click
Margins of a Linearlayout, Programmatically with Dp
How to Show Alphabetical Letters on Side of Android Listview
Android Understanding Heap Sizes
Onclick Method Not Working Properly After Nestedscrollview Scrolled
Android: Is There Any Free PDF Library for Android
Commitallowingstateloss() in Fragment Activities
App Links Intent Filters in Assetlinks.JSON Not Working on Android
Android Convert Px to Dp (Video Aspect Ratio)
How to Bypass The Firebase Cache to Refresh Data (In Android App)
Notification Passes Old Intent Extras
Android Sqlitedatabase Query with Like
Android - Set a Progressbar to Be a Vertical Bar Instead of Horizontal
Set Starting Height of Collapsingtoolbarlayout
How to Resolve Target 'Android-16'
Android Getting Exact Scroll Position in Listview
Handle Screen Orientation Changes When There Are Asynctasks Running