android 蓝牙发送数据

 

    private BluetoothGattCharacteristic getSupportedCharacteristic(String uuidService, String uuidChar) {
        BluetoothGattCharacteristic alertLevel = null;
        if (mGatt != null) {
            BluetoothGattService linkLossService = mGatt.getService(UUID.fromString(uuidService));
            if (linkLossService == null) {
                return null;
            }
            alertLevel = linkLossService.getCharacteristic(UUID.fromString(uuidChar));
        }
        return alertLevel;
    }
 private boolean sendMsg(byte[] value) {
        BluetoothGattCharacteristic characteristic = BletoothManager.getInstance().getSupportedCharacteristic(BletoothManager.UUID_SERVICE, BletoothManager.UUID_WRITE);
        if (characteristic == null) {
            Log.e(TAG, "write characteristic cannot find");
            return false;
        }
        mGatt.setCharacteristicNotification(characteristic, true);
        characteristic.setValue(value);
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        boolean result = mGatt.writeCharacteristic(characteristic);
        return result;
    }

注意点是:setWriteType模式不同会对发送有很大影响。

猜你喜欢

转载自blog.csdn.net/qq_28714343/article/details/54862701