Languages

Menu
Sites
Language
verifying Bluetooth connectivity

 

In my wearable web app, I'd like to verify that bluetooth connection to my phone is active/valid.

 

How can this  be done using java script?

 

Thanks!

Responses

3 Replies
Iqbal Hossain
hi~ Just check the bluetooth power...
The current state of the Bluetooth adapter.

This attribute holds one of the following 2 values:

  • true - If Bluetooth adapter is currently on
  • false - If Bluetooth adapter is currently off

Since: 2.3.1

Code example:

 var adapter = tizen.bluetooth.getDefaultAdapter();
 console.log("Bluetooth state: " + (adapter.powered ? "On" : "Off"));
Rich Mabb

Iqbal,

 

What if BT on watch in on, but on phone is off?

What if BT on watch in on, but phone is out of range?

My wearable app still can't retrieve data from web, I need a way to chek for this so I can handle or provide an alert!

Iqbal Hossain

Use 

getKnownDevices
Gets all the known devices that have information stored in the local Bluetooth adapter.
var adapter = tizen.bluetooth.getDefaultAdapter();

 function onGotDevices(devices) {
    console.log("Devices");
    for (var i = 0; i < devices.length; i++) {
        console.log(" Name: " + devices[i].name + ", Address: " + devices[i].address);
    }
    console.log("Total: " + devices.length);
 }

 function onError(e) {
   console.log ("Error: " + e.message);
 }

 function onBluetoothsetPowered() {
    adapter.getKnownDevices(onGotDevices, onError);
 }

 // Turns on Bluetooth
 adapter.setPowered(true, onBluetoothsetPowered);

getDevice

Gets the BluetoothDevice object for a given device hardware address.
function gotDeviceInfo(device) {
    console.log("Device Name: " + device.name);
    console.log("Device Address: " + device.address);
    console.log("Device Class: " + device.deviceClass.major);
    console.log("Is Bonded: " + (device.isBonded ? "Yes" : "No"));
 }

 function onError(e) {
    console.log ("Could not get device info:" + e.message);
 }

 var adapter = tizen.bluetooth.getDefaultAdapter();
 adapter.getDevice("35:F4:59:D1:7A:03", gotDeviceInfo, onError);

Ref: https://developer.tizen.org/development/api-references/web-application?redirect=/dev-guide/2.4/org.tizen.web.apireference/html/device_api/mobile/tizen/bluetooth.html#BluetoothAdapter::getKnownDevices