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:
Any thoughts would be much appreciated. Thanks!
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With