Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal iOS BluetoothManager Example

I've been constructing a minimal example for detecting nearby Bluetooth devices using the BluetoothManager private framework in iOS 5.0.

Using an answer found in this question: Finding generic Bluetooth devices within reach

Here's my viewDidLoad method to register for the BluetoothAvailabilityChangedNotification. I also register for the BluetoothDeviceDiscoveredNotification as well.

[[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 get the Bluetooth availability changed notification, I set the device scanning enabled, as outlined in one of the answers in the aforementioned link.

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

For completeness, here's the deviceDiscovered notification method.

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

The logs produced by running the test application are as follows:

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

Unfortunately, the phone isn't picking up any Bluetooth devices at all, even though I know there are proximate discoverable devices (verified using an Android device).

Some things I have tried already:

  • Calling [btCont setPowered: YES]; and registering for the associated power state change notification, executing setDeviceScanningEnabled:YES in the callback
  • Calling [btCont resetDeviceScanning] prior to the setDeviceScanningEnabled call
  • Calling the scanForConnectableDevices:(unsigned int)arg1; method, guessing that arg1 may be some kind of timeout value. I've tried a variety of values with no success.

Any thoughts would be much appreciated. Thanks!

like image 460
WoolyWonder Avatar asked Nov 04 '22 03:11

WoolyWonder


1 Answers

As far as I know, the bluetooth manager gets the list after OS has filtered the results. You will only get the nearby headset devices and not all generic devices.

you have to use scanForServices:

    // start scan
    [btManager  setDeviceScanningEnabled:YES];
    [btManager scanForServices:0xFFFFFFFF];
like image 189
lucianoenrico Avatar answered Nov 15 '22 04:11

lucianoenrico