This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

discoveringServices problem Android app, delay 600ms

Hello.

After connection establishing i would like to doscover services.

I read here:

https://stackoverflow.com/questions/41434555/onservicesdiscovered-never-called-while-connecting-to-gatt-server

that need to add 600ms delay before "gatt.discoverServices();"

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            Log.d("Connection", newState+"");
            Toast.makeText(context, newState+"", Toast.LENGTH_SHORT).show();
            
            //Here is the place for 600ms delay
            
             try {
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            gatt.discoverServices();
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            List <BluetoothGattService> services = gatt.getServices();

            Log.d("services","Services ");
            //gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            //Log.d("czy_polaczono", "Charakterystyki: " +characteristic.toString());

        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
           // Log.d("czy_polaczono", "Charakterystyki: " +characteristic.toString());
        }
    };

I tried with and without delay but it did not work. Does anybody have a knowledge how to fix this?

Parents Reply Children
No Data
Related