android 耗电分析工具ble搜索耗电吗

开发Ble(公司项目,防丢器)已经有一段时间,由于是第一次接触Ble而网上资料又不多,且android平台自身的差异性,遇到了很多问题。为了将来方便查阅,在此做下记录。
1.中兴手机,蓝牙手动断开后,无法再次链接问题。(可能别的手机也存在类似问题)
解决办法: 在连接gatt之前,对bluetoothadapter进行一次scan 顺利解决此问题。
2.Gatt链接不稳定(在部分手机上出现过,此问题先排除硬件问题。此处只针对自己遇到的情况,或许有别的方案,待补充)
解决办法: 由于用到了gattserver,在启动gattserver和连接gatt之间由于未做顺序的限制,导致了此问题。经过反复调试后,发现只需要先创建gattserver,在serviceadded之后,在去进行gatt链接,此时的gatt连接会比较稳定。
3.小米手机遇到的,在gatt连接之后,手动调用gatt.disconnect&&和 close。第二次链接后 自动断开,再次连接会重新绑定。
解决办法:也是无意中,看到一篇文章,写的是小米手机在gatt操作的时候 ,最好要做一下sleep操作,可能是因为小米底层对蓝牙的某些东西没有来得及释放(自己猜测)。
于是在gatt 连接上时,在gatt.discoverServices 之前,加上 sleep(500) ,发现断开后在连接 成功率大大提高。
4.蓝牙关闭和打开变得异常缓慢,甚至崩溃
此问题可能与当蓝牙关闭时,尚有为关闭的 gatt连接/gattserver。我的解决办法是,注册蓝牙状态的监听,当发现蓝牙状态变为BluetoothAdapter.STATE_TURNING_OFF
时,立刻释放所有的gatt连接,关闭gattserver。经不完全测试发现,蓝牙的关闭速度变得快多了。
5.当批量连接蓝牙时,不进行onConnectionStateChange 回调。
这个问题是自己 闲的蛋疼,在测试的时候,用了很多假的mac 地址。于是造成的后果可想而知,肯定的是连不上的。但是同时也发现了问题,当ble去连接一个不存在的mac,或者不在范围内的mac地址时,发现有时候
不会进行onConnectionStateChange的回调,觉得很蛋疼。个人觉得会不会是因为 ,同时发送的connectGatt 请求太多,导致了系统的蓝牙阻塞或者别的什么问题。没办法,既然发现了这个问题,就只好动手解决了。&&首先,我在批量连接connectGatt的时候,新增了一个扫描,只有当 要连接的 设备扫描到了,才进行连接。然后,我还对gatt的连接,做了一个同步(每次只能进行一个连接,上一个连接不结束的话,无法开始上一个连接,同时针对连接无回调的情况做了超时处理,超时的话立马释放当前连接)。
这么说可能感受不是很直观。但个人觉得,在操作手机蓝牙的时候,还是做个同步的好。 避免同时发生多个gatt的操作,倒是手机蓝牙奔溃等情况的发生。
.Gatt链接不稳定
最近因为在保存gatt连接状态的serveice中,增加了一个后台的lescan,突然发现gatt连接又变的不稳定了,不知道什么原因。走投无路的情况下,把后台的lescan放到另一个service中进行,发现好像gatt又变得稳定多了,目前测试下来
已经连了40分钟没有断开了。 不知道接下去会咋样。&&也不知道是不是因为在同一个线程中在gatt连接成功后,开启lescan会导致连接不稳定,或亦是手机本身的原因,希望高人指教。
还是上面的问题,今天早上来,又重试了后台扫描和gatt连接,发现又有两次断开,诶。。。。 郁闷,求高人指教了。
以上发现的问题,均为个人开发经验所得,如有不妥或者欠缺错误的地方,请大家指正批评。
原文链接 /xiaorenwu702/p/4307380.html
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1155次
排名:千里之外28624人阅读
android4.3 nei内置了ble并为上层app提供相应的接口来使用BLE功能。
BLE主要涉及的协议及术语:
GenericAttribute Profile (GATT)
BLE上层的协议都是基于GATT,它是一个通用的规范,通过BLE连接发送/接收属性值。
bluetoothSIG定义了很多的ble协议。
AttributeProtocol (ATT)
GATT是建立在ATT之上。也被称为GATT/ATT。
ATT运行在ble设备上,所以被优化,尽可能的占用较少的字节。
每一个属性被指定一个UUID,通过ATT传输的属性被格式化位特性(characteristics)或服务(services)。
Characteristic
一个特性包含一个单一的值和0-n个描述符(Descriptor)来描述这个特性值。一个特性可以被看作一个类型。
Descriptor
描述符被定义为属性,这些属性用来描述特性的值。
例如:规定特性值的取值范围,特性值的单位等
服务是一组特性的集合。例如:“心率检测”服务包含“心速测量”的特性。
角色和职责
中心与外围:被应用于BLE连接本身,中心角色设备扫描/寻找广播,外围设备角色发出广播。
GATTserver vs. GATT client:
这个决定了两个设备在建立连接之后如何交互。
android手机和BLE设备的区别:手机支持中心角色(centralrole),BLE设备支持peripheralrole。
一旦建立连接,他们就开始相互传输gatt数据,根据传输的数据的种类其中的一个可能作为服务器。
如果BLE设备想报告数据给手机,它可能让BLE设备的传感器作为服务器。如果BLE设备准备接收来自手机的数据,手机的传感器就作为服务端。
以上例子说明:androidapp
是GATT client,GATTclient
从GATT server 获取数据。也可以设计android app
作为GATTserver。
&uses-permission android:name=&android.permission.BLUETOOTH&/&
&uses-permission android:name=&android.permission.BLUETOOTH_ADMIN&/&
&uses-feature android:name=&android.hardware.bluetooth_le& android:required=&true&/&
如果想把app提供给不支持BLE的设备需要设置android:required=&fasle&,然后在运行时进行判断:
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
& & Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
& & finish();
1:获取BluetoothAdapter
BluetoothAdapter是所有蓝牙功能所必需的,整个系统只有一个BluetoothAdapter,获取BluetoothAdapter之后就可以进行各种蓝牙的操作了。
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
& & & & (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
2:启动蓝牙
通过isEnabled()判断是否启动,如果没有启动,通过下面的方式启动:
private BluetoothAdapter mBluetoothA
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
& & Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
& & startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
3:查找BLE设备
通过startLeScan查找LE设备,并实现LeScanCallback作为参数。
注意事项:1:一旦找到所查找的设备,立即停止扫描
2:设置扫描超时,避免循环扫描。
public class DeviceScanActivity extends ListActivity {
& & private BluetoothAdapter mBluetoothA
& & private boolean mS
& & private Handler mH
& & // Stops scanning after 10 seconds.
& & private static final long SCAN_PERIOD = 10000;
& & private void scanLeDevice(final boolean enable) {
& & & & if (enable) {
& & & & & & // Stops scanning after a pre-defined scan period.
& & & & & & mHandler.postDelayed(new Runnable() {
& & & & & & & & @Override
& & & & & & & & public void run() {
& & & & & & & & & & mScanning =
& & & & & & & & & & mBluetoothAdapter.stopLeScan(mLeScanCallback);
& & & & & & & & }
& & & & & & }, SCAN_PERIOD);
& & & & & & mScanning =
& & & & & & mBluetoothAdapter.startLeScan(mLeScanCallback);
& & & & } else {
& & & & & & mScanning =
& & & & & & mBluetoothAdapter.stopLeScan(mLeScanCallback);
& & & & ...
如果需要查找特定类型的LE设备,需要使用startLeScan(UUID[],BluetoothAdapter.LeScanCallback()),提供一个你的设备支持的服务的UUID数组。
private LeDeviceListAdapter mLeDeviceListA
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
& & & & new BluetoothAdapter.LeScanCallback() {
& & @Override
& & public void onLeScan(final BluetoothDevice device, int rssi,
& & & & & & byte[] scanRecord) {
& & & & runOnUiThread(new Runnable() {
& & & & & &@Override
& & & & & &public void run() {
& & & & & & & &mLeDeviceListAdapter.addDevice(device);
& & & & & & & &mLeDeviceListAdapter.notifyDataSetChanged();
& & & & & &}
& & & &});
注意:要么搜索经典蓝牙,要么搜索BLE,两者不能同时搜索。
连接GATTserver
使用connectGatt连接到BLE设备的GATT server,
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
将会返回一个BluetoothGatt实例,通过这个实例可以进行Gattclient的各种操作。调用者(androidapp)是Gattclient。GattCallback用来提供结果给客户端。
读取BLE的属性
如果androidapp已经连接了Gatt server并且发现了服务,就能够进行属性的读写了。
收取Gatt的通知
通常BLEapp需要被通知,如果BLE设备的特性发生了改变。
使用setCharacteristicNotification方法为一个特性设置通知:
private BluetoothGatt mBluetoothG
BluetoothGattCharacter
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
& & & & UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
一旦一个特性被设置为通知可用,远端设备的特性发生改变就会触发onCharacteristicChanged
// Characteristic notification
public void onCharacteristicChanged(BluetoothGatt gatt,
& & & & BluetoothGattCharacteristic characteristic) {
& & broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
关闭clientapp
结束和BLE设备的通讯后,需要释放资源:
public void close() {
& & if (mBluetoothGatt == null) {
& & mBluetoothGatt.close();
& & mBluetoothGatt =
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:61453次
排名:千里之外
原创:22篇
转载:16篇
(1)(1)(1)(2)(1)(2)(1)(2)(3)(3)(1)(1)(4)(4)(1)(10)(1)(1)Android ble 蓝牙4.0 总结一 - 推酷
Android ble 蓝牙4.0 总结一
本文介绍Android ble 蓝牙4.0,也就是说API level &= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level & 18,也是用不了蓝牙4.0的哦。
首先发一下官方的demo,有兴趣的可以过去看看:
。android系统4.3以上,手机支持蓝牙4.0,具有搜索,配对,连接,发现服务及特征值,断开连接等功能,下载地址:
一、了解api及概念
1.1 BluetoothGatt
继承BluetoothProfile,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到
1.2 BluetoothGattCharacteristic
相当于一个数据类型,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)
1.3 BluetoothGattDescriptor
描述符,对Characteristic的描述,包括范围、计量单位等
1.4 BluetoothGattService
服务,Characteristic的集合。
1.5 BluetoothProfile
一个通用的规范,按照这个规范来收发数据。
1.6 BluetoothManager
通过BluetoothManager来获取BluetoothAdapter
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
1.7 BluetoothAdapter
一个Android系统只有一个BluetoothAdapter ,通过BluetoothManager 获取
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
1.8 BluetoothGattCallback
已经连接上设备,对设备的某些操作后返回的结果。这里必须提醒下,已经连接上设备后的才可以返回,没有返回的认真看看有没有连接上设备。
private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
// 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){};
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){};
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
BluetoothGatt gatt = device.connectGatt(this, false, mGattCallback);
1.8.1:notification对应
onCharacteristicChanged
gatt.setCharacteristicNotification(characteristic, true);
onCharacteristicRead;
gatt.readCharacteristic(characteristic);
onCharacteristicWrite;
gatt.wirteCharacteristic(mCurrentcharacteristic);
1.8.4:连接蓝牙或者断开蓝牙&对应&
onConnectionStateChange;
onDescriptorRead;
onDescriptorWrite;
gatt.writeDescriptor(descriptor);
onReadRemoteRssi;
gatt.readRemoteRssi()
onReliableWriteCompleted;
onServicesDiscovered。
gatt.discoverServices()
1.9 BluetoothDevice
扫描后发现可连接的设备,获取已经连接的设备
二、开启蓝牙权限
&uses-permission android:name=&android.permission.BLUETOOTH&/&
&uses-permission android:name=&android.permission.BLUETOOTH_ADMIN&/&
&uses-feature android:name=&android.hardware.bluetooth_le& android:required=&true&/&
如果&android.hardware.bluetooth_le设置为false,可以安装在不支持的设备上使用,判断是否支持蓝牙4.0用以下代码就可以了
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, &设备不支持蓝牙4.0&, Toast.LENGTH_SHORT).show();
三、对蓝牙的启动关闭操作
1、利用系统默认开启蓝牙对话框
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
2、后台打开蓝牙,不做任何提示,这个也可以用来自定义打开蓝牙对话框啦
mBluetoothAdapter.enable();
3、后台关闭蓝牙
mBluetoothAdapter.disable();
四、扫描设备,连接设备,获取设备信息&,断开连接设备,自行查看官方demo,还是看demo比较清晰啊
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致为什么simpleBLEPeripheral与Android手机蓝牙4.0第一次通信慢? - 蓝牙Bluetooth 技术 - 德州仪器在线技术支持社区
为什么simpleBLEPeripheral与Android手机蓝牙4.0第一次通信慢?
发表于2年前
<input type="hidden" id="hGroupID" value="42"
我是使用&a href=&.cn/product/cn/CC2541& target=&extwin&>CC2541&/a>芯片运行例子simpleBLEPeripheral,作为终端从机,手机端开发的软件为主机,&/p>
&p>操作步骤如下:&/p>
&p>1、把手机蓝牙关闭(清除蓝牙之前连接的缓存),然后再打开(让蓝牙初始化完毕)。&/p>
&p>2、运行simpleBLEPeripheral终端。&/p>
&p>3、把手机慢慢的靠近终端,当手机扫描到的终端信号达到一定值时就建立连接通信,通信完毕断开连接。&/p>
&p>4、通信过程使用抓包软件,抓取到的数据包如下图:&/p>
&p>&a href=&/cfs-file.ashx/__key/communityserver-discussions-components-files/103/5226.1.jpg&>&img src=&/resized-image.ashx/__size/550x0/__key/communityserver-discussions-components-files/103/5226.1.jpg& border=&0& alt=& &>&/a>&/p>
&p>&a href=&/cfs-file.ashx/__key/communityserver-discussions-components-files/103/47004.2.jpg&>&img src=&/resized-image.ashx/__size/550x0/__key/communityserver-discussions-components-files/103/47004.2.jpg& border=&0& alt=& &>&/a>&/p>
&p>上面是第一次通信抓取的数据包,通信过程到断开连接需要700多毫秒(手机软件做记时),中间有比较多的通信数据包。&/p>
&p>当不用关闭手机蓝牙第二次通信时,抓取的数据包如下:&/p>
&p>&a href=&/cfs-file.ashx/__key/communityserver-discussions-components-files/103/1884.3.jpg&>&img src=&/resized-image.ashx/__size/550x0/__key/communityserver-discussions-components-files/103/1884.3.jpg& border=&0& alt=& &>&/a>&/p>
&p>终端与手机之间的通信过程就很快了,只需100多毫秒(手机软件记时),现在我就是不明白为什么第一次通信时中间需要传递那么多数据包,那些数据包有什么用的?能否去掉那些不必要的数据包,缩短两者之间的通信时间。不管第一次通信还是第二次通信如果通信保持在100多毫秒多好啊。希望TI工程师们帮帮忙,如何解决这个问题?&/p>&div style=&clear:&>&/div>" />
为什么simpleBLEPeripheral与Android手机蓝牙4.0第一次通信慢?
此问题尚无答案
All Replies
我是使用芯片运行例子simpleBLEPeripheral,作为终端从机,手机端开发的软件为主机,
操作步骤如下:
1、把手机蓝牙关闭(清除蓝牙之前连接的缓存),然后再打开(让蓝牙初始化完毕)。
2、运行simpleBLEPeripheral终端。
3、把手机慢慢的靠近终端,当手机扫描到的终端信号达到一定值时就建立连接通信,通信完毕断开连接。
4、通信过程使用抓包软件,抓取到的数据包如下图:
上面是第一次通信抓取的数据包,通信过程到断开连接需要700多毫秒(手机软件做记时),中间有比较多的通信数据包。
当不用关闭手机蓝牙第二次通信时,抓取的数据包如下:
终端与手机之间的通信过程就很快了,只需100多毫秒(手机软件记时),现在我就是不明白为什么第一次通信时中间需要传递那么多数据包,那些数据包有什么用的?能否去掉那些不必要的数据包,缩短两者之间的通信时间。不管第一次通信还是第二次通信如果通信保持在100多毫秒多好啊。希望TI工程师们帮帮忙,如何解决这个问题?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
进士3472分
1、看了一下你的数据,有错误的答复,ATT_NO+FOUND(0X0A),可能与你模块的发射功率等有关,具体待查
2、设备广播与连接的流程如下图所示:
3、你可以使用bonding 试一下
4、我也坐等TI 相关FAE的答复,希望能增长点姿势^_^
专注于电子电路设计,致力于智能产品的开发。欢迎加入:BLE/WIFI兴趣群
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
探花5816分
不清楚,是不是第一次需要查找服务?以后就缓存了?
我是学习者,不是德州仪器的 ,以免误导
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
进士3472分
看一下连接流程吧,或许给你点启发
专注于电子电路设计,致力于智能产品的开发。欢迎加入:BLE/WIFI兴趣群
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
第一次连接主从设备之间通信环节就多了很多,不知道为什么多那些数据包,按我们正常理解应该是第二次连接后的通信数据包就可以了。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
进士3073分
&顶起来,我这边也是手机与从机通讯,第一次连接相当慢,之后就快很多。也很纠结这个问题,希望TI工程师能够及时回复,解答,谢谢!
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
这个问题要顶!望TI的工程师们解答啊
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
是否还有回复??期待
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
楼主现在解决了这个问题了吗? 我也碰到这个问题了 跪求解决方案啊
You have posted to a forum that requires a moderator to approve posts before they are publicly available.}

我要回帖

更多关于 android 耗电优化 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信