Skip to content

Discovery

Peter Foot edited this page Apr 14, 2018 · 3 revisions

Discovery

To discover and select the peer device at runtime, one can either discover the devices and select one in code, or display the UI device selection dialog. One would use code like the following, respectively.

Dim cli As New BluetoothClient
Dim peers() As BluetoothDeviceInfo = cli.DiscoverDevices()
Dim device As BluetoothDeviceInfo = ... select one of peer()...
Dim addr As BluetoothAddress = device.DeviceAddress
...
var cli = new BluetoothClient();
BluetoothDeviceInfo[]() peers = cli.DiscoverDevices();
BluetoothDeviceInfo device = null;//= ... select one of peer()...
BluetoothAddress addr = device.DeviceAddress;
...
``` and

```vbnet
Dim dlg As New SelectBluetoothDeviceDialog
Dim result As DialogResult = dlg.ShowDialog(Me)
If result <> DialogResult.OK Then
  Return
End If
Dim device As BluetoothDeviceInfo = dlg.SelectedDevice
Dim addr As BluetoothAddress = device.DeviceAddress
...
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK) {
   return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
...

Finally, the discovered devices can be added to a Windows Form’s control with Data Binding, using code like the following, which uses a drop-down list box.

Dim cli As New BluetoothClient
Dim peers() As BluetoothDeviceInfo = cli.DiscoverDevices()
Me.ListBox1.DisplayMember = "DeviceName"
Me.ListBox1.ValueMember = Nothing
Me.ListBox1.DataSource = peers
var cli = new BluetoothClient();
BluetoothDeviceInfo[]() peers = cli.DiscoverDevices();
this.listBox1.DisplayMember = "DeviceName";
this.listBox1.ValueMember = null;
this.listBox1.DataSource = peers;

A set of flags can be passed to DiscoverDevices to select which devices will be returned: remembered, authenticated, unknown, and discoverableOnly. When ‘discoverable only’ (new in v2.4) is set, only devices in range and in discoverable mode will be returned. When ‘unknown’ alone is set the same discoverable devices are found but any devices in the remembered set will be removed from the list.

On desktop Windows with the Microsoft stack discoverableOnly does work on Windows 7 from library version 3.0RC. There is no direct API to get the discoverable devices only, the remembered and discoverable devices are returned merged into one list so we have to use some platform events to filter the list. This does not work on Windows XP and thus discoverableOnly does not work there and it is undefined whether it will return all or zero devices. See http://32feetnetdev.wordpress.com/2010/11/15/device-discovery-improvements-on-msftwin32/ for more information on this.

Finally the maxDevices parameter allows the discovery process to complete early if and when the specified number of devices in range have been found. It does not place a strict upper limit on the number of devices returned — particularly when remembered devices are to be included. It is not supported on all platforms.

There is also property BluetoothClient.InquiryLength which controls how long one run will last.

var cli = new BluetoothClient();
int maxDevices = 255;
bool authenticated = false;
bool remembered = true;
bool unknown = false;
bool discoverableOnly = false;
BluetoothDeviceInfo[]() list = cli.DiscoverDevices(maxDevices,
    authenticated, remembered, unknown, discoverableOnly);
...

To remove devices from the list of remembered/authenticated devices use BluetoothSecurity.RemoveDevice, see the Bluetooth Security section.

Asynchronous Device Discovery

Clone this wiki locally