iOS蓝牙通知应用程序蓝牙设备连接时
我一直在这个项目上工作了一段时间。当应用程序位于后台的应用程序切换器中时,需要定期从蓝牙设备收集数据。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
由于周围没有被完全确定,我相信
有在该代码至少有两个问题。
-
你不应该自己创建
CBPeripheral
实例。这会导致你的崩溃。 如果您需要连接到蓝牙设备在centralManager:didDiscoverPeripheral:advertisementData:RSSI:
或
centralManager:didRetrievePeripherals:
这些方法为您提供一个有效的
CBPeripheral
实例这样做。在您的真实应用程序中,您应该创建一个用户界面,供用户选择其中一个发现的设备。您不想连接到您发现的所有设备。
-
应该是
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
您应该实现其他人。
这仍然可以与附件框架设备一起使用吗? – theshadow124 2014-11-23 00:48:16
否。核心蓝牙是连接蓝牙低功耗设备的一种方式。 – 2014-11-24 05:12:32
只需发出一个连接到您的外设。当外围设备处于范围内时,即使您的应用程序位于后台,也会调用“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