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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章