Reputation: 2902
I'm working on BLE device named WIRELESS BLOOD PRESSURE WRIST MONITOR. I've downloaded these application and every thing is working great. But when I tried to connect to the device from my application, I didn't receive a response. and my code is straight like the code from developer.apple.com and also this tutorial.
This is my code:
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_centralManager scanForPeripheralsWithServices:nil options:nil];
I receive notification on the delegate for centralManagerDidUpdateState
but I don't receive the didDiscoverPeripheral
even if I'm searching with nil
in services.
When I go to Setting -> Bluetooth: I can see the device and it is connected and the signal of bluetooth is on. So the iPhone can see the BLE device, So when I used in my code these method
retrieveConnectedPeripheralsWithServices
to get the list of connected device it returns 0 object.
So I don't know what is the problem, keeping in mind that the BLE device is working great with there own app so it's Low Energy not classic , and the BLE device display bluetooth signal when opening the app.
So any ideas from the GEEKS :D
Thanks..
Upvotes: 3
Views: 3891
Reputation: 16790
There are lots of pieces you need to take care of:
centralManagerDidUpdateState
to indicate CBCentralManagerStatePoweredOn
. Anything you do before will either result in error or be ignored. So your call to scanForPeripheralsWithServices
is probably ignored. This is true for other APIs, like the retrieveConnectedPeripheralsWithServices
you mentioned.Upvotes: 2
Reputation: 1731
Instead of searching immediately after initializing the central manager, try first to wait for update that the power is on.
Try these steps:
code:
- (void)scanForPeripherals {
if (self.centralManager.state != CBCentralManagerStatePoweredOn) {
NSLog(@"CBCentralManager must be powered to scan peripherals. %d", self.centralManager.state);
return;
}
if (self.scanning) {
return;
}
self.scanning = YES;
[self.centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey: @YES }];
NSLog(@"Scanning started");
}
code:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
NSLog(@"CBCentralManager not powered on yet");
return;
}
// The state must be CBCentralManagerStatePoweredOn
[self scanForPeripherals];
}
Upvotes: 2