蓝牙LE发送从手机到蓝牙设备的字符串

时间:2018-01-24 08:38:29

标签: java android bluetooth-lowenergy

我正在尝试发送一个由" 1234567"组成的字符串。但不幸的是,我发送它的唯一方法是为每个ASCII字符添加多个延迟,有什么办法可以让代码更有效率吗?

这是我发送字符串的部分:

public void onServiceConnected(ComponentName componentName, IBinder service) {
    mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
    mBluetoothLeService.writeCustomCharacteristic(49);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(50);
        }
    },100);

    final Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(51);
        }
    },100);

    final Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(52);
        }
    },100);

    final Handler handler3 = new Handler();
    handler3.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(53);
        }
    },100);

    final Handler handler4 = new Handler();
    handler4.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(54);
        }
    },100);

    final Handler handler5 = new Handler();
    handler5.postDelayed(new Runnable(){
        @Override
        public void run() {
            mBluetoothLeService.writeCustomCharacteristic(55);
        }
    },100);
}

这是我的writeCustomCharacteristic功能:

public void writeCustomCharacteristic(int 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;
    }
    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"));
    mWriteCharacteristic.setValue(value, BluetoothGattCharacteristic.FORMAT_SINT8,0);
    if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
        Log.w(TAG, "Failed to write characteristic");
    }
}

1 个答案:

答案 0 :(得分:0)

重新定义您的writeCustomCharacteristic方法:

public void writeCustomCharacteristic(byte[] payload) {
  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;
  }
  /*get the read characteristic from the service*/
  BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e"));
  mWriteCharacteristic.setValue(payload);
  if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
    Log.w(TAG, "Failed to write characteristic");
  }
}

并使用以下命令调用此函数:

writeCustomCharacteristic("1234567".getBytes());

顺便说一句:等待方法不是BLE通信的最佳方式。因为您不确切知道传输需要多长时间。而且你没有得到你的传输真正成功的反馈。最好等到BLE堆栈调用onCharacteristicWrite中的BluetoothGattCallback回调。有关Android BLE及其陷阱的更多信息,我会recommend this video

相关问题