Domanda

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

È stato utile?

Soluzione

There are lots of pieces you need to take care of:

  1. You need to wait for the 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.
  2. It is also possible that the device turns off advertising after it is connected, so your scanning will not succeed until you disconnect from it.
  3. Scanning in the background has many limitations. You can search the SO questions to find out the details. In the beginning I would advise you to not to try backgrounded operation as it can be really tricky.

Altri suggerimenti

Instead of searching immediately after initializing the central manager, try first to wait for update that the power is on.

Try these steps:

  • In viewDidLoad remove the call to scanForPeripheralsWithServices
  • Add method scanForPeripherals, that will first check the Central Manager is powered on and also check for the scanning state (see below).

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");
}
  • In ViewWillAppear call [self scanForPeripherals]
  • In centralManagerDidUpdateState, call scanForPeripherals only if central manager is powered on.

code:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state != CBCentralManagerStatePoweredOn) {        
        NSLog(@"CBCentralManager not powered on yet");
        return;
    }    
    // The state must be CBCentralManagerStatePoweredOn
    [self scanForPeripherals];
}
  • Add BOOL property scanning. Using the scanning propery allows you to safely try and scan before the update callback called. You should handle the scanning state to prevent calling scan twice.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top