inovait / neatle

A Neat BluetoothLE library for Android
MIT License
44 stars 13 forks source link

IllegalStateException during connection #41

Open RoProducts opened 6 years ago

RoProducts commented 6 years ago

Hi,

I'm using neatle to connect one or more BLE cycling sensors in an Android app. These are long lasting connections and I'm setting the keepAlive flag in a ConnectionMonitor. Occasionally, after some time I'm getting the following exception during the connection :

java.lang.IllegalStateException at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) at java.lang.reflect.Method.invoke(Method.java:372) at java.lang.reflect.Method.invoke(Native Method) at android.app.ActivityThread.main(ActivityThread.java:5254) at android.os.Looper.loop(Looper.java:135) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Handler.handleCallback(Handler.java:739) at si.inova.neatle.Device$6.run(Device.java:480) at si.inova.neatle.operation.CharacteristicSubscriptionImpl$1.onConnectionStateChanged(CharacteristicSubscriptionImpl.java:56) at si.inova.neatle.operation.CharacteristicSubscriptionImpl.access$000(CharacteristicSubscriptionImpl.java:39) at si.inova.neatle.operation.CharacteristicSubscriptionImpl.subscribeOnDevice(CharacteristicSubscriptionImpl.java:130) at si.inova.neatle.operation.OperationImpl.execute(OperationImpl.java:97) at si.inova.neatle.Device.execute(Device.java:220) at si.inova.neatle.Device.connect(Device.java:388)

I'm doing the following to connect a device (here for a csc sensor) :

  final BluetoothDevice device = Neatle.getDevice(macAddress));
  final CharacteristicSubscription cscSubscription = Neatle.createSubscription(context, device, Bluetooth.CSC_SERVICE, Bluetooth.CSC_MEASUREMENT_CHARACTERISTIC);
  cscSubscription.setOnCharacteristicsChangedListener(sensorDataHandler.getBLEListener());
  cscSubscription.start();

  final ConnectionMonitor monitor = Neatle.createConnectionMonitor(context, btDevice);
        monitor.setOnConnectionStateListener(new ConnectionStateListener() {
            @Override
            public void onConnectionStateChanged(Connection connection, int newState) {

                switch (newState){
                    case BluetoothAdapter.STATE_DISCONNECTED:
                        log("sensor "+ sensorName + " disconnected");
                        break;
                    case BluetoothAdapter.STATE_CONNECTING:
                        log("sensor "+ sensorName + " connecting");
                        break;
                    case BluetoothAdapter.STATE_CONNECTED:
                        log("sensor "+ sensorName + " connected");
                        break;
                    case BluetoothAdapter.STATE_DISCONNECTING:
                        log("sensor "+ sensorName + " disconnecting");
                        break;
                }
            }
        });
   monitor.setKeepAlive(true);
   monitor.start();`

What am I doing wrong here ? Can you help me ?

Thank you

Robert

tomazs commented 6 years ago

Is your application running in the background?

RoProducts commented 6 years ago

This is done in an Android service, which runs on the UI thread.

RoProducts commented 6 years ago

I changed the order of the api calls. I do now connect first and subscribe then. The crash did not reappear this way.