Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Write consecutive Characteristic fast and stable for BLE in Android?

I am developing BLE in Android , I can scan, connected and write characteristic to to the BLE device.

I call the following function to pass BluetoothGatt and characteristic to AsyncTask when click the Button.

write_btn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        new WriteCharacteristic(mBluetoothGatt , HueCharacteristic).execute();
     }
});

The code of write characteristic is like the following:

private class WriteCharacteristic extends AsyncTask<String, Void, String> {

        public BluetoothGatt mGatt;
        public BluetoothGattCharacteristic mCharacteristic;

        public WriteCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
            mGatt = gatt;
            mCharacteristic = characteristic;
        }

        @Override
        protected String doInBackground(String... urls) {
            mGatt.writeCharacteristic(mCharacteristic);
            return null;
        }
    }

But I try to click the button consecutive, it seems the Android did not write every characteristic to the BLE device.

If I click the button consecutive for 5 times , it will loss 1~3 times. It only write characteristic to BLE device for two times.

Question:

Is there any better way to write characteristic consecutive and stable to BLE device for Android?

like image 216
Wun Avatar asked Feb 13 '15 05:02

Wun


People also ask

Is BLE stable?

Detection of BLE device is not stable - most of the time device not found #521.

How many BLE devices can Android connect?

x protocol SPEC, when the iPhone/Android phone play as role of BLE central mode, the limitation to have active connection at the same time is up to 8 devices.

What is BLE setting in Android?

Android provides built-in platform support for Bluetooth Low Energy (BLE) in the central role and provides APIs that apps can use to discover devices, query for services, and transmit information. Common use cases include the following: Transferring small amounts of data between nearby devices.


1 Answers

The read/write characteristic system in Android's Bluetooth stack isn't good at queueing up multiple operations. You need to wait for operations to complete before sending along another one. In addition, since your code is using AsyncTask, you will get parallel execution of tasks on some devices, so even the requests aren't being serialized when you repeatedly hit the button.

To get stable results from the framework, you will need to queue up those requests yourself and wait for BluetoothGattCallback onCharacteristicWrite() to trigger before sending the next command. Your code needs to synchronize all access to the GATT object, so that the next writeCharacteristic() never comes until the completion callback fires for the previous request.

like image 177
devunwired Avatar answered Sep 19 '22 12:09

devunwired