BLE(Bluetooth Low Energy)低功耗蓝牙兴起的原因
BLE蓝牙的兴起主要是因为可穿戴设备的流行,由于传统蓝牙不能满足可穿戴设备的续航要求,因此大部分可穿戴设备采用蓝牙4.0技术,即BLE蓝牙技术。
BLE的特点
快速搜索、快速连接、超低功耗连接和数据传输,但是数据传输速率低。
在Android开发中,BLE蓝牙一包数据最多20个字节,因此在Android系统下最好不要使用BLE蓝牙传输大量的数据
BLE蓝牙开发流程
1、申请蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
注意:在Android6.0以上需要打开位置,不然搜索不到蓝牙设备
2、获取蓝牙适配器,判断本地蓝牙是否可用,打开蓝牙
/*获取蓝牙适配器*/
mBluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
mBluetoothAdapter =mBluetoothManager.getAdapter();
/*** 判断蓝牙是否可用*/
public boolean isBlueAdapterEnable() {
return mBluetoothAdapter !=null &&mBluetoothAdapter.isEnabled();
}
如果本地蓝牙未打开,调用已下代码打开本地蓝牙
/* 打开蓝牙适配器*/
Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivityForResult(intent, 0);
3、建立BluetoothAdapter.LeScanCall类的对象
BluetoothAdapter.LeScanCallback scanCallback =new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
//TODO处理搜索到的蓝牙设备逻辑
}
};
4、搜索蓝牙设备
mBluetoothAdapter .startLeScan(scanCallback);
5、停止搜索蓝牙设备
mBluetoothAdapter.stopLeScan(scanCallback);
6、建立BluetoothGattCallBack类对象
private BluetoothGattCallbackgattCallback =new BluetoothGattCallback() {
/* 断开或连接 状态发生变化时调用* */
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
//连接成功
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.i(TAG, "连接成功");
gatt.discoverServices();
}else {
Log.i(TAG, "连接失败");
connected=false;
}
}
/* 发现设备(真正建立连接)*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
//直到这里才是真正建立了可通信的连接
connected=ture;
//获取初始化服务和特征值
mBluetoothGatt = gatt;
initServiceAndChara(mBluetoothGatt);
//订阅通知
mBluetoothGatt.setCharacteristicNotification(mBluetoothGatt.getService(NotifyUUID).getCharacteristic(NOTIFY_DESCRIPTOR), true);
}
/* 读操作的回调*/
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.e(TAG, "onCharacteristicRead()");
}
/* 写操作的回调*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.e(TAG, "onCharacteristicWrite() status=" + status +",value=" + HexUtil.encodeHexStr(characteristic.getValue()));
}
/ * 接收到硬件返回的数据 */
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.e(TAG,"onCharacteristicChanged()"+characteristic.getValue());
}
};
/*初始化mBlueGatt的服务和特征*/
private void initServiceAndChara(BluetoothGatt mBluetoothGatt) {
List bluetoothGattServices = mBluetoothGatt.getServices();
for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
List characteristics = bluetoothGattService.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
int charaProp = characteristic.getProperties();
if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) >0) {
NotifyUUID = bluetoothGattService.getUuid();
NOTIFY_DESCRIPTOR = characteristic.getUuid();
}
if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) >0) {
bleWriteGattCharacteristic = characteristic;
}
if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ)>0){
bleReadGattCharacteristic=characteristic;
}
Log.i("NOTIFY_DESCRIPTOR", "NotifyUUID:" +NotifyUUID +",NOTIFY_DESCRIPTOR" +NOTIFY_DESCRIPTOR);
}
}
}
7、读取蓝牙数据
public void readBleDeviceData(){
if (connected &&mBluetoothGatt!=null){
mBluetoothGatt.readCharacteristic(bleReadGattCharacteristic);
}
}
8、向蓝牙设备发送数据
bleWriteGattCharacteristic.setValue(byte[] bytes);
mBluetoothGatt.writeCharacteristic(bleWriteGattCharacteristic);
9、断开蓝牙设备连接
mBluetoothGatt.disconnect();