简体   繁体   中英

Bluetooth LE Sending String from phone to bluetooth le device

I am trying to send a string consisting of " 1234567 " but unfortunately the only way I can send it is to put multiple delays for every ASCII character, is there any way I can make the code more efficient?

This is the part where I send my string over:

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);
}

And this is my writeCustomCharacteristic function:

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");
    }
}

Just redefine your writeCustomCharacteristic method:

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");
  }
}

and call this function with:

writeCustomCharacteristic("1234567".getBytes());

By the way: The waiting approach is not the best way for BLE communication. Because you don't know exactly how long the transmission takes. And you don't get the feedback that your transmission was really successful. It's better to wait until the onCharacteristicWrite callback in your BluetoothGattCallback is called by the BLE stack. For more information about Android BLE and it's pitfalls, I would recommend this video .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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