Bryan ORTIZ
Bryan ORTIZ

Reputation: 316

iOS - CoreBluetooth didDiscoverPeripheral called only once

I am actually making an app using Bluetooth Low Energy.

My app got 3 views so far, every one displaying some BLE informations.

When my Bluetooth protocole code was in the first view, there was no problem. But I decided to make a class and put my Bluetooth code into it, so I could make a global class for my 2 others views. I used:

self.bluetoothManager = [BluetoothManager getInstance];

BluetoothManager, being my global class.

And here is the problem:

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

This method isnt looping anymore, it is called one time at the start only. I'm affraid the others services such as discoverCharacteristics or Notifications won't loop either.

Edit: This is how I create my class

+(BluetoothManager *)getInstance {
    @synchronized(self)
    {
        if(instance==nil)
        {
            instance= [[BluetoothManager alloc] init];
        }
    }
    return instance;
}

- (id) init {
    self.CentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    self.activePeripheral = [CBPeripheral alloc];
    self.discoveredPeripherals = [NSMutableArray new];
    self.resultValue = [[NSString alloc] init];
    return self;
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    if (central.state == CBCentralManagerStatePoweredOn) {
        // Scan for devices
        [self.CentralManager scanForPeripheralsWithServices:nil options:nil];
        NSLog(@"Scanning started");
     }
}

Edit 2: So far, I did not post all the class methods to get a proper question but every methods from CBCentralManager delegate are implemented into my class.

So far I think my problem is about threads because the only difference is that my class isn't in the main queue anymore ? I did try a few things but I'm not confortable with threads, if anyone has a solution or idea to solve this problem, thanks in advance.

Upvotes: 0

Views: 2101

Answers (1)

Zmicier Zaleznicenka
Zmicier Zaleznicenka

Reputation: 1964

If didDiscoverPeripheral:... callback was invoked successfully, this means that you did everything properly. When you call scanForPeripheralsWithServices:options:, iOS starts scanning the surroundings in search for BLE devices. It will do so until you invoke stopScan explicitly or disable BT or kill your CBCentralManager instance. Some of peripheral devices will be found immediately, for some of them it will take more time. Each time when the device is discovered, didDiscoverPeripheral: callback is called. When this happens, it's your responsibility to decide whether you're interested in it, connect to it and discover its services.

Upvotes: 3

Related Questions