Reputation: 671
Ive been working on this project for quite some time. Needing to collect data from a bluetooth device periodically when the app is in the app switcher in the background.
My most recent thought is using Core Bluetooth to notify my app when a bluetooth device has connected so I can check if its my device and then do what I need to do.
Or am I miss interpreting the docs when it says: "didConnectPeripheral"?
Im having trouble finding the function in my CBCentralManager object to start a "watcher" of some sort to give me these notifications.
Am I on the wrong path here?
Thanks.
Code attempt to use core bluetooth:
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.
}
Gets error:
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
Due to peripheral not being fully defined I believe
Upvotes: 2
Views: 1722
Reputation: 90117
There are at least two problems in that code.
You are not supposed to create CBPeripheral
instances yourself. This leads to your crash.
If you need to connect to a bluetooth device do so in
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
or
centralManager:didRetrievePeripherals:
These methods provide you with a valid CBPeripheral
instance.
In your real app you should create a UI where the user can select one of the discovered devices. You don't want to connect to all devices that you discover.
The methods that are supposed to be CBCentralManagerDelegate
methods are named wrong, so they will never be called. You cannot change the selectors (i.e. the "name") of these methods.
the correct methods are named:
- (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
You should implement the others as well.
Upvotes: 4