android ble 获取ServiceUUID和CharacteristicUUID,DescriptorUUID的UUID发送指令打开蓝牙锁

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();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值