我一直在構建一個最小的示例,用於在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