【问题标题】:Minimal iOS BluetoothManager Example最小的 iOS BluetoothManager 示例
【发布时间】:2012-03-19 23:19:25
【问题描述】:

我一直在构建一个使用 iOS 5.0 中的 BluetoothManager 私有框架检测附近蓝牙设备的最小示例。

使用在这个问题中找到的答案:Finding generic Bluetooth devices within reach

这是我注册 BluetoothAvailabilityChangedNotification 的 viewDidLoad 方法。我也注册了 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
  • 在调用 setDeviceScanningEnabled 之前调用 [btCont resetDeviceScanning]
  • 调用 scanForConnectableDevices:(unsigned int)arg1;方法,猜测 arg1 可能是某种超时值。我尝试了各种价值观,但都没有成功。

任何想法将不胜感激。 谢谢!

【问题讨论】:

  • 有什么更新吗?你能解决问题吗?你有关于 scanForConnectableDevices 方法的任何信息吗?

标签: iphone objective-c


【解决方案1】:

据我所知,蓝牙管理器在操作系统过滤结果后获取列表。 您只会获得附近的耳机设备,而不是所有通用设备。

你必须使用 scanForServices:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2021-08-19
    • 2014-12-12
    • 2019-09-08
    • 2016-04-18
    相关资源
    最近更新 更多