简体   繁体   中英

Connect to Peripheral of Bluetooth LE device from OSX app

I'm trying to connect to Peripherals of a BlueTooth LE device with a simple ( very similar to an Hello World ) OSX App.

I'm following Apple's Guide Lines , but when i try to connect to a Peripheral my app does not work as expected.

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered %@", peripheral.name);

if([peripheral.name isEqualToString:@"BLE-DEVICE"]){

    NSLog(@"Found BLE Device!");

    [_centralManager stopScan];

    NSLog(@"Scanning stopped");

    peripheral.delegate = self;

    [_centralManager connectPeripheral:peripheral options:nil];

   } 
}

The problem is that connectPeripheral (last line) does not trigger the centralManager:didConnectPeripheral method of the delegate object, but if i run a step by step debug with a simple break point it does.

Should i add some other scan options? How can i check if connection is rightly performed?

Try to wait for the event that tells you scanning has indeed stopped. Then, connect to the Peripheral. Maybe the iOS BLE stack doesn't have enough time to stop the scanning and when it tries to connect it fails because it has not yet reached "idle" state.

That explains why it works when debugging step by step: after stopScan is executed and before you manually execute connectPeripheral , there's enough time for the LE Controller to process the first command.

Although in a normal stack architecture, the messages should be queued.

EDIT: Alternatively, add a short delay of a few milliseconds between the two stack calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM