ServiceUUID和CharacteristicUUID,DescriptorUUID的介绍参考以下两篇:
https://www.cnblogs.com/xxzjyf/p/x_x_z_j_y_f.html
https://www.jianshu.com/p/3711cfbf7128
BLE 开发找到一个还可以的蓝牙框架:https://github.com/xiaoyaoyou1212/BLE
贴出重要代码如下:
发送数据需要上面提到的三个UUID,一般需要厂家提供,而自己买的蓝牙单片机继电器,没有提供,所有只能自己去查找:
代码://由于不知道具体哪个UUID写入指令有效,所以把指令(openlock开锁指令字节数组,我这里是16进制的需要转换为字节数组)循环写入所有对象
//框架根据蓝牙地址查找并连接上单片机蓝牙设备
ViseBle.getInstance().connectByMac(bluetoohAddress, new IConnectCallback() { @Override public void onConnectSuccess(DeviceMirror deviceMirror) { linkedDevice.put(Integer.valueOf(clickBluetoohPos),deviceMirror); bluetoothLeDevice=deviceMirror.getBluetoothLeDevice(); Log.e("link blue", "is ok"); getUUIDAndSendData(deviceMirror); } @Override public void onConnectFailure(BleException exception) { Log.e("link blue", "is fail=" + exception.getDescription()); } @Override public void onDisconnect(boolean isActive) { Log.e("link blue", "is onDisconnect because " + isActive); } }); }
private void getUUIDAndSendData(DeviceMirror deviceMirror){ for (int i = 0; i < deviceMirror.getBluetoothGatt().getServices().size(); i++) { BluetoothGattService bluetoothGattServer = deviceMirror.getBluetoothGatt().getServices().get(i); serviceUid = bluetoothGattServer.getUuid(); for (int j = 0; j < bluetoothGattServer.getCharacteristics().size(); j++) { BluetoothGattCharacteristic characteristic = bluetoothGattServer.getCharacteristics().get(j); characterUid = characteristic.getUuid(); needWriteSum++; writeToDpjBlooth(deviceMirror, serviceUid, characterUid, openlock); //descriptor值在写书时不是很重要,此次注释,调用填了一个相同的默认值 MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // for (int z = 0; z < characteristic.getDescriptors().size(); z++) { // BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(z); // //descripUid = descriptor.getUuid(); // } } } }
//写入指令到单片机蓝牙的方法,openLock 字节数组指令
private void writeToDpjBlooth(final DeviceMirror deviceMirror, UUID serviceId, UUID characterUid, byte[] openLock) { BluetoothGattChannel bluetoothGattChannel = new BluetoothGattChannel.Builder() .setBluetoothGatt(deviceMirror.getBluetoothGatt()) .setPropertyType(PropertyType.PROPERTY_WRITE) .setServiceUUID(serviceId) .setCharacteristicUUID(characterUid) .setDescriptorUUID(MY_UUID) .builder(); deviceMirror.bindChannel(new IBleCallback() { @Override public void onSuccess(byte[] data, BluetoothGattChannel bluetoothGattChannel, BluetoothLeDevice bluetoothLeDevice) { Log.e("link blue", "bind ok="+data.toString()); writeSum++; if(writeSum>=needWriteSum){//所有需要发送的对象都已经发送完毕,应该已经发送到了对应开锁的属性对象,开锁了 //下面关闭Gatt传输协议和断开蓝牙连接,下次重新连接即可,避免出现Gatt 133状态错误 closeBlueToothGatt(deviceMirror); } } @Override public void onFailure(BleException exception) { Log.e("link blue", "bind fail="+exception.toString()); closeBlueToothGatt(deviceMirror); } }, bluetoothGattChannel); deviceMirror.writeData(openLock); }
//关闭蓝牙和Gatt输入通道 避免出现Gatt 133状态错误
private void closeBlueToothGatt(DeviceMirror deviceMirror){ deviceMirror.close(); deviceMirror.disconnect(); deviceMirror.clear(); ViseBle.getInstance().disconnect(); ViseBle.getInstance().clear(); }