繁体   English   中英

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

[英]Bluetooth LE Sending String from phone to bluetooth le device

我正在尝试发送由“ 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");
    }
}

只需重新定义您的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堆栈调用BluetoothGattCallbackonCharacteristicWrite回调。 有关Android BLE及其陷阱的更多信息,我推荐此视频

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM