最小的iOS蓝牙示例 [英] Minimal iOS BluetoothManager Example
问题描述
我一直在构建一个使用iOS 5.0中的BluetoothManager私有框架检测附近蓝牙设备的最小示例。
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
这是我的viewDidLoad方法,用于注册BluetoothAvailabilityChangedNotification。我也注册了BluetoothDeviceDiscoveredNotification。
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];
}
为了完整性,这里是deviceDiscovered通知方法。
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
不幸的是,手机根本没有拿起任何蓝牙设备,即使我知道有近似的可发现设备(使用Android设备验证)。
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:
- 调用[btCont setPowered:YES];并注册相关的电源状态变化通知,在回调中执行setDeviceScanningEnabled:YES
- 在setDeviceScanningEnabled调用之前调用[btCont resetDeviceScanning]
- 调用scanForConnectableDevices :( unsigned int)arg1;方法,猜测arg1可能是某种超时值。我尝试了各种各样的价值但没有成功。
任何想法都会非常感激。
谢谢!
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.
您必须使用scanForServices:
you have to use scanForServices:
// start scan
[btManager setDeviceScanningEnabled:YES];
[btManager scanForServices:0xFFFFFFFF];
这篇关于最小的iOS蓝牙示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!