Android BLE Connection time interval
Asked Answered
R

5

28

I am developing a BLE application on Nexus 4 using Android BLE API. I have a few questions/doubts:

1) Is there a way to set/override the connection or notification interval of BLE central device. I have found that for Android, the default connection interval is fixed to 7.5ms. Is there a way to change this connection/notification delay interval settings.

Source : http://processors.wiki.ti.com/index.php/Bluetooth_SensorTag?DCMP=lprf-stdroid&HQS=lprf-stdroid-pr-wiki1#Supported_Android_devices

2) On connecting to the remote device, I am facing an issue of getting disconnection after random period of time interval. There are many people facing the connection drop issue stating that android is unstable when they are using Android 4.3 API for BLE connection. Is there any solution for this?

EDIT

what else I observe here is when it is taking time to reconnect then it's coming with some L2CAP log ...following the log

Trying to create a new connection laststate_ BOND_NONE
D/BluetoothGatt( 9620): connect() - device: 1C:BA:81:11:CA:36, auto: true
D/BluetoothGatt( 9620): registerApp()
D/BluetoothGatt( 9620): registerApp() - UUID=1a9a0911-4d5c-41dc-8ac0-0284ef550510
D/BtGatt.GattService( 3208): registerClient() - UUID=1adsds0911-4sdsc-41dc-8ac0-0sdsdf550510
D/BtGatt.btif( 3208): btif_gattc_register_app
D/BtGatt.btif( 3208): btgattc_handle_event: Event 1000
D/BtGatt.btif( 3208): btif_gattc_upstreams_evt: Event 0
D/BtGatt.GattService( 3208): onClientRegistered() - UUID=1a9a0911-4d5c-41dc-8ac0-0284ef550510, clientIf=5
D/BluetoothGatt( 9620): onClientRegistered() - status=0 clientIf=5
D/BtGatt.GattService( 3208): clientConnect() - address=1C:BA:8C:1E:CA:36, isDirect=true
D/BtGatt.btif( 3208): btif_gattc_open
D/BtGatt.btif( 3208): btgattc_handle_event: Event 1004
D/BtGatt.btif( 3208): btif_get_device_type: Device [1c:ba:8c:1e:ca:36] type 2, addr. type 0
W/bt-l2cap( 3208): L2CAP - LE - cannot start new connection at conn st: 3

Any idea how can clear cahce mantain by L2CAP?

Regnal answered 28/1, 2014 at 6:50 Comment(11)
As for 1st) The smartphone does that for you. You can't adjust that in your app. Your BLE device might suggest an interval. But the smartphone will decide in the end. The decision also depends on wheather wifi is turned on or off. I read that somewhere on code.google.com/p/android/issues As for 2nd) You find answers on #17870689 The solution would be that android engineers fix their bugs. There are only work arounds. You have to implement your code properly as well. E.g. calling BluetoothGatt#close();Jawbone
so u saying it will be remain unstable in first case...it's seriously bad to work with this issue...1) sice application never come to know connection will be established or not and after so many try 2) either off wifi and do things/ restart device again hactic process...any suggestion so at least can stable connection interval.Regnal
is there any way to find out or to match client(android-device) connection time interval with slave (peripheral) define connection time interval...Regnal
This is the question I refered to in my first comment. It provides some detailed knowledge about connection interval and sniff tools. As far as I know, that connection interval gets matched during some kind of hand shake process. The stability is bad, but you can work with it when following the work around I provided in the previously linked SO-question. Your app comes to know when a connection is established by event. The connection is stable once connected.Jawbone
@Jawbone I saw that...n mostly this case be with when slave device not in use for 3min..am i right?....in my case, when app try to reconnect then It's not confirmed whether device will get connected or not...as L2CAP not allow to create connection(log mention in edit Q)...and this heppened within 3 min time interval also...Regnal
also can u elaborate more about hand shake process happening here?Regnal
Sorry, I can't ;) Maybe what you describe is a bug which you can search or report on code.google.com/p/android/issues/… Also note, that your peripheral device's BLE implementation can differ a lot from other devices and might include bugs as well.Jawbone
Heyy @Jawbone ..found code.google.com/p/android/issues/detail?id=58381 bit intresting...as I also getting log mentioning "Register with GATT stack failed"Regnal
also does any know meaning of individual for values 0, 2, 3 that is coming with L2CAP log "LE - cannot start new connection at conn st: 3"Regnal
@Jawbone did you got any related information here?Regnal
To keep connection interval 7.5 work far better now. But same L2CAP issue occur after some minute of testing(~4-5 min) with cycle of discovery->connect->disconnect->discovery. Some of mention connection automatic drop after 3 min of connection code.google.com/p/android/issues/detail?id=60284 ..Regnal
I
13

