Android低功耗蓝牙通讯(1)

step2、扫描的回调

//蓝牙扫描回调接口

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback(){

@Override

public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

if (device.getName() == null) {

return;

}

Log.e(“—>搜索到的蓝牙名字:”, device.getName());

//可以将扫描的设备弄成列表,点击设备连接,也可以根据每个设备不同标识,自动连接。

}

};

3、连接蓝牙

step1、获取设备的mac地址,然后连接。

//获取所需地址

String mDeviceAddress = device.getAddress();

BluetoothGatt mBluetoothGatt = device.connectGatt(context, false, mGattCallback);

step2、onConnectionStateChange()被调用

  • 连接状态改变时,mGattCallback中onConnectionStateChange()方法会被调用,当连接成功时,需要调用 mBluetoothGatt.discoverServices();去获取服务。

step3、onServicesDiscovered()被调用

  • 调用mBluetoothGatt.discoverServices();方法后,onServicesDiscovered()这个方法会被调用,说明发现当前设备了。然后我们就可以在里面去获取BluetoothGattService和BluetoothGattCharacteristic。

  • 下面就是mGattCallback回调方法。

// BLE回调操作

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState){

super.onConnectionStateChange(gatt, status, newState);

if (newState == BluetoothProfile.STATE_CONNECTED) {

// 连接成功

mBluetoothGatt.discoverServices();

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

// 连接断开

Log.d(“TAG”,“onConnectionStateChange fail–>” + status);

}

}

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

super.onServicesDiscovered(gatt, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

//发现设备,遍历服务,初始化特征

initBLE(gatt);

} else {

Log.d(“TAG”,“onServicesDiscovered fail–>” + status);

}

}

@Override

public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){

super.onCharacteristicRead(gatt, characteristic, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

// 收到的数据

byte[] receiveByte = characteristic.getValue();

}else{

Log.d(“TAG”,“onCharacteristicRead fail–>” + status);

}

}

@Override

public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic){

super.onCharacteristicChanged(gatt, characteristic);

//当特征中value值发生改变

}

/**

  • 收到BLE终端写入数据回调

  • @param gatt

  • @param characteristic

  • @param status

*/

@Override

public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {

super.onCharacteristicWrite(gatt, characteristic, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

// 发送成功

} else {

// 发送失败

}

}

@Override

public void onDescriptorWrite(BluetoothGatt gatt,

BluetoothGattDescriptor descriptor, int status) {

super.onDescriptorWrite(gatt, descriptor, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

}

}

@Override

public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {

super.onReadRemoteRssi(gatt, rssi, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

}

}

@Override

public void onDescriptorRead(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {

super.onDescriptorRead(gatt, descriptor, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

}

}

};

4、获取特征

step1、ble设备相关的UUID

//写通道uuid

private static final UUID writeCharactUuid = UUID.fromString(“0000fff6-0000-1000-8000-00805f9b34fb”);

//通知通道 uuid

private static final UUID notifyCharactUuid =UUID.fromString( “0000fff7-0000-1000-8000-00805f9b34fb”);

  • 不同的ble设备的UUID不相同,请根据自己的设备初始化UUID。

step2、获取bluetoothGattCharacteristic(因为有的设备可能存在双服务的情况,所以这里遍历所有服务)

//初始化特征

public void initBLE(BluetoothGatt gatt) {

if (gatt == null) {

return;

}

//遍历所有服务

for (BluetoothGattService BluetoothGattService : gatt.getServices()) {

Log.e(TAG, “—>BluetoothGattService” + BluetoothGattService.getUuid().toString());

//遍历所有特征

for (BluetoothGattCharacteristic bluetoothGattCharacteristic : BluetoothGattService.getCharacteristics()) {

Log.e(“---->gattCharacteristic”, bluetoothGattCharacteristic.getUuid().toString());

String str = bluetoothGattCharacteristic.getUuid().toString();

if (str.equals(writeCharactUuid)) {

//根据写UUID找到写特征

mBluetoothGattCharacteristic = bluetoothGattCharacteristic;

} else if (str.equals(notifyCharactUuid)) {

//根据通知UUID找到通知特征

mBluetoothGattCharacteristicNotify = bluetoothGattCharacteristic;

}

}

}

}

step3、开启通知

  • 设置开启之后,才能在onCharacteristicRead()这个方法中收到数据。

mBluetoothGatt.setCharacteristicNotification(mGattCharacteristicNotify, true);

  • 1

5、发送消息

mGattCharacteristicWrite .setValue(sData);

if (mBluetoothGatt != null) {

mBluetoothGatt.setCharacteristicNotification(notifyCharactUuid , true);

mBluetoothGatt.writeCharacteristic(mGattCharacteristicWrite );

}

6、接收消息

  • 接收到数据后,mGattCallback 中的onCharacteristicRead()这个方法会被调用。

@Override

public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){

super.onCharacteristicRead(gatt, characteristic, status);

if (status == BluetoothGatt.GATT_SUCCESS) {

// 收到的数据

byte[] receiveByte = characteristic.getValue();

}else{

Log.d(“TAG”,“onCharacteristicRead fail–>” + status);

}

}

7、释放资源

  • 断开连接、关闭资源。

public boolean disConnect() {

if (mBluetoothGatt != null) {

mBluetoothGatt.disconnect();

mBluetoothGatt.close();

mBluetoothGatt = null;

return true;

}

return false;

}

五、开发中踩过的坑


  1. 通知开启后,才能读到数据,否则读不到。

  2. 发送数据时,如果一包数据超过20字节,需要分包发送,一次最多发送二十字节。

  3. 接收数据时,一次最多也只接收20字节的数据,需要将接收到的数据拼接起来,在数据的结尾弄一个特定的标识,去判断数据是否接受完毕。

  4. 每次发送数据或者数据分包发送时, 操作间要有至少15ms的间隔。

  5. 最近公司来了个新的蓝牙产品,发现获取不到需要的特征,后来打断点,发现他们蓝牙设备的通知特征根本没有,是他们给错协议了。。。所以建议各位开发的时候,如果一直连接失败,也可以查看一下写特征和通知特征是否为空,是不是卖家搞错了,协议和产品不匹配。(当然,这样马虎的卖家估计是少数)。

  6. 又补充来了!这个蓝牙如果出现扫描不到的情况,那是因为手机没有开启定位权限,清单文件中写上定位权限,代码中在动态获取下就OK了。

六、demo图示


点我下载

这里写图片描述

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

总而言之,Android开发行业变化太快,作为技术人员就要保持终生学习的态度,让学习力成为核心竞争力,所谓“活到老学到老”只有不断的学习,不断的提升自己,才能跟紧行业的步伐,才能不被时代所淘汰。

在这里我分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司20年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

还有高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

的态度,让学习力成为核心竞争力,所谓“活到老学到老”只有不断的学习,不断的提升自己,才能跟紧行业的步伐,才能不被时代所淘汰。

在这里我分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司20年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

[外链图片转存中…(img-MGFlmhKx-1713837062077)]

[外链图片转存中…(img-K5rrTenv-1713837062078)]

[外链图片转存中…(img-a6qiJldn-1713837062079)]

还有高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 23
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值