Samsung Electronics logo

Discovering Bluetooth Devices



Learning how to search for remote devices and get the known devices is a basic Bluetooth management skill:

  1. Retrieve a BluetoothAdapter object with the getDefaultAdapter() method:

    var adapter = webapis.bluetooth.getDefaultAdapter();
    
  2. To search for remote devices, use the discoverDevices() method.

    The results of the search are returned in the BluetoothDiscoverDevicesSuccessCallback event handler.

    var discoverDevicesSuccessCallback = 
    {
       /* When a device is found */
       ondevicefound: function(device)
       {
          console.log("Found device - name: " + device.name);
       }
    }
    
    /* Discover devices */
    adapter.discoverDevices(discoverDevicesSuccessCallback, null);
    
    Note
    To allow other Bluetooth devices to find your device, you must set the device to be visible through the system settings.
  3. To retrieve known devices (which have been previously paired or searched for), use the getKnownDevices() method.

    The results of the search are returned in the BluetoothDeviceArraySuccessCallback event handler.

    /* When a known device is found */
    function onGotDevices(devices) 
    {
       var message = new String("Known devices: " + devices.length);
       console.log(message);
    }
    
    /* Retrieve known devices */
    adapter.getKnownDevices(onGotDevices, onError);