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!

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];

Here's a way to do it: http://www.pocketmagic.net/?p=2827

The discovered device notification is a pointer to a BluetoothDevice object.


Need Your Help

NixOS list all possible configuration options

nixos

I started using NixOS a few days ago. It seems like there is a configuration option for everything, e.g. hardware.opengl.driSupport32Bit or system.copySystemConfiguration.

createStoredProcedureQuery blocks forever

hibernate java-ee derby hibernate-entitymanager

I have been trying to create backups of a derby database from within a Java EE application running on Glassfish. I am trying to accomplish that using StoredProcedureQueries which work fine in the m...