我一直在构建一个最小的示例,用于在iOS 5.0中使用BluetoothManager专用框架检测附近的蓝牙设备。最小的iOS BluetoothManager示例
使用在这个问题找到了答案:Finding generic Bluetooth devices within reach
这里是我的viewDidLoad方法为BluetoothAvailabilityChangedNotification注册。我也注册了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];
当我拿到蓝牙可用性改变的通知,我设置的设备已启用扫描,如在上述环节的答案一个概述。
- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
NSLog(@"BT State: %d", [btCont enabled]);
[btCont setDeviceScanningEnabled:YES];
}
为了完整起见,这里是deviceDiscovered通知方法。是
- (void)deviceDiscovered:(NSNotification *) notification
{
NSLog(@"Discovered one!");
}
通过运行测试应用程序产生的日志如下:
BTM: attaching to BTServer
BTM: posting notification BluetoothAvailabilityChangedNotification
BT State: 1
BTM: setting device scanning enabled
不幸的是,手机没有拿起所有的任何蓝牙设备,即使我知道有接近发现设备(使用Android设备进行验证)。
有些事情我已经试过不已:
- 调用[btCont setPowered:YES];和用于相关联的电力状态改变通知注册,则执行setDeviceScanningEnabled:YES在回调
- 致电[btCont resetDeviceScanning]之前setDeviceScanningEnabled呼叫
- 调用scanForConnectableDevices:(无符号整型)ARG1;方法,猜测arg1可能是某种超时值。我尝试了各种价值观,但都没有成功。
任何想法将不胜感激。 谢谢!
任何更新?你能解决这个问题吗?你有关于scanForConnectableDevicesmethod的信息吗? – Mahes 2014-01-30 19:58:30