我一直在这个项目上工作了一段时间。当应用程序位于后台的应用程序切换器中时,需要定期从蓝牙设备收集数据。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