F1sher
F1sher

Reputation: 7310

Bluetooth LE connection time/caching

I want to connect to bluetooth device and write value to characteristic the fastest way it's possible. For now my code gives bad results:

1) BLE scan - as you can see I have hardcoded MAC adress of device I want to connect to. I also get timestamp of connectGatt method invoke.

    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

        if(device.getAddress().equals("F1:9F:EE:6B:AB:83")) {
            mConnectedGatt = device.connectGatt(this, false, mGattCallback);
            start = System.currentTimeMillis()/1000;
        };
    }

2) onConnectionChanged - I get enother timestamp and substract them to get to know how much connecting operation lasted

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        end = System.currentTimeMillis()/1000;
        Log.d("QWERTY", "TIME " + (end - start));

        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {

            Log.d("QWERTY","Success");
            gatt.discoverServices();

        } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {

            Log.d("QWERTY","Disconnected");

        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            Log.d("QWERTY","Connection error");
            gatt.disconnect();
        }
    }

And there is my main problem. Time between device.connectGatt and onConnectionStateChange takes from 3 - 8 seconds which is far too long for me. I'd be happy if it was around 1 sec. Write characteristic takes less than second in onServicesDiscovered listener so it's not a problem.

Question 1): Is there any way to make this connection time faster?

Question 2): Is there any way to store bluetooth connection with device in cache, so it will last long at first connection and during later invokes would be faster?

Question 3): Any other suggestions what could make it work faster? Maybe my approach is bad.

Upvotes: 2

Views: 2626

Answers (1)

huberdth numsenprut
huberdth numsenprut

Reputation: 66

1) yes your peripheral must give better connection intervalparameters on connect. After Services/Characteristics discovery you can always slow them down again to save battery on long lasting connection if you need to.

2) bonding might help for future connection.

3) if you have many services/characteristics then it take longer to discover them and generate the handle tables in the lower layer stack. With iOS you should send min-connection-interval of 20ms (32) and Max of 40ms (64) but you can hack it with 10ms-20ms and you will get around 18-19ms interval. On Android this might be too aggressive and can cause Samsung to refuse. So try [20;40ms]

Upvotes: 2

Related Questions