Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我想知道为什么discoverServices()和onGetService()之间需要时间。_Android_Bluetooth Lowenergy_Bluetooth Gatt - Fatal编程技术网

Android 我想知道为什么discoverServices()和onGetService()之间需要时间。

Android 我想知道为什么discoverServices()和onGetService()之间需要时间。,android,bluetooth-lowenergy,bluetooth-gatt,Android,Bluetooth Lowenergy,Bluetooth Gatt,我开发了一个使用ble的应用程序。我使用Eclipse作为开发工具 我的“使用ble的应用程序”安装在我的智能手机上。我把我的智能手机和电脑连接到usb。然后在我的智能手机上,我启动了我开发的“使用ble的应用程序” 我在Eclipse的Logcat上找到了以下日志 08-22 20:59:11.278: V/BluetoothLeService(23378): report STATE_CONNECTED 08-22 20:59:11.283: E/BluetoothLeService(233

我开发了一个使用ble的应用程序。我使用Eclipse作为开发工具

我的“使用ble的应用程序”安装在我的智能手机上。我把我的智能手机和电脑连接到usb。然后在我的智能手机上,我启动了我开发的“使用ble的应用程序”

我在Eclipse的Logcat上找到了以下日志

08-22 20:59:11.278: V/BluetoothLeService(23378): report STATE_CONNECTED
08-22 20:59:11.283: E/BluetoothLeService(23378): ccccc BluetoothGattCallback public void onConnectionStateChange end
08-22 20:59:11.283: I/MainActivity2(23378): BroadcastReceiver CONNECTED
08-22 20:59:11.288: I/MainActivity2(23378): BroadcastReceiver GATT_SUCCESS
08-22 20:59:11.288: I/BluetoothLeService(23378): getBtGatt()
08-22 20:59:11.288: V/MainActivity2(23378): onView mServicesRdy = false
08-22 20:59:11.288: V/MainActivity2(23378): onView mBtGatt = android.bluetooth.BluetoothGatt@4335ebb8
08-22 20:59:11.288: I/BluetoothLeService(23378): getNumServices()
08-22 20:59:11.288: D/BluetoothGatt(23378): discoverServices() - device: 98:7B:F3:52:48:11
08-22 20:59:11.288: E/MainActivity2(23378): mBtGatt.discoverServices() = true
08-22 20:59:11.288: D/BluetoothGatt(23378): discoverServices() - device: 98:7B:F3:52:48:11
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=00001800-0000-1000-8000-00805f9b34fb
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=00001801-0000-1000-8000-00805f9b34fb
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=0000180a-0000-1000-8000-00805f9b34fb
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=f000aa00-0451-4000-b000-000000000000
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=f000aa60-0451-4000-b000-000000000000
08-22 20:59:15.193: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=f000ccc0-0451-4000-b000-000000000000
在上面的日志中

08-22 20:59:11.288: D/BluetoothGatt(23378): discoverServices() - device: 98:7B:F3:52:48:11
08-22 20:59:15.188: D/BluetoothGatt(23378): onGetService() - Device=98:7B:F3:52:48:11 UUID=00001800-0000-1000-8000-00805f9b34fb
这大约需要3~4秒


我想知道为什么这部分大约需要3~4秒。

这是与蓝牙框架相关的。所有蓝牙LE操作都是异步的,并且会立即返回,但它们的功能是根据操作系统自身的内部调度执行的。正常情况下,启动扫描可能会启动得非常快,但在启动设备扫描回调之前,它可能会被延迟。我通常注意到,在最近关机或闲置相当长时间的设备上使用蓝牙功能时会出现延迟。通常在第一次扫描之后,所有后续扫描都是快速且响应迅速的


我认为操作系统执行了一些节省电池的操作,并在内部关闭了蓝牙收音机(这只是推测,我还没有进行任何测试来证明这一点)。

发现服务需要时间,因为蓝牙GATT协议有点糟糕,而且优化得非常糟糕。这是一个需要很多请求和响应的聊天协议。一个更好的办法是请求“给我GATT结构”,然后GATT服务器用一个包含所有信息的突发响应

所需时间取决于外围设备的GATT服务器具有多少服务、特征和描述符。它还取决于UUID是16位UUID还是128位UUID。这与安卓操作系统运行缓慢或类似无关

较新的Android版本(如果我没记错的话,可以是6和更高版本)在GATT发现期间临时将连接间隔更改为7.5毫秒,只是为了加快速度

您可以在Android设备上查看HCI日志,也可以使用空气嗅探器查看此发现过程


有时,一个简单的解决办法是,在连接设置后,外设会请求更大的MTU。

这完全取决于您使用的Android设备、您连接的蓝牙设备、连接条件等。换句话说,答案就是这样谢谢你的回答。谢谢你的回答。如果是这样,固件是否比应用程序有更大的影响@전철우 我的意思是,应用程序不能做比API提供的更多的事情。很多蓝牙内部组件都被抽象掉了,所以应用程序不能做任何危险的事情。蓝牙API基本上只提供硬件全部功能的一个子集。据我所知,应用程序无法绕过蓝牙API,直接控制硬件。