Skip to content
Guido Marucci Blas edited this page Dec 22, 2014 · 2 revisions

The discovery process is handled by an object that conforms to the WLXDiscoverer protocol. You can get this object by accessing the discoverer property of the WLXBluetoothDeviceManager. This property will return an object that implements the WLXDiscoverer protocol.

The WLXDiscoverer protocol exposes a simple API to discover devices. The only thing you should do to start discovering is invoke the discoverDevicesNamed:withServices:andTimeout: method. You can filter devices by their name, the services they expose or both. A discovery timeout (in milliseconds) is mandatory and must be a positive number. After the timeout expires the discovery process will be stopped. If you want to filter devices by the services they expose you should provide an NSArray of CBUUID objects with the required services UUID.

You can also configure some properties of the discovery process by setting the scanOptions property of the WLXDiscoverer object. This property expect an NSDictionary with some specific values. The default value is @{CBCentralManagerScanOptionAllowDuplicatesKey: @NO};

Every time a new devices gets discovered the deviceDiscoverer:discoveredDevice: method of the WLXDeviceDiscovererDelegate. You should implement this protocol and set the delegate using the delegate property of the WLXDiscoverer object. When a device is discovered a WLXDiscoveryData object is provided. This object contains information about the device and its discovery data, for example it contains the CBPeripheral object.

The WLXDeviceDiscovererDelegate will also be notified every time the discovery process is started or stopped.

To stop the discovery process you can invoke the stopDiscoveringDevices method.

Notifications

The WLXDiscoverer also publishes discovery related notifications using NSNotificationCenter. The following is a list of the notifications:

  • WLXBluetoothDeviceStartDiscovering: When the discovery process is started.
  • WLXBluetoothDeviceStoptDiscovering: Every time a new devices is discovered. You can get the WLXDiscoveryData object from the user info dictionary of the NSNotification object using the key WLXBluetoothDeviceDiscoveryData.
  • WLXBluetoothDeviceDeviceDiscovered: When the discovery process is stopped.

Examples

To discover all the devices that expose a service with UUID 68753A44-4D6F-1226-9C60-0050E4C00066 with a timeout of 30 seconds, you should call the discover method with the following parameters:

WLXBluetoothDeviceManager * deviceManager = [WLXBluetoothDeviceManager deviceManager];
id<WLXDiscoverer> discoverer = deviceManager.discoverer;
discoverer.delegate = self;
CBUUID * serviceUUID = [CBUUID UUIDWithString:@"68753A44-4D6F-1226-9C60-0050E4C00066"];
[discoverer discoverDevicesNamed:nil withServices:@[serviceUUID] andTimeout:30000];

If you want to list only the devices whose name starts with Tracker, you can pass a regular expression to the name parameter:

[discoverer discoverDevicesNamed:@"Tracker.*" withServices:nil andTimeout:30000];

This will match device with the name Tracker, Tracker1, Tracker A, etc. You can also combine the name filter with the service filter.

Here is an example of UITableViewController that lists all the discovered devices in a UITableView.

@import CoreBluetooth;

@interface WLXDiscoverViewController ()<WLXDeviceDiscovererDelegate>

@property (nonatomic) id<WLXDeviceDiscoverer> discoverer;

@end

static NSUInteger DISCOVERY_TIMEOUT = 30000; //ms

@implementation WLXDiscoverViewController

- (void)viewDidLoad {
  [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
  self.discoverer.delegate = self;
  [self discover];
}

- (void)viewWillDisappear:(BOOL)animated {
  [self.discoverer stopDiscoveringDevices];
}

#pragma mark - WLXDeviceDiscovererDelegate methods

- (void)deviceDiscoverer:(id<WLXDeviceDiscoverer>)discoverer startDiscoveringDevicesWithTimeout:(NSUInteger)timeout {

}

- (void)deviceDiscoverer:(id<WLXDeviceDiscoverer>)discoverer discoveredDevice:(WLXDeviceDiscoveryData *)discoveryData {
  [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

- (void)deviceDiscovererStopDiscoveringDevices:(id<WLXDeviceDiscoverer>)discoverer {

}

#pragma mark - UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  CBPeripheral * peripheral = [self peripheralForIndexPath:indexPath];
  [self.delegate discoverViewController:self didSelectPeripheral:peripheral];
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [self.discoverer.discoveredDevices count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
  CBPeripheral * peripheral = [self peripheralForIndexPath:indexPath];
  cell.textLabel.text = peripheral.name;
  cell.detailTextLabel.text = peripheral.identifier.UUIDString;
  return cell;
}

#pragma mark - Private methods

- (void)discover {
  self.title = @"Discovering ...";
  [self.discoverer discoverDevicesNamed:nil withServices:nil andTimeout:DISCOVERY_TIMEOUT];
}

- (CBPeripheral *)peripheralForIndexPath:(NSIndexPath *)indexPath {
  WLXDeviceDiscoveryData * discoveryData = self.discoverer.discoveredDevices[indexPath.row];
  return discoveryData.peripheral;
}

@end
Clone this wiki locally