Widget's first run gives Bluetooth timeout (-2)

Hi all,

I'm getting my widget ready for upload to the app store and discovered a weird bug which occurs when I first install the widget (both as beta or by manually copying .PRG). My widget is simple, it does a web request to one of my own servers (as explained here in the example: developer.garmin.com/.../Communications.html) and then draws data on the screen. The weird thing is that the first time the widget runs, it times out and gives error code -2. This doesn't happen on the Simulator even if I delete the temp files.

If I turn off and then turn on bluetooth on my phone, it works right away. If I don't do this, it keeps failing and giving -2. Anyone have any idea what's going on here? It seems similar to this thread https://forums.garmin.com/developer/connect-iq/f/discussion/211119/ble-host-timeout but unfortunately this didn't get me any further to solving it. 

Below are some relevant code snippets once inside the class. I'm compiling for Fenix 5S in this case (that's the only physical watch I have), using SDK 3.1.6, manifest uses SDK => 1.2.X and Eclipse IDE 2019-06 (4.12.0). 

Thanks in advance for anyone who can help!

Niall

App.mc 

    function initialize() {
        AppBase.initialize();
		var savedSpot = Storage.getValue("savedSpot");
    	if (savedSpot == null) {
    		SpotNr = "22";
    	} else {
    		SpotNr = savedSpot;
    	}
    }

...

    function onStop(state) {
    	Storage.setValue("savedSpot", SpotNr); 
    }

View.mc

    function initialize() {
		firstRunBool = 1;
		View.initialize();
	}

...

    function onUpdate(dc) {
		if (firstRunBool == 1){
			makeRequest();
		}

		View.onUpdate(dc);
...

	function makeRequest() {
		firstRunBool = 0;
		var url = "https://WEBSITE_HIDDEN";
		var params = {
		"definedParams" => "null",
			"spot" => SpotNr
		};
		
		var options = {                                            
			:method => Comm.HTTP_REQUEST_METHOD_GET,
			:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
		};
		
		var responseCallback = method(:onReceive);                  
		Comm.makeWebRequest(url, params, options, method(:onReceive));
	}

...

function onReceive(responseCode, data) {
    
    if ((responseCode == 200) && (data != null)){
        // do a whole bunch of stuff to update view based on data. Code removed. 

		} else {
			timestampDisplay.setText(getResponseMsg(responseCode));
			spotDisplay.setText("Error");
	}
    
    WatchUi.requestUpdate();
    
}

  • The sim uses a different interface than is used on the device.  On a device, your phone is used as a proxy, and on top of that, things can work differently if the phone is Android or iOS.

    the -2 means your phone got the request and passed it on, but nothing was heard back before the timeout occurred.  That can be something with the server, or between the phone and the garmin device.  I see the -2 when I'm right at the edge of BT range from my Garmin device.

    If you're within range, I'd look at the server and see if it got the request and responded

  • Thanks Jim. I indeed followed up on what -2 could mean but the watch and phone are less than a meter away and once bluetooth has restarted, same distance too. I also tried keeping the screen on for the first run but that also didn't help. The server is alive and kicking, it's a simple json response. I'll try creating a file on my server if a request has been made to see if it's coming in. I welcome any other suggestions or people with similar issues. 

  • Can you try connecting to your server directly from the phone when it's in this state and see how long it takes?

    What type of phone?  Does GCM show you are connected to the Garmin when this happens?

  • Tried that, it takes less than a second. Sony XZ2 Compact running Android 10. And yes, both Connect and Connect IQ show green blob and connected.