Languages

Menu
Sites
Language
How to connect over Bluetooth Low Energy (BLE)

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);}
})

 

Edited by: Alan Buch on 08 Jun, 2018

Responses

3 Replies
Armaan-Ul- Islam

"is there anything in this code that prevents it from scanning my own device. "

-> Code Seems pretty straight forward, I don't see any problem with it. There's no filter for searching devices. Initiate Scan from both Standalone mode and Companion mode (Connected with your phone via 'Samung Gear'). If you can afford, test a BLE scan from any other different device (any device, not necessary to be a tizen gear ) and check if your J7 device is listed or not.

 

Make sure 'Visible to All nearby devices' is checked in Settings of your smartphone.

 

About Inconsistency in scan, I guess it needs to be more stable.

Alan Buch

Great advice, thanks!!  I downloaded BLE Beacan for my Gear S2.  Same result...it finds other devices while I'm at my work office.  But it doesn't find my J7, or my Galaxy S3, or my iPhone 5s - so i guess none of those devices are beacons.

Armaan-Ul- Islam

You're Welcome.