When I studied the Android BLE API, I could not find an API for changing the connection interval and supervision timeout (maybe slave latency as well, can't remember from the top of my head). I you do need to change these, you must do it from the slave device.

The answer from Ashwini you can just ignore, what he says is simply not correct. A Bluetooth 4.0 compliant master device must support connection intervals from 7.5 ms up to 4.0s. The slave device may request a change in connection parameters and sends a connection parameter update request, and the master will update the connection parameter accordingly.

On the other hand, the master (in your case the Android device) could have an interest in changing the connection interval on its own, in order to save power, and you would like to change the connection interval to a more relaxed interval.

In my opinion the Android API and even the hardware implementation on several devices are immature and using BLE, in the sense that BLE was intended, draws to much power on an Android device. In the future I believe you will see much better support on the API level and a division of the host and controller so that the controller can maintain connectivity even when the main CPU of the mobile device is sleeping. That will save a lot of power and you can maintain connectivity with your BLE devices 24/7 without any major constrain on you battery life.

Indeterminism answered 10/2, 2014 at 8:52 Comment(9)
Yes hope so for better solution. You are right, still there is no API expose to set time interval setting at client side, in my case Changes made at slave peripheral device to connection time interval. It was more like run and trial and it work better. I checked with time interval define up to 20 ms, thing is it work best for 7.5ms. Here you mention ". slave device may request a change in connection parameters.." how slave come to know this is now change in interval. interval?Regnal
It depends on the slave device. Lets say the device is a temperature sensor in a room. Then it does not make a lot of sense sending the temperature every 7.5 ms, it will just waste a lot of power and does not give you anything extra. Changing the connection interval to 4s, which still will be a perfectly good frequency for temp updates, but you will transmit 533 times less, and the current consumption is reduced accordingly, making you batteries last way way longer. And that is sort of the clue behind BLE.Indeterminism
Heyy Glenn, yes depending on kind of device work. In my case I required continues data to work on.Regnal
Ok, but is BLE the right choice for you then? The whole idea behind BLE is sending little data seldom. Basically keeping the radio off for as long as possible. If you need a data rate which require a connection interval of 7.5 ms the whole time, then you should probably be looking at BR or EDR devices as data rate and not power consumption is you primary concern.Indeterminism
Glenn..I am not sure how these two will be preferable over BLE, could u please tell me more on here.Regnal
As I said, BLE is intended for devices which sends little data seldom and the main objective is to save power so it can run on a coin cell battery for a year or even longer. Basic Rate (BR / ~1 MBit/s) or Enhanced Data Rate (EDR / ~3 Mbit/s) devices has less focus on power and more on transmitting data. In your case, it sounds like one of these would suit your need better.Indeterminism
thanks Glemnn...I will check it also and yes it will help me to understand concept more deeply :).Regnal
It's up to the slave device to decide (roughly) the connection interval and the "latency" (really badly named - it means the number of connection intervals that the slave can skip transmitting and still not be considered disconnected). I made a BLE remote control and initially set (on the slave) the connection interval to 1s. It apparently worked (with a Nexus 4) because there was a noticeable delay when pressing buttons. I changed the connection interval to 50-100 ms, and the "latency" to 50 and it worked as expected.Dysarthria
Im facing similar problem on Android and ble peripheral. I connect to a BLE peripheral and exchange dozens of packets (say 300) in approximately 20-30 seconds and then communication become very slow, starting taking 1.5sec, to write a characteristic and getting back its response. This amount of time is not acceptable for my application. If I disconnect and reconnect after every few hundred packets and resume process from there than it never slows down or stop working. Is this issue is related to limit of packets per connection or time duration for connection. Please helpSaffier
Q
5

@Ashwini : As mentioned in the last comment with connection interval 7.5 it is working better for some amount of time. After some time, the L2CAP issue appears and the connectivity doesnt work unless the bluetooth adapter is reset. Also, a weird behavior is been noticed when the bluetooth adapter is disabled and re-enabled again programtically. We get the following GKI error below:

