writeCharacteristic总是返回false

时间:2016-06-09 10:57:38

标签: java android bluetooth

我想从android向蓝牙设备发送数据,然后点击按钮获得响应,但总是返回false。请有人帮助我。

////在DeviceControlActivity.java

public void onGetButtonStatus(View v) {
    if(mBluetoothLeService != null) {
        mBluetoothLeService.writeData("btn1?".getBytes());

    }

}

////在BluetoothLeService.java

public void writeData(byte[] value) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"));
    if(mCustomService == null){
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }

    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"));
    mWriteCharacteristic.setValue(value);

    if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
        Log.w(TAG, "Failed to write characteristic");
    }
}

0 个答案:

没有答案