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
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.