我一直在這個項目上工作了一段時間。當應用程序位於後臺的應用程序切換器中時,需要定期從藍牙設備收集數據。iOS藍牙通知應用程序藍牙設備連接時
我最近的想法是使用Core Bluetooth在藍牙設備已連接時通知我的應用程序,以便我可以檢查它的設備,然後執行我需要的操作。
還是我想念解釋文檔時說:「didConnectPeripheral」?
我無法找到我的CBCentralManager對象中的函數來啓動某種「監視器」來給我這些通知。
我在錯誤的道路上嗎?
謝謝。
代碼試圖利用藍牙核心:
CBCentralManager *mgr;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
mgr = [CBCentralManager alloc];
CBPeripheral *peripheral = [CBPeripheral alloc];
[mgr connectPeripheral:peripheral options:nil];
// Override point for customization after application launch.
return YES;
}
- (void) mgr:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"Device Connected!");
}
- (void) mgr:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"Device Disconnected!");
}
- (void)mgr:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData);
//Do something when a peripheral is discovered.
}
獲取錯誤:
2014-11-21 23:30:27.730 TelematicsService[436:185202] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x286db49f 0x35e91c8b 0x285fb873 0x285fb657 0x283adb85 0xf005f 0x2bc084f1 0x2bdfd43f 0x2bdff98b 0x2be0a209 0x2bdfe217 0x2ee6c0d1 0x286a1d7d 0x286a1041 0x2869fb7b 0x285ed3c1 0x285ed1d3 0x2bc021bf 0x2bbfcfa1 0xf2a29 0x36411aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
由於周圍沒有被完全確定,我相信
只需發出一個連接到您的外設。當外圍設備處於範圍內時,即使您的應用程序位於後臺,也會調用「didConnectPeripheral」。如果你希望它可以跨應用程序/設備重新啓動,那麼在覈心藍牙編程指南 – Paulw11 2014-11-22 05:04:35
中查看狀態恢復。有趣的是,Im使用由硬件製造商提供的庫來處理與設備的所有藍牙和通信協議。當你說連接時,你的意思是連接對象CBCentralManager的外設? – theshadow124 2014-11-22 05:19:10
是的。 Core Bluetooth編程指南討論了背景連接 - https://developer.apple。com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth /Introduction.html#// apple_ref/doc/uid/TP40013257 – Paulw11 2014-11-22 05:20:16