2014-11-22 236 views
1

我一直在這個項目上工作了一段時間。當應用程序位於後臺的應用程序切換器中時,需要定期從藍牙設備收集數據。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 

由於周圍沒有被完全確定,我相信

+0

只需發出一個連接到您的外設。當外圍設備處於範圍內時,即使您的應用程序位於後臺,也會調用「didConnectPeripheral」。如果你希望它可以跨應用程序/設備重新啓動,那麼在覈心藍牙編程指南 – Paulw11 2014-11-22 05:04:35

+0

中查看狀態恢復。有趣的是,Im使用由硬件製造商提供的庫來處理與設備的所有藍牙和通信協議。當你說連接時,你的意思是連接對象CBCentralManager的外設? – theshadow124 2014-11-22 05:19:10

+0

是的。 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

回答

3

有在該代碼至少有兩個問題。

  1. 你不應該自己創建CBPeripheral實例。這會導致你的崩潰。 如果您需要連接到藍牙設備在

    centralManager:didDiscoverPeripheral:advertisementData:RSSI: 
    

    centralManager:didRetrievePeripherals: 
    

    這些方法爲您提供一個有效的CBPeripheral實例這樣做。

    在您的真實應用程序中,您應該創建一個用戶界面,供用戶選擇其中一個發現的設備。您不想連接到您發現的所有設備。

  2. 應該是CBCentralManagerDelegate方法的方法被命名爲錯誤的,所以它們將永遠不會被調用。您不能更改這些方法的選擇器(即「名稱」)。

    正確的方法被命名爲:

    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
    - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
    

    您應該實現其他人。

+0

這仍然可以與附件框架設備一起使用嗎? – theshadow124 2014-11-23 00:48:16

+0

否。核心藍牙是連接藍牙低功耗設備的一種方式。 – 2014-11-24 05:12:32