Callback from CBCentralManagerDelegate to retrieve CBPeripheral on iOS8

I have a BluetoothManager class that manages BTLE communications. I can scan and connect to CBPeripheral and discover services or features. I have good callbacks from CBCentralManagerDelegate and CBPeripheralDelegate

When I'm connected to CBPeripheral, I save the UUIDString to CoreData to get this peripheral when I restart the application.

This is my Swift code to retrieve peripherals when the application restarts:

func retrievePeripheralsWithIdentifiers(identifiers: [AnyObject]!){ let data = self.centralManager.retrievePeripheralsWithIdentifiers(identifiers) println("Data retrieved: \(data)") for peripheral in data as [CBPeripheral] { println("Peripheral : \(peripheral)") peripheral.delegate = self centralManager.connectPeripheral(peripheral, options: nil) } } 

This is what I have:

 Data retrieved: [<CBPeripheral: 0x1669fcd0, identifier = XXXXX, name = Peripheral1, state = disconnected>] Peripheral : <CBPeripheral: 0x1669fcd0, identifier = XXXXX, name = Peripheral1, state = disconnected> 

I can find one of my peripherals without any problems. But when I call the line "centralManager.connectPeripheral (peripheral, options: nil)", I have no answer.

These methods

 func centralManager(central: CBCentralManager!, didRetrievePeripherals peripherals: [AnyObject]!) func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) 

don't come back to tell me what happened. Therefore, I can find a good peripheral device, but I cannot connect to it.

However, I have the same code to connect to some peripherals when scanning, and this works; my own CBCentralManager has a good delegate and my peripherals.

What am I doing wrong?

I am on Xcode-Beta5 with iPod-Touch iOS8-Beta5.

Thanks,

Ben

EDIT

Ok that's weirder

I try this following code from my old application on iOS7 and Objective-C, I saved the UUIDString when I connected, and try to retrieve the peripherals. This works well: NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

 if ([defaults objectForKey:@"UUIDString"]) { NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:[defaults objectForKey:@"UUIDString"]]; NSArray * data = [self.centralManager retrievePeripheralsWithIdentifiers:@[uuid]]; if ( [data count] > 0 ){ self.currentPeripheral = [data objectAtIndex:0]; [self.currentPeripheral setDelegate:self]; [self.centralManager connectPeripheral:self.currentPeripheral options:nil]; } } else { NSLog(@"Scanning started"); /*...*/ } 

The same code from my new iOS8 and Swift app:

  let userDefaults = NSUserDefaults.standardUserDefaults() if userDefaults.objectForKey("UUIDString") != nil { let uuid = NSUUID(UUIDString: userDefaults.objectForKey("UUIDString") as String) let data = self.centralManager.retrievePeripheralsWithIdentifiers([uuid]) as [CBPeripheral] if data.count > 0 { self.currentPeripheral = data[0] println("Current peripheral \(self.currentPeripheral)") self.currentPeripheral!.delegate = self self.centralManager.connectPeripheral(self.currentPeripheral!, options: nil) } } else { println("Start Scanning") /*...*/ } 

I'm trying to use my old app on iPod Touch iOS7 and it works well. I'm trying to use my old app on iPod Touch iOS8 and it works great. I tried my new app on iPod Touch iOS8 and it does not work.

I did not find differences between the two codes. I can scan, detect, and connect peripherals, but connecting to the extracted peripherals doesn't seem to work on iOS8 and Swift.

+5
source share
3 answers

It seems that in the original example, you are not saving a local copy of CBPeripheral.

From the Link to the CBCentralManager class - connectPeripheral :

Pending connection attempts are also automatically canceled when peripherals are released.

+7
source

Make sure you save a local copy of the CBPeripheral object or not.

You must keep a local copy of the periphery to avoid dealloc after the callback function completes.

If the peripheral dealloc object prior to the peripheral callback didConnect call, the bluetooth core automatically cancels the pending connection request.

+1
source

In my case (iphone4s / ios8.2) my problem was that I tried to reconnect in the viewDidLoad() controller, but moved it to viewDidAppear() fixed it.

0
source

Source: https://habr.com/ru/post/1200014/


All Articles