I wrote a simple javascript widget to run on my Samsung Gear S2. Code below. It attempts to scan for BLE devices and then connect to them. It works as expected when I go to a location with many people. That is, in my logged messages, it shows the MAC addresses for devices... but none of them are for my Samsung Galaxy J7 (Android 7, Bluetooth 4.1). So the question is, is there anything in this code that prevents it from scanning my own device. Or, given that it DOES find other devices, can I assume that my problem is not with the code, but rather with my device?
The other thing I noticed is that when i start a scan, find several MAC addresses, stop the scan and then start the scan again, it no longer finds those same MAC Addresses. But if I reboot the watch and run the scan a 3rd time, it finds those same mac addresses. Why doesn't the second scan, before rebooting, find the devices a second time? and maybe this is related to my first issue, above(?)
Thanks for anyhelp! (My goal is to use a BLE connection between smartphone and smartwatch so that I can display the battery level of the phone on my watchface.)
(function() {
var batteryServiceUUID = "0000180F-0000-1000-8000-00805f9b34fb";
var batteryLevelUUID = "00002a19-0000-1000-8000-00805f9b34fb";
var bleDevices = [];
var bleAdapter;
window.Xonload = init;
function init() {
bindEvents();
}
function startScan()
{
try {
writeLog("Initializing.");
//BLE
bleAdapter = tizen.bluetooth.getLEAdapter();
bleAdapter.startScan(onBLEScanFoundCB, onBLEScanErrorCB);
writeLog("Scanning for BLE devices.");
}
catch (err)
{writeLog("startScan-ERROR:"+err);}
}
function stopScan()
{
try {
writeLog("Stopping...");
bleAdapter.stopScan();
writeLog("Scanning stopped.");
}
catch (err)
{writeLog("stopScan-ERROR:"+err);}
}
function onBLEScanFoundCB(bleDevice)
{
try {
if (bleDevices.indexOf(bleDevice.address)===-1)
{
bleDevices.push(bleDevice.address);
bleDevice.connect(onDeviceConnectSuccess,onDeviceConnectError);
writeLog("Found BLE Device: Address ["+bleDevice.address+"] Name ["+bleDevice.name+"]");
}
}
catch (err)
{writeLog("Error [onBLEFoundCB] ");}
}
function onBLEScanErrorCB()
{
writeLog("Error on Scan");
}
function onDeviceConnectSuccess(bleDevice)
{
try {
writeLog("Successfully connected BLE Device ["+bleDevice.address+"]");
}
catch (err)
{writeLog("Error [onDeviceConnectSuccess("+bleDevice.address+")] "+err);}
})