I am new to java and android. I have the following function running in a service in the main UI thread.
public void startSession() {
    Log.d(TAG, "startSession()");
    sessionStartRequestCompleted = false;
    int c =0;
    do {
        Log.d(TAG, "startSession() - Tyring again #" + c);
        writeCharacteristic(
                characteristicMap.get(ShsServiceConstants.SH_GATT_SVC_SESSION_TOGGLER_CHARACTERISTIC_UUID),
                ShsServiceConstants.SESSION_TOGGLER_CHAR_SESSION_START_OPCODE);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
        int timeout = 0;
        sessionStartStatus = false;
        checkSessionStatus();
        while (!sessionStartStatus && timeout < 10) {
            timeout++;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (timeout >= 10) {
            Log.d(TAG, "StatusCheckTask Timed Out waiting for result");
            //fireNextShsGattRequest();
        }
        c++;
    } while (!sessionStartRequestCompleted);
}
But calling this function is blocking the UI thread. How can I write this function so that it doesn't block the UI thread but does the checking for the flags.
The flags are being set inside onCharacteristicRead.
 
     
     
     
     
     
     
    