iphone - 最小的 iOS BluetoothManager 示例

标签 iphone objective-c

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

使用在这个问题中找到的答案: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
  • 在调用 setDeviceScanningEnabled 之前调用 [btCont resetDeviceScanning]
  • 调用 scanForConnectableDevices:(unsigned int)arg1;方法,猜测arg1可能是某种超时值。我尝试了多种值(value)观,但都没有成功。

如有任何想法,我们将不胜感激。 谢谢!

最佳答案

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

你必须使用 scanForServices:

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

关于iphone - 最小的 iOS BluetoothManager 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779363/

相关文章:

iphone - 单击 UISearchBar 中的取消按钮会导致 tableView 中没有选择?

iphone - 在我的对象类中使用 typedef 枚举

iphone - 将 UIImage 转换为 CVPixelBufferRef

ios - UISearchBar 上方的 UITableView 背景颜色

ios - 如何使用 NS_ENUM

iphone - 仪器 vm 跟踪器脏

iphone - NSDateFormatter 模拟器上的日期不正确,设备上的日期正确

iphone - "pointInside:"是如何工作的?

ios - 根据 iPhone 屏幕尺寸增加字体大小

iphone - 返回 View 时 iOS UISlider 旋钮重影