BLE scan data

Former Member
Former Member

I am a beginner with developing apps using the CIQ SDK and also new to developing with BLE but I am trying to create a simple app that will scan surrounding devices and output them to the LOGS txt file using System.print. I am currently not able to find any new scans. I have ensured that the scan state is scanning, but my onScanResults function is not being called. What could be a reason for why the onScanResults is not being called? My phone is right next to the watch and is discoverable and the watch I am using is the Garmin fenix 6, which is BLE enabled. I have inserted my code below, any help would be much appreciated!

using Toybox.WatchUi;
using Toybox.System;
using Toybox.BluetoothLowEnergy as Ble;

var resultOfScan = [];
var count = 0;

function changeText(inputVal) {
	action_string = inputVal;
	WatchUi.requestUpdate();
}

function printScan(arrayScan) {
	if (arrayScan.size() == 0){
		System.println("No scans");
	} else {
		for (var i = 0; i < arrayScan.size(); i++) {
			System.println(arrayScan[i]);
		}
	}
}

class NewProjectAppDelegate extends WatchUi.BehaviorDelegate {
	var test = null;
	function initialize() {
		BehaviorDelegate.initialize();
		test = new Handler();
		Ble.setDelegate(test);
	}
    
    function onKey(keyEvent) {
    	if (keyEvent.getKey() == 5) {
    		count++;
    		if (count > 1){
    			System.exit();
    		}
    		System.println("ESC triggered");
    		printScan(resultOfScan);
    		test.stopScan();
    	} else if (keyEvent.getKey() == 7) {
    		changeText("Attempting scan...");
        	System.println("Menu triggered");
    		test.startScan();
    		count = 0;
    	} else {
    		count = 0;
    	}
    	
        return true;
    }
}

class Handler extends Ble.BleDelegate {
    function initialize() {
        BleDelegate.initialize();
    }
    
    function onScanResults(scanResults) {
    	System.println("Scans!");
    	changeText("Scans found...");
    	for(var result = scanResults.next(); result != null; result = scanResults.next()){
    		resultOfScan.add(result);
    	}
    }
    
    function startScan() {
    	Ble.setScanState(Ble.SCAN_STATE_SCANNING);
    }
    
    function stopScan() {
    	Ble.setScanState(Ble.SCAN_STATE_OFF);
    }
    
    function onScanStateChange(scanState, status) {
    	System.println(scanState);
    }
}

  • Nothing jumps out at me as far as seeing scan results. Are you expecting to see your phone on the f6?  It could be that if the watch is already paired to it, you won't see it.

    But to make sure you can see things that are advertising, start with the NordicThingy52 sample.  Then, in the BLE delegate, there's a function called "contains" which is actually where scan results are tested for a Thingy.

    In that loop put your Sys.println() calls (ie, Sys.println("uuid="+uuid);)  Unless you have a Thingy52, it will just keep scanning after you start the scan. Once you see that works, you should be able to narrow down things in your own code.

    One thing I see is an issue is how you are saving scan results.  resultsOfScan will keep growing with duplicates allowed, and you'll get a whole lot of duplicates.  You only want to add to that array when it's a result you've not seen before.  The logic for this is in the thingy sample.

  • I just tried a scan and while I see other things, I don't see my phone.  (not something I've looked for before), but I do see scan results from other things just fine.  

  • I was able to write up an app that display scan results on my vivactive4 (I don't have a fenix6 at home). I found a few issues along the way, but it seems to work. Here is my code:

    EDIT: I'm unable to upload my code.. Disappointed I've posted it to pastebin here: https://pastebin.com/4rK24pUG

  • What issues did you find?  Do you mean in the first code posted? 

    Here's the  output I see from yours in the sim for a f5+

    The first 2 are raspberry pi systems, and thing18 is a thingy52.  No sign of a phone though when they are visible under BLE phone settings.

  • Former Member
    0 Former Member over 4 years ago in reply to jim_m_58

    Thank you for the advice! I was able to finally get it working and I have referenced the thingy sample for the logic to make sure that duplicates are not saved, I would not have caught that mistake.

  • Former Member
    0 Former Member over 4 years ago in reply to Travis.ConnectIQ

    Thank you for the sample app! It helped a lot. The main problem was that it was just taking a long time to find new devices so I thought it was not working. However, I was not able to find my phone and it seems as if jim_m_58 was not able to either. Do you know how I would be able to find devices like phones and laptops? The watch is not currently paired to my phone or any other device.

  • What issues did you find?  Do you mean in the first code posted? 

    The big one was can only look at some of the attributes of a ScanResult after BleDelegate::onScanResults() is called. There were others, but I don't recall what they were. The code posted above works around all the issues that I ran into.

    As for whether or not a phone is found, I'm not sure that they are supposed to or that they are broadcasting. I'm not really a Bluetooth ninja, I just wanted to prove to myself that the scan functionality was working. At the very least it appears to work for some devices... I've got a handful of entries but don't really know what they are.

  • What I think might be the case, is what on a phone or PC would be just sending out advertisements, and then advertisements for what?  On a phone, when you turn on BT, I have a feeling it's advertising with BT, and not BLE.

    In the case of the pi and Raspbian (now Pi OS), it needed code to advertise, so I wrote something using node.js.  While I've not looked into this at all, it could be that writing something for a phone or a pc to advertise would be needed.