如图:
BLE是Bluetooth low energy的意思,属于蓝牙低功耗协议,Android4.3以上及苹果手机等现在都支持蓝牙BLE,主要面向传感器应用市场,进行短时间小数据传输,如健康领域:手机监测血压,体育:手机计步器等。
低功耗蓝牙通讯协议:
1、权限问题
step1、在AndroidManifest.xml中声明权限
第二个权限是允许程序发现和配对蓝牙设备。
因为只有在API18(Android4.3)以上的手机才支持ble开发,所以还要声明一个feature。
required为false时,Android设备均可正常安装运行,需要在代码运行时判断设备是否支持BLE。
注意:还得写上定位权限,要不然有的机型扫描不到ble设备。
step2、获取蓝牙适配器
BluetoothManager mBluetoothManager =(BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
step3、判断手机蓝牙是否被打开
mBluetoothAdapter.isEnabled()
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);startActivity(enable);
2、搜索蓝牙
step1、开始扫描
//10s后停止搜索
new Handler().postDelayed(new Runnable() {@Overridepublic void run() {mBluetoothAdapter.stopLeScan(mLeScanCallback);}}, 1000 * 10);UUID[] serviceUuids = {UUID.fromString(service_uuid)};
mBluetoothAdapter.startLeScan(serviceUuids, mLeScanCallback);
因为蓝牙扫描比较耗电,建议设置扫描时间,一定时间后停止扫描。
如果不需要过滤扫描到的蓝牙设备,可用mBluetoothAdapter.startLeScan(mLeScanCallback);
进行扫描。
step2、扫描的回调
//蓝牙扫描回调接口
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback(){@Overridepublic 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()被调用
mBluetoothGatt.discoverServices();
去获取服务。step3、onServicesDiscovered()被调用
调用mBluetoothGatt.discoverServices();
方法后,onServicesDiscovered()
这个方法会被调用,说明发现当前设备了。然后我们就可以在里面去获取BluetoothGattService和BluetoothGattCharacteristic。
下面就是mGattCallback回调方法。
// BLE回调操作private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {@Overridepublic 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);}}@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {super.onServicesDiscovered(gatt, status);if (status == BluetoothGatt.GATT_SUCCESS) {//发现设备,遍历服务,初始化特征initBLE(gatt);} else {Log.d("TAG","onServicesDiscovered fail-->" + status);}}@Overridepublic 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);}}@Overridepublic void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic){super.onCharacteristicChanged(gatt, characteristic);//当特征中value值发生改变}/*** 收到BLE终端写入数据回调* @param gatt* @param characteristic* @param status*/@Overridepublic void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicWrite(gatt, characteristic, status);if (status == BluetoothGatt.GATT_SUCCESS) {// 发送成功} else {// 发送失败}}@Overridepublic void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {super.onDescriptorWrite(gatt, descriptor, status);if (status == BluetoothGatt.GATT_SUCCESS) {}}@Overridepublic void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {super.onReadRemoteRssi(gatt, rssi, status);if (status == BluetoothGatt.GATT_SUCCESS) {}}@Overridepublic void onDescriptorRead(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {super.onDescriptorRead(gatt, descriptor, status);if (status == BluetoothGatt.GATT_SUCCESS) {}}};
4、获取特征
step1、ble设备相关的UUID
//写通道uuidprivate static final UUID writeCharactUuid = UUID.fromString("0000fff6-0000-1000-8000-00805f9b34fb");//通知通道 uuidprivate static final UUID notifyCharactUuid =UUID.fromString( "0000fff7-0000-1000-8000-00805f9b34fb");
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、开启通知
mBluetoothGatt.setCharacteristicNotification(mGattCharacteristicNotify, true);
5、发送消息
mGattCharacteristicWrite .setValue(sData);if (mBluetoothGatt != null) {mBluetoothGatt.setCharacteristicNotification(notifyCharactUuid , true);mBluetoothGatt.writeCharacteristic(mGattCharacteristicWrite );}
6、接收消息
@Overridepublic 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;}
点我下载
欢迎关注技术公众号,微信号搜索ColorfulCode 代码男人
分享技术文章,投稿分享,不限技术种类,不限技术深度,让更多人因为分享而受益。