> /GKI_LINUX( 2232): ##### ERROR : GKI_exception: GKI_exception(): Task State Table
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: TASK ID [0] task name [BTU] state [0]
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: TASK ID [1] task name [BTIF] state [1]
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: TASK ID [2] task name [A2DP-MEDIA] state [1]
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: GKI_exception 65531 Sending to unknown dest#####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: 
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): 
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: * GKI_exception(): 65531 Sending to unknown dest
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): ##### ERROR : GKI_exception: ***************
> 02-05 15:49:24.466 E/GKI_LINUX( 2232): #####

Also, does this createBond API helps in improving the connection with BLE device (which actually doesnt need pairing for connection) ?

Quiddity answered 6/2, 2014 at 6:3 Comment(0)
C
5

As for changing connection interval:

Since Android Lollipop API level 21 you can use: requestConnectionPriority() With 3 levels: CONNECTION_PRIORITY_BALANCED, CONNECTION_PRIORITY_HIGH or CONNECTION_PRIORITY_LOW_POWER.

It is still not so versatile as we developers would want, but at least something...

Chaffinch answered 2/10, 2017 at 9:9 Comment(0)
W
2

For your first question, as OneWorld said, it is advisable to let smartphone handle the connection delay interval settings. This settings are closely dependent on bluetooth hardware & stack used for particular smartphone.

About the disconnection issue, I also faced similar issues. I think, this is due to an issue logged in given link. When OS handles connection & pairing, you can not unpair the device and all the re-connection attempts fail after this. This doesn't happen if the device paired(& unpaired) programmatically using private APIs on 4.3, assuming you have have handled GATT connections correctly. In android 4.4.2, there is createBond API added for pairing. So with 4.4.2, communication works pretty well.

Regarding the stability of Bluetooth low energy on android, I can confirm that it is pretty unstable. I have tested an application installed on Nexus 4 & Nexus 5 with a low energy sensor. In spite of having the same OS version 4.4.2, both devices gave different results for connection. I observed that device got disconnected after certain time on Nexus 4 while for Nexus 5 things worked well.

Hope this helps you moving forward for your problem.

Wastage answered 5/2, 2014 at 16:31 Comment(6)
thanks for ur rpl, as u can see in last comment i mention connection improved after set time interval to 7.5 ms @peripheral, but after performing same cycle several time, got time out which is defined for 20 sec at client side. After then if perform connection then it result L2CAP issue(in log)..so to prevent it I restart BT pragmatically...and it solved L2CAP issue.Regnal
After doing so..it connect but before to reconnect it respond time-out(define in client app) 2-4 times..which gradually increase connection time to 50-60 sec..which is bad. Also to off-on BT any way not an solution..let me know if you have better choice to handle this issue.Regnal
Do u have any suggestion here?Regnal
If turning off and on BT solves issue for the time, make sure all GATT calls are handled correctly. The BT stack may be in inconsistent state to handle other requests.Wastage
yes that is mandate..but here some times BT not on...might be build 4.2.2 issue..some of mention already..n then have to do manual on-off. Some times it more weird when I try to off BT from setting screen but it not off BT..it just show message for off Bt but again start BT instead to do off it.Regnal
from where in API or any sniff tools logs I can find that the connection interval time is 7.5 ms??Misunderstanding
R
2

I met auto disconnection problem same as your second question. Two ways to solve it:

1) Manually pair you Android device with the remote device through Bluetooth setting before running your application.

2) Or you can programmatically pair them in your code. Here's the pairing code I found online, which works for me

private void pairDevice(BluetoothDevice device) {
        try {
            Log.d("pairDevice()", "Start Pairing...");
            Method m = device.getClass()
                    .getMethod("createBond", (Class[]) null);
            m.invoke(device, (Object[]) null);
            Log.d("pairDevice()", "Pairing finished.");
        } catch (Exception e) {
            Log.e("pairDevice()", e.getMessage());
        }
    }
Radiopaque answered 15/4, 2014 at 20:2 Comment(3)
Hi can you please elaborate it more.Regnal
Hi, of course. Check my updated answer. And I'm using Nexus 7 for testing.Radiopaque
Hi, This should be tried, also an measure issue while working with BLE..sometimes device bluetooth get crash...n it not start even if we do it manually....have any suggestion regards this. check my last comment of conversation with Ashwini.Regnal

© 2022 - 2024 — McMap. All rights reserved.