【Datafield】How to know if a BLE device is disconnect

As we know the critical bug

onConnectedStateChanged can not being called 

https://forums.garmin.com/developer/connect-iq/f/discussion/365544/onconnectedstatechanged-not-being-called

I reproduced this issue with the demo code https://github.com/Likenttt/demo-ble

So I am wondering is there any trick to know when the connection is in available. It is an occasion to make an alert view.

  • See https://forums.garmin.com/developer/connect-iq/b/news-announcements/posts/would-you-like-some-raspberry-pi-with-your-connect-iq

    onConnectionStateChanged it called when a sensor pairs/connects or disconnects and which happened is indicated by the state passed

    In the github code you reference, it doesn't look like it's paying attention to state .  Look at the blog post I referenced, in the BLE delegate code and onConnectedStatechanged.

    If it connects, it sets up the app.  If it disconnects, resetConnection is called, and one of the things that does is to start scanning again.

    I've used this logic for years and it works. 

    To test it, it depends a bit on the sensor you are using.  Some you can just turn off, breaking the connection.  Some, you get far enough away from the device, which for some Garmin devices and be 20+ feet

  • Thanks for your reply.

    Even I checked state,the onConnectedStateChanged has never been invoked when disconnecting.

    I suspect the occasion is different between a datafield and widget. 

    In this thread 

    https://forums.garmin.com/developer/connect-iq/f/discussion/365544/onconnectedstatechanged-not-being-called, they are using a datafield too. Without a solution finally.

  • I put together a DF where I connect/pair with a BLE device.. It stays connected as long as the sensor is on and within range.

    If the sensor is lost, maybe not that very second but soon, the DF unpairs, starts scanning. and reconnects.  I do this based on the state in onConnectionStateChanged.  It works fine in the 7.3.1 sim as well as on a real device.  I've tested for hours with numerous disconnects/reconnects.  Here's the function:

            function onConnectedStateChanged( device, state ) {
                myDevice=device;
                if(state==Ble.CONNECTION_STATE_CONNECTED) {
                    myDevice=device;
    Sys.println("connected");          
                    var service = myDevice.getService(myService);                
                    doReadChar(service,charTemp);        
                } else {
                    connected=false;
                    Ble.unpairDevice(device);
                    Ble.setScanState(Ble.SCAN_STATE_SCANNING);
    Sys.println("disconnect - new scan");
                }
            }

    Looks like you may need to do the unPairDevice...

  • I don't think the key problem is unpairDevice. Even I added this line. 

    onConnectedStateChanged 

    has nerver been invoked after disconnected. Cuz I found message never be disconnected

    Note, 

    onConnectedStateChanged works fine when connected but not when disconnected. I feel really confused.