Never seen anything in CoreBluetooth Framework abo

2020-04-16 04:54发布

问题:

I am using the Xcode 5 and CoreBluetooth Framework.
In plist file

Required background modes -> App communicates using CoreBluetooth

In Xcode 5 is a section for Background Modes in the your app's capabilities. Set Background Modes to ON and check "Uses Bluetooth LE accessories"

Our app and BLE connection works fine while the app is running.
The problem is when the phone goes to sleep or if the back goes to the background, the BLE connection stops working.
I haven't seen anything in the CoreBluetooth Framework about keeping the connection alive in the background.

When app get background than will call method to startBackgroundNotification. like below method

-(void)startBackgroundNotification{
    self.counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:counterTask];
    }];
    BackgroundManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    nDevices = [[NSMutableArray alloc]init];
    sensorTags = [[NSMutableArray alloc]init];
}

After startBackgroundNotification call than will call (delegate) to centralManagerDidUpdateState

-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state != CBCentralManagerStatePoweredOn) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"BLE not supported !"
                                                               message:[NSString stringWithFormat:@"CoreBluetooth return state: %d",central.state]
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
    }
    else
        [central scanForPeripheralsWithServices:nil options:nil];
}

But never call below function

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


When the app is in the background and we press a button on the BLE device, the phone gives a notification saying that the device needs access to the app so I have to open the app manually again.

回答1:

This is actually documented in Apple's API documentation; doing a scan without scanning for any services will not return anything while in the background, as explained here:

Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManagerOptionShowPowerAlertKey scan option is ignored while scanning in the background.

Therefore, to get any results, you must scan for peripherals with specific services, by passing a UUID in the serviceUUIDs parameter. Alternatively, if you have connected to the device you're looking for before, you can just call the connectPeripheral function directly