0

Bluetooth device is: Blue Maestro Tempo disk sensor. (Sensor 23, temperature + humidity + dew point sensor)

This is the link for the vendor SDK which has the bug I am trying to fix.

I connect to the bluetooth device using this method:

/**
     * Connects to the GATT server hosted on the Bluetooth LE device.
     *
     * @param address The device address of the destination device.
     *
     * @return Return true if the connection is initiated successfully. The connection result
     *         is reported asynchronously through the
     *         {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
     *         callback.
     */


      public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }

        // Previously connected device.  Try to reconnect.
        if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {
            Log.e(TAG, "Trying to use an existing mBluetoothGatt for connection.");
            if (mBluetoothGatt.connect()) {
                mConnectionState = STATE_CONNECTING;
                return true;
            } else {
                return false;
            }
        }


        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.e(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        // We want to directly connect to the device, so we are setting the autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
        Log.e(TAG, "Trying to create a new connection.");
        mBluetoothDeviceAddress = address;
        mConnectionState = STATE_CONNECTING;
        return true;
    }

Here's my BluetoothGattCallback:

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        Log.e(TAG, "In onConnectionStateChange and there has been a change of :" +newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.e(TAG, "Connected to GATT server.");

            // Attempts to discover services after successful connection.
            Log.e(TAG, "Attempting to start service discovery:");
            try {
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Boolean discover = mBluetoothGatt.discoverServices();
            if (discover == true) {
                //Raise flag
                unstableBluetoothBehaviour = true;
                Log.e(TAG, "Service discovery returns true and is started");
            } else {
                Log.e(TAG, "Service discovery returns false and has stopped unexpectedly");
            }

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            if (unstableBluetoothBehaviour == true) {

                Log.e(TAG, "Unstable behaviour detected");

            }
            Log.e(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
            close();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.e(TAG, "In onServicesDiscovered");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.e(TAG, "mBluetoothGatt = " + mBluetoothGatt);

            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.e(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }


    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        Log.e(TAG, "Characteristic has been updated = " + characteristic);
    }


};

The problem is that when I call Connect(), the onConnectionChange() is called with a STATE_CONNECTED. Then it is called again with a STATE_DISCONNECTED!


Here's a print from Logcat it might help you figure out the problem:

11-01 12:42:29.082 17969-17969/com.bluemaestro.tempo_utility D/ViewRootImpl: #3 mView = null
11-01 12:42:29.172 17969-17969/com.bluemaestro.tempo_utility D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 24 - 0, 0) vi=Rect(0, 24 - 0, 0) or=1
11-01 12:42:29.242 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: onServiceConnected mService= com.bluemaestro.tempo_utility.delivery_monitor.MyUARTService@dd6c856
11-01 12:42:29.242 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: connect() - device: ED:90:ED:F9:4C:60, auto: false
    registerApp()
    registerApp() - UUID=b3664342-0b17-427b-b32b-048726bc32de
11-01 12:42:29.282 17969-17987/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
11-01 12:42:29.292 17969-17969/com.bluemaestro.tempo_utility E/MyUARTService: Trying to create a new connection.
11-01 12:42:29.322 17969-17969/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@574a141 time:88055063
    Timeline: Activity_idle id: android.os.BinderProxy@c3710fb time:88055065
11-01 12:42:29.512 17969-17969/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{2870b81 token=android.os.BinderProxy@c3710fb {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.ConnectSensorActivity}} show : false
11-01 12:42:30.482 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 12:42:30.492 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :2
    Connected to GATT server.
    Attempting to start service discovery:
11-01 12:42:30.502 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: UART_CONNECT_MSG
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: discoverServices() - device: ED:90:ED:F9:4C:60
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: Service discovery returns true and is started
11-01 12:42:32.502 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=6 status=0
    onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=39 status=0
11-01 12:42:32.512 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onSearchComplete() = Device=ED:90:ED:F9:4C:60 Status=0
11-01 12:42:32.512 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onServicesDiscovered
    mBluetoothGatt = android.bluetooth.BluetoothGatt@e01b0d7
11-01 12:42:32.512 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: ENABLETXNOTIFICATION
11-01 12:42:36.092 17969-17980/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnParamsChanged() - Device=ED:90:ED:F9:4C:60 interval=159 status=0
11-01 12:42:56.842 17969-17969/com.bluemaestro.tempo_utility D/BluetoothAdapter: stopLeScan()
    STATE_ON
    STATE_ON
    scan not started yet
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=19 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :0
    Unstable behaviour detected
    Disconnected from GATT server.
    mBluetoothGatt closed
11-01 12:43:00.762 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility E/BlueMaestro: UART_DISCONNECT_MSG
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility E/MyUARTService: mBluetoothGatt closed
11-01 12:43:00.762 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 12:43:00.772 17969-17981/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 12:43:00.772 17969-17969/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 12:44:28.422 17969-17969/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{7e3fdc4 token=android.os.BinderProxy@574a141 {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.DashboardActivity}} show : true
11-01 12:49:51.792 17969-17969/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@574a141 time:88497537

Another log cat I get without changing anycode:

11-01 14:07:25.892 21448-21448/com.bluemaestro.tempo_utility E/MyUARTService: Trying to create a new connection.
11-01 14:07:25.892 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
11-01 14:07:25.902 21448-21448/com.bluemaestro.tempo_utility I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3cd6867 time:93151645
    Timeline: Activity_idle id: android.os.BinderProxy@a2ac28a time:93151645
11-01 14:07:26.112 21448-21448/com.bluemaestro.tempo_utility V/ActivityThread: updateVisibility : ActivityRecord{258b2e1 token=android.os.BinderProxy@a2ac28a {com.bluemaestro.tempo_utility/com.bluemaestro.tempo_utility.delivery_monitor.ConnectSensorActivity}} show : false
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=ED:90:ED:F9:4C:60
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility E/MyUARTService: In onConnectionStateChange and there has been a change of :0
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility E/MyUARTService: Disconnected from GATT server.
    mBluetoothGatt closed
11-01 14:07:30.902 21448-21462/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
    unregisterApp() - mClientIf=7
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility E/BlueMaestro: UART_DISCONNECT_MSG
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility E/MyUARTService: mBluetoothGatt closed
11-01 14:07:30.902 21448-21448/com.bluemaestro.tempo_utility D/BluetoothGatt: close()
11-01 14:07:30.912 21448-21448/com.bluemaestro.tempo_utility D/BluetoothGatt: unregisterApp() - mClientIf=7
11-01 14:07:50.572 21448-21448/com.bluemaestro.tempo_utility D/BluetoothAdapter: stopLeScan()
    STATE_ON
    STATE_ON
    scan not started yet
Mena
  • 3,019
  • 1
  • 25
  • 54
  • you probably should `stopLeScan` before connecting to the device – Vladyslav Matviienko Nov 01 '18 at 10:57
  • @VladyslavMatviienko I made sure that i stopLeScan before calling the Connect method. – Mena Nov 01 '18 at 11:09
  • according to log, `stopLeScan()` is called way later that connect – Vladyslav Matviienko Nov 01 '18 at 11:23
  • @VladyslavMatviienko probably another call for stopLeScan() which is already not scanning. I made sure by calling stopLeScan() before connect() after you told me to. – Mena Nov 01 '18 at 11:41
  • that in theory could be the problem, try to avoid calling stopScan if it is not scanning already. – Vladyslav Matviienko Nov 01 '18 at 11:46
  • @VladyslavMatviienko Ok, I tried to find the source of that stopLeScan, and it is called right after receiving the onClientConnParamsChanged() and by the BluetoothAdapter class. So, not from my code. Check logcat and tell me if you get what i am trying to tell u. – Mena Nov 01 '18 at 12:02
  • yes I get what you mean. You say that it is not you who is stopping BT scan, but the system does itself, and it may be that way. Then probably the device you are connecting to does not like what you do with the enabling notifications, and disconnects because of it – Vladyslav Matviienko Nov 01 '18 at 12:04
  • @VladyslavMatviienko I commented out the code of ENABLENOTIFICATION. But still.... btw. I sometimes get another Logcat without changing anycode. The disconnection happens before checking for the services. I posted an Update above. – Mena Nov 01 '18 at 12:14
  • Its not necessary that you should call stopLeScan, it's recommended though anyways it just depends on your application logic. I work with multiple drives send receive commands and keep scanning ble's all along the way. – Dushyant Suthar Dec 27 '18 at 11:00
  • @Mena DId you get the solution, I am also facing the same problem. – Shubham Anand Feb 11 '20 at 10:06

1 Answers1

0

1- Make sure you are connecting to the correct device. Bluetooth scanner will scan for many devices and may find a device which can not be connected to (in the same way we connect to BLE device)

2-Make sure you dont call bluetoothLeScanner.stopScan when you are trying to connect, before or after. Instead call in after you successfully get the connection status 2 (connected)

MSaudi
  • 4,442
  • 2
  • 40
  • 65
  • If you mean stop scanning after connecting, I don't think it is important step. But if you decided to call stopScan for any reason, then advice in point 2 is useful. – MSaudi Oct 14 '19 at 18:47