In CBCentralManager delegate method didDiscoverPeripheral: never called - ios

I initialize the CBCenteralManager in viewdidload on main queue. First time update state method is called and start scanning for bluetooth devices but didDiscoverPeripheral: delegate method never get called.
Code is :
- (void)viewDidLoad
{
[super viewDidLoad];
self.CM = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state == CBCentralManagerStatePoweredOn) {
[self.CM scanForPeripheralsWithServices:nil options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
}
-(void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
}

Related

CoreBluetooth , BLE : couldn't discover peripheral

I have a BLE device (obd2 dongle) that I want to connect to. I can pair with it through "settings" but I can't discover it via didDiscoverPeripheral delegate method , any help ?
- (void)viewDidLoad {
[super viewDidLoad];
_centralManager =[[CBCentralManager alloc]initWithDelegate:self queue:nil];
[self scan];
}
-(void)scan {
NSDictionary *options1 =#{ CBCentralManagerScanOptionAllowDuplicatesKey : #YES };
[_centralManager scanForPeripheralsWithServices:nil options:options1];
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state == CBCentralManagerStatePoweredOn) {
[self scan];
}
}
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
[peripheral readRSSI];
[central connectPeripheral:peripheral options:nil];
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
CBPeripheral *peripheral1 =peripheral;
NSLog(#"peripheral1 name = %#",peripheral1.name); peripheral.delegate=self;
[peripheral discoverServices:nil];
}

BLE only getting Battery Level characteristic Value IOS

I am trying to read all the services available and their characteristics value using CoreBluetooth framework from a device.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(#"Received peripheral : \n%#", peripheral);
NSLog(#"Adv data : %#", advertisementData);
self.activeperipheral=peripheral;
[self.myCentralManager connectPeripheral:peripheral options:nil];
if(peripheral.state==CBPeripheralStateConnected){
peripheral.delegate=self;
NSLog(#"Connected");
}
else
NSLog(#"Not Connected");
NSArray *serviceUUIDs=[advertisementData objectForKey:CBAdvertisementDataServiceUUIDsKey];
for(CBUUID *foundserviceUUIDs in serviceUUIDs){
if([serviceUUIDs containsObject:foundserviceUUIDs]){
NSLog(#"%#",serviceUUIDs);
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for(CBService *service in peripheral.services){
[peripheral discoverCharacteristics:nil forService:service];
NSLog(#"Discover Service:%#",service);
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for(CBCharacteristic *characteristic in service.characteristics){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
self.myCharacteristic=characteristic;
NSLog(#"NotifyValue set on %#",characteristic);
//[peripheral readValueForCharacteristic:myCharacteristic];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(characteristic.isNotifying)
NSLog(#"Notification began on %#",characteristic);
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(#"%#",characteristic);
// NSLog(#"Characteristic Value Updated");
}
- (int)scanForPeripherals {
NSLog(#"Scanning");
if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
NSLog(#"Turn on Bluetooth");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
NSLog(#"Scanning");
[myCentralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(#"Peripheral Connected");
peripheral.delegate=self;
//CBUUID *serviceUUID=[CBUUID UUIDWithString:#"1804"];
[peripheral discoverServices:nil];
//[myCentralManager stopScan];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(#"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(#"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(#"didReadRSSI");
}
#end
The problem is I cannot setNotifyValue : Yes for other characteristics except the BatteryLevel characteristic and I am also getting the notification for the same only.
Here is my output.
Any idea what am I doing wrong here?
Thanks
That because your characteristic settings in gatt file (your bluetooth project) is not properly setup. See for example another characteristic settings and update your gatt file.
Another way to get characteristic value use a timer in which read your charackteristic:
-(void)TimerFunction
{
[talking_peripher readValueForCharacteristic:_talk_charack];
NSlog("VALUE: %#" _talk_charack);
}
And that is all. But actually best to do - modify your characteristic settings in gatt file (bluetooth project).

retrievePeripherals deprecated in IOS7 how to substitude it with retrievePeripheralsWithIdentifiers

I am using "[self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]];” which works fine but is deprecated in IOS7. I found I have to use “retrievePeripheralsWithIdentifiers” instead. But I can not figure out which would be the right syntax for the new function. Can anyone please translate
"[self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]];”
to the new function “retrievePeripheralsWithIdentifiers”.
// Invoked when the central discovers heart rate peripheral while scanning.
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)aPeripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSMutableArray *peripherals = [self mutableArrayValueForKey:#"heartRateMonitors"];
if(![self.heartRateMonitors containsObject:aPeripheral])
[peripherals addObject:aPeripheral];
// Retrieve already known devices
[self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]];
}
Finally I could solve it, hope it helps.
// Invoked when the central discovers heart rate peripheral while scanning.
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)aPeripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
[self.manager retrievePeripheralsWithIdentifiers:#[[CBUUID UUIDWithString:#"180D"]]];
NSLog(#"centralManager didDiscoverPeripheral -> Discovered: %#", aPeripheral.name);
if (aPeripheral) {
[self stopScan];
_peripheral = aPeripheral;
[_manager connectPeripheral:self.peripheral
options:[NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES]
forKey:
CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
}

didDiscoverPeripheral never called

I have two devices (ipad 3 and iphone 4s), and was trying to find a way to recognize one of the two devices (For future exchange information between them), for that, I'm with active Bluetooth devices in 2, and using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
CBUUID *otherDevice = [CBUUID UUIDWithString:#"180A"];
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[self.mgr scanForPeripheralsWithServices:[NSArray arrayWithObject:otherDevice] options:scanOptions];
}
#pragma mark - delegates
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(#"I found new devices -> %#",[NSString stringWithFormat:#"%#",[advertisementData description]]);
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(#"This is it!");
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
[self.mgr scanForPeripheralsWithServices:nil options:nil];
break;
}
}
NSLog(#"%#",messtoshow);
}
The problem with this code is that I am getting the message on the console that bluetooth is active, but the method 'didDiscoverPeripheral:' not being called, why this is happening? (since the Bluetooth on both devices are active)?

iOS CoreBluetooth : centralManager:didConnectPeripheral / didFailToConnectPeripheral: not getting called

I'm pulling my hair out of this problems. I'm trying to connect to BLE devices, can't see what I've done wrong in my code below.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (NSString*)UUIDString:(CFUUIDRef)uuid {
CFStringRef string = CFUUIDCreateString(NULL, uuid);
return (__bridge_transfer NSString*)string;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
// NSLog(#"Received peripheral : \n%#", peripheral);
// NSLog(#"Adv data : %#", advertisementData);
[peripheral setDelegate:self];
[central connectPeripheral:peripheral options:nil];
[peripheral readRSSI];
}
- (int)scanForPeripherals {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
[_cm scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(#"didConnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(#"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(#"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(#"didReadRSSI");
}
These devices are not my own. I don't know its proximity UUID, but as far as I know, It won't be needed in connecting via CoreBluetooth right?
All of the devices are discovered in didDiscoverPeripheral:, in the selector I tried to connect them. But there's nothing comes after that.
Am I to expect a dialog with Pairing Password Request when I called to didDiscoverPeripheral:?
If so I don't see any dialog, why is that?
From apple documents, It clearly stated that after trying to connect to a device you should get a called to either didConnectPeripheral or didFailToConnectPeripher but I got none.
Any thoughts? I've been trying for almost a week now.
Appreciate every helps, thanks.
If you don't somehow retain the peripheral object that is delivered to didDiscoverPeripheral then it is released once this delegate method exits and you won't get a connection.
I suggest adding a property to track discovered peripherals
#property (strong,nonatomic) NSMutableArray *peripherals;
initialise this in viewDidLoad or init
self.peripherals=[NSMutableArray new];
And then add the peripheral to it in didDiscoverPeripheral
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(#"Discovered peripheral %#",peripheral.identifier.UUIDString);
[self.peripherals addObject:peripheral];
[central connectPeripheral:peripheral options:nil];
}
var peripherals = [CBPeripheral]()
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
peripherals.append(peripheral)
bleManager.connectPeripheral(peripheral, options: nil)
}
This is the Swift version.

Resources