Minimal iOS BluetoothManager Example

I am creating a minimal example to detect nearby Bluetooth devices using the private BluetoothManager infrastructure in iOS 5.0.

Using the answer found in this question: Search for common Bluetooth devices within reach

Here is my viewDidLoad method for registering for BluetoothAvailabilityChangedNotification. I also register for BluetoothDeviceDiscoveredNotification.

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(bluetoothAvailabilityChanged:)
    name:@"BluetoothAvailabilityChangedNotification"
    object:nil];

btCont = [BluetoothManager sharedInstance];

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(deviceDiscovered:)
    name:@"BluetoothDeviceDiscoveredNotification"
    object:nil];

When I receive a notification about a change in the availability of Bluetooth, I set the device to scan on as described in one of the answers in the above link.

- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
    NSLog(@"BT State: %d", [btCont enabled]);
    [btCont setDeviceScanningEnabled:YES];
}

For completeness, here is the method of the discovered device.

- (void)deviceDiscovered:(NSNotification *) notification
{
    NSLog(@"Discovered one!");
}

The logs created when the test application was launched look like this:

BTM: attaching to BTServer
BTM: posting notification BluetoothAvailabilityChangedNotification
BT State: 1
BTM: setting device scanning enabled

, - Bluetooth, , ( Android).

, :

  • [btCont setPowered: YES]; , setDeviceScanningEnabled:
  • [btCont resetDeviceScanning] setDeviceScanningEnabled
  • scanForConnectableDevices: (unsigned int) arg1; , , arg1 -. .

. !

+5
2

, bluetooth , .   , .

scanForServices:

    // start scan
    [btManager  setDeviceScanningEnabled:YES];
    [btManager scanForServices:0xFFFFFFFF];
+2
0

All Articles