最近出于项目需要,花了几天时间,研究了一下低功耗蓝牙(BLE)的开发,为了让有需要的小伙伴们少走弯路,现将我所遇到的问题分享出来。
刚开始,我被低功耗蓝牙(BLE)的基础概念所困扰,想当然的以为低功耗蓝牙仅仅是传统蓝牙的升级版本,只要传统蓝牙能做的,低功耗蓝牙理应可以做到。这从一开始就陷入误区了。此外,也想当然地把低功耗蓝牙外设和中心的概念直接往C/S模式硬套,认为低功耗蓝牙的外设就是client,而中心就是server。这当然是不对的。蓝牙的外设在往外发广播,中心搜索到广播之后,可以发起并建立连接,外设和外设,中心和中心都无法直接连接,只有外设和中心搭配才可以建立连接。仔细分析,其实外设更像是server端。
为什么要做低功耗蓝牙开发?
很简单,由于ios的同事提出,无法用传统蓝牙的socket接口与我们的设备通讯,ios仅有关于BLE的开发库(sdk)。
低功耗蓝牙是什么?
从传统蓝牙(或说标准蓝牙)4.0的版本开始,新开发了低功耗蓝牙的分支,后来的蓝牙基本都实现了双模,也就是既支持传统蓝牙,又支持低功耗蓝牙。潜台词是,有的模块可能仅支持低功耗蓝牙,或者传统蓝牙。其实,传统蓝牙和低功耗蓝牙,不仅协议不同,通讯交互的流程不同,连硬件芯片也是不一样的。对我来说,最直观的体验就是,传统蓝牙需要配对,而低功耗蓝牙不需要配对,此外传统蓝牙的数据传输率还过的去(没仔细分析过,大概是超过1024字节的数据,会被自动分包吧),而低功耗蓝牙的数据传输率就不得不当当做一个问题来看待。(这里仅按照我的理解陈述,如有不妥还请指正。)
低功耗蓝牙适用于什么场景?
低功耗蓝牙据说一颗纽扣电池,就能供电好几个月甚至几年,可想而知,它在设计之初就极力的压缩能量消耗。所以,低功耗蓝牙适用于低速率、低频次、低功耗的短距离蓝牙通讯模块(如手环、鼠标、心率监测仪等)。
Android对低功耗蓝牙的支持如何?
Android系统从4.3(API 18)开始支持BLE,且从5.1(API 21)才开始支持MTU修改(默认MTU仅为23字节,而且传输本身用掉3字节),但是实际测试结果显示,安卓手机对低功耗蓝牙的适配性并不好,很多机型都不支持低功耗蓝牙连接,比如:华为荣耀6x(后刷的Android 7.0),华为mate 8等。实测leMax2(Android 6.0)是支持低功耗蓝牙连接的,只不过需要申请位置相关的权限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
不做此申明就无法建立连接。
低功耗蓝牙的通讯流程是怎样的?
首先开发外设(peripheral),随后开发中心(centre),比较简单,一步一步基本都是回调机制。详情参照Android官方文档说明和示例。
开发外设,用到一个java文件足矣,广播的关键代码如下:
bluetoothGattServer = mBluetoothManager.openGattServer(context, bluetoothGattServerCallback);
BluetoothGattService service = new BluetoothGattService(UUID_SERVER, BluetoothGattService.SERVICE_TYPE_PRIMARY);
//添加一个可读、可通知的特征值,用于远端接收信息;
//注意需要descriptor,远端才可实现通知;
characteristicRead = new BluetoothGattCharacteristic(UUID_CHARREAD, BluetoothGattCharacteristic.PROPERTY_READ
| BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID_DESCRIPTOR, BluetoothGattCharacteristic.PERMISSION_WRITE);
characteristicRead.addDescriptor(descriptor);
service.addCharacteristic(characteristicRead);
//添加一个可写的特征值,用于远端发送信息;
BluetoothGattCharacteristic characteristicWrite = new BluetoothGattCharacteristic(UUID_CHARWRITE,
BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ,
//BluetoothGattCharacteristic.PROPERTY_WRITE
//| BluetoothGattCharacteristic.PROPERTY_READ
//| BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_WRITE);
service.addCharacteristic(characteristicWrite);
bluetoothGattServer.addService(service);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
关键的 BluetoothGattServerCallback 代码如下:
/**
* 服务事件的回调
*/
private BluetoothGattServerCallback bluetoothGattServerCallback = new BluetoothGattServerCallback() {
/**
* 1.连接状态发生变化时
* @param device
* @param status
* @param newState
*/
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
logd(String.format("1.onConnectionStateChange:device name = %s, address = %sstatus = %s, newState =%s "
, device.getName(), device.getAddress(), status, newState));
if(newState == 2){
curDevice = device;
} else {
curDevice = null;
}
super.onConnectionStateChange(device, status, newState);
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
logd(String.format("onServiceAdded:status = %s", status));
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
logd(String.format("%s,onCharacteristicReadRequest:device name = %s, address = %s, requestId = %s, offset = %s",
characteristic.getUuid().toString(), device.getName(), device.getAddress(), requestId, offset));
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, characteristic.getValue());
}
/**
* 3. onCharacteristicWriteRequest,接收具体的字节
* @param device
* @param requestId
* @param characteristic
* @param preparedWrite
* @param responseNeeded
* @param offset
* @param requestBytes
*/
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] requestBytes) {
logd(String.format("%s,onCharacteristicWriteRequest:device name = %s, address = %s, requestId = %s, " +
"preparedWrite=%s, responseNeeded=%s, offset=%s, value=%s", characteristic.getUuid().toString(),
device.getName(), device.getAddress(), requestId, preparedWrite, responseNeeded, offset,
OutputStringUtil.toHexString(requestBytes)));
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);
String msg = OutputStringUtil.transferForPrint(requestBytes);
logd("4.收到:" + msg.getBytes().length + "=" + msg);
receiveMsg.append(new String(requestBytes));
if (receiveMsg.toString().contains(BluetoothUtil.END_FLAG)
&& receiveMsg.toString().endsWith(BluetoothUtil.END_FLAG)) {
String[] msgs = receiveMsg.toString().split(BluetoothUtil.END_FLAG);
for (String s : msgs) {
//4.处理响应内容
logd("length="+s.length() + ", cmd=" + s);
dealwithReceivedMsg(s,device);
}
receiveMsg = new StringBuilder();
}
}
/**
* 2.描述被写入时,在这里执行 bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS... 收,触发 onCharacteristicWriteRequest
* @param device
* @param requestId
* @param descriptor
* @param preparedWrite
* @param responseNeeded
* @param offset
* @param value
*/
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
logd(String.format("%s,onDescriptorWriteRequest:device name = %s, address = %s, requestId = %s, preparedWrite = %s, responseNeeded = %s, " +
"offset = %s, value = %s,", descriptor.getUuid().toString(), device.getName(), device.getAddress(), requestId, preparedWrite,
responseNeeded, offset, OutputStringUtil.toHexString(value)));
// now tell the connected device that this was all successfull
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}
/**
* 5.特征被读取。当回复响应成功后,客户端会读取然后触发本方法
* @param device
* @param requestId
* @param offset
* @param descriptor
*/
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
logd(String.format("%s,onDescriptorReadRequest:device name = %s, address = %s, requestId = %s", descriptor.getUuid().toString(),
device.getName(), device.getAddress(), requestId));
// super.onDescriptorReadRequest(device, requestId, offset, descriptor);
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, null);
}
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
logd(String.format("5.onNotificationSent:device name = %s, address = %s, status = %s", device.getName(), device.getAddress(), status));
}
@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
super.onMtuChanged(device, mtu);
logd(String.format("onMtuChanged:mtu = %s", mtu));
MTU = mtu - 3;
}
@Override
public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
super.onExecuteWrite(device, requestId, execute);
logd(String.format("onExecuteWrite:requestId = %s", requestId));
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
注意上述代码的 onMtuChanged 可以发挥关键作用。MTU默认取的是23,当收到 onMtuChanged 后,会根据传递的值修改MTU,注意由于传输用掉3字节,因此传递的值需要减3。为什么专门记录MTU,接下看看实际传输的函数,你就会明白。实际传输的代码如下:
private void doRealTransfer(String msg, BluetoothDevice device){
if (bluetoothGattServer != null && characteristicRead != null && device != null) {
msg += BluetoothUtil.END_FLAG;
byte[] temp, data = msg.getBytes();
int i = 0, j = 0;
for (; i < data.length / MTU; i++) {
temp = new byte[MTU];
//src:源数组, srcPos:源数组要复制的起始位置,
//dest:目的数组,destPos:目的数组放置的起始位置,length:要复制的长度
System.arraycopy(data, j, temp, 0, MTU);
logd("send: "+new String(temp));
characteristicRead.setValue(temp);
bluetoothGattServer.notifyCharacteristicChanged(device, characteristicRead, false);
j += MTU;
}
if (j < data.length) {
temp = new byte[data.length - j];
System.arraycopy(data, j, temp, 0, data.length - j);
logd("send: "+new String(temp));
characteristicRead.setValue(temp);
bluetoothGattServer.notifyCharacteristicChanged(device, characteristicRead, false);
}
} else {
logd("can not send message out by BLE.");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
这了是按照MTU的大小严格约束每次发送的数据包大小,如果不这么做,很可能远端接收就会出错。除非你的数据包大小本身就很小。同时可以看到,我还是用到了结束符(END_FLAG)这么个小技巧,每句话的结尾一定有且仅有一个结束符,这样对方在解析时,就可根据这个特征把接收到的数据拼接完整。
开发中心(当然实际项目中,BLE中心就是在IOS端实现了),首先是搜索与选择:
private void scanLeDevice(final boolean enable) {
logd("scanLeDevice:"+enable);
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// 搜索到了之后就添加到界面进行显示.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
logd("BLE:" + device.getName() + "," + device.getAddress());
if(isShowWaitNotice){
mHandler.sendEmptyMessage(HIDDEN_WAIT);
}
if(!TextUtils.isEmpty(device.getName()) && device.getName().startsWith("TGT")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
选中了某个设备后,就进行连接,连接成功就发现服务,解析服务、注册通知:
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
logd("BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
logd("Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
logd("Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
logd("Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
curDevice = device;
return true;
}
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int mtu = 185;
logd("request " + mtu + " mtu:" + mBluetoothGatt.requestMtu(mtu));
}
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
logd("Connected to GATT server.");
// Attempts to discover services after successful connection.
logd("Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
setStatus(getString(R.string.title_connected_to) + " " + getCurDeviceName());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
logd("Disconnected from GATT server.");
broadcastUpdate(intentAction);
setStatus(R.string.title_not_connected);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
parseGattServices(getSupportedGattServices());
} else {
logd("onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
int status) {
logd("onCharacteristicRead. status = " + status + ", value = "+ new String(characteristic.getValue()));
if (status == BluetoothGatt.GATT_SUCCESS) {
//broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
logd(String.format("onCharacteristicWrite: characteristic = %s, status = %s, value = %s",
characteristic.getUuid(), status, new String(characteristic.getValue())));
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
logd(String.format("onCharacteristicChanged: characteristic = %s", characteristic.getUuid()));
//broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
receiveMsg.append(new String(mReadCharacteristic.getValue()));
if (receiveMsg.toString().contains(BluetoothChatService.END_FLAG)
&& receiveMsg.toString().endsWith(BluetoothChatService.END_FLAG)) {
String[] msgs = receiveMsg.toString().split(BluetoothChatService.END_FLAG);
for (String s : msgs) {
logd(s.length() + ":" + s);
dealwithReceivedMsg(s);
}
receiveMsg = new StringBuilder();
}
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
logd(String.format("onDescriptorRead: descriptor = %s, status = %s", descriptor.getUuid(), status));
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
logd(String.format("onDescriptorWrite: descriptor = %s, status = %s", descriptor.getUuid(), status));
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
logd(String.format("onMtuChanged:mtu = %s", mtu));
MTU = mtu-3;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
中心发送数据,基本上和外设类似:
public void sendMessageBLE(String msg){
if (mBluetoothGatt != null && mWriteCharacteristic != null) {
msg += BluetoothChatService.END_FLAG;
byte[] temp, data = msg.getBytes();
int i = 0, j = 0;
for (; i < data.length / MTU; i++) {
temp = new byte[MTU];
System.arraycopy(data, j, temp, 0, MTU);
logd("send: "+new String(temp));
mWriteCharacteristic.setValue(temp);
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
logd(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic));
j += MTU;
}
if (j < data.length) {
temp = new byte[data.length - j];
System.arraycopy(data, j, temp, 0, data.length -j);
logd("send: "+new String(temp));
mWriteCharacteristic.setValue(temp);
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
logd(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic));
}
} else {
logd("can not send message out by BLE.");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
这里在顺带提一下,拼接数据包的技巧:
receiveMsg.append(new String(mReadCharacteristic.getValue()));
if (receiveMsg.toString().contains(BluetoothChatService.END_FLAG)
&& receiveMsg.toString().endsWith(BluetoothChatService.END_FLAG)) {
String[] msgs = receiveMsg.toString().split(BluetoothChatService.END_FLAG);
for (String s : msgs) {
logd(s.length() + ":" + s);
dealwithReceivedMsg(s);
}
receiveMsg = new StringBuilder();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
总结:
经过实测,低功耗蓝牙的功耗影响很小,设备增加BLE的广播后(并持续对外发送),对于设备的待机时长几乎没有影响。不过谷歌的demo中,是设置了10分钟关闭广播的。BLE外设持续开启广播会有什么其他影响,暂待发掘。
我在开发的过程中也参考了许多网上的资料,感谢众多小伙伴无私的分享。若有对我的具体实现感兴趣的,可联系我的邮箱:suyux8@163.com。
Android低功耗蓝牙介绍的官方资料:
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html
https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html
--------------------- 本文来自 Android-大雄 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qiandaxiong/article/details/78903969?utm_source=copy