Android BLE 连接时间间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21398766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:30:28  来源:igfitidea点击:

Android BLE Connection time interval

androidconnection-poolingbluetooth-lowenergyl2cap

提问by CoDe

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

我正在使用 Android BLE API 在 Nexus 4 上开发 BLE 应用程序。我有几个问题/疑问:

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.

1)有没有办法设置/覆盖BLE中央设备的连接或通知间隔。我发现对于Android,默认连接间隔固定为7.5ms。有没有办法更改此连接/通知延迟间隔设置。

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

来源: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?

2) 在连接到远程设备时,我遇到了随机时间间隔后断开连接的问题。有很多人面临连接断开问题,指出当他们使用 Android 4.3 API 进行 BLE 连接时,android 不稳定。有什么解决办法吗?

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

我在这里观察到的其他情况是,当重新连接需要时间时,它会带有一些 L2CAP 日志......在日志之后

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?

知道如何通过 L2CAP 清除 cahce mantain 吗?

回答by Glenn Ivar Klausen

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.

当我研究 Android BLE API 时,我找不到用于更改连接间隔和监督超时的 API(可能也是从设备延迟,我想不起来了)。我确实需要更改这些,您必须从从设备进行。

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.

Ashwini 的答案你可以忽略,他说的根本不正确。符合蓝牙 4.0 的主设备必须支持从 7.5 毫秒到 4.0 秒的连接间隔。从设备可以请求改变连接参数并发送连接参数更新请求,主设备会相应地更新连接参数。

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.

另一方面,主机(在您的情况下是 Android 设备)可能有兴趣自行更改连接间隔,以节省电量,并且您希望将连接间隔更改为更宽松的间隔。

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.

在我看来,Android API 甚至是一些设备上的硬件实现都不成熟,并且使用 BLE(就 BLE 的意图而言)在 Android 设备上消耗了大量能量。未来我相信你会在 API 层面看到更好的支持以及主机和控制器的划分,这样控制器即使在移动设备的主 CPU 处于睡眠状态时也能保持连接。这将节省大量电量,并且您可以 24/7 全天候保持与 BLE 设备的连接,而不会对您的电池寿命产生任何重大限制。

回答by user3278150

@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:

@Ashwini:正如在连接间隔为 7.5 的最后一条评论中提到的,它在一段时间内工作得更好。一段时间后,会出现 L2CAP 问题,除非重置蓝牙适配器,否则连接无法正常工作。此外,当蓝牙适配器被禁用并以编程方式重新启用时,会注意到一个奇怪的行为。我们在下面收到以下 GKI 错误:

> /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) ?

此外,这个 createBond API 是否有助于改善与 BLE 设备的连接(实际上不需要配对连接)?

回答by Vit Bernatik

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.

自 Android Lollipop API 级别 21 起,您可以使用: requestConnectionPriority()具有 3 个级别:CONNECTION_PRIORITY_BALANCED、CONNECTION_PRIORITY_HIGH 或 CONNECTION_PRIORITY_LOW_POWER。

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

它仍然没有我们开发人员想要的那么通用,但至少有一些东西......

回答by Magic

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.

1) 在运行您的应用程序之前,通过蓝牙设置手动将您的 Android 设备与远程设备配对。

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

2)或者您可以在代码中以编程方式将它们配对。这是我在网上找到的配对代码,对我有用

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());
        }
    }

回答by Ashwini Shahapurkar

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 issuelogged 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.

关于断线问题,我也遇到过类似的问题。我认为,这是由于在给定链接中登录的问题。当操作系统处理连接和配对时,您无法取消设备配对,此后所有重新连接尝试都失败。如果设备在 4.3 上使用私有 API 以编程方式配对(和未配对),则不会发生这种情况,假设您已正确处理 GATT 连接。在 android 4.4.2 中,添加了用于配对的 createBond API。因此,在 4.4.2 中,通信效果很好。

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.

关于android上蓝牙低功耗的稳定性,我可以确认它非常不稳定。我已经使用低能量传感器测试了安装在 Nexus 4 和 Nexus 5 上的应用程序。尽管具有相同的操作系统版本 4.4.2,但两种设备的连接结果不同。我观察到设备在 Nexus 4 上一段时间后断开连接,而在 Nexus 5 上运行良好。

Hope this helps you moving forward for your problem.

希望这可以帮助您解决问题。