I'm creating an iOS app that triggers a server call whenever you come into bluetooth proximity of another iPhone running the app. Each iOS device should have some unique bluetooth advertisement identifier (e.g major/minor combo) so the appropriate server/DB call can be made.
My first plan was to use the standard iBeacon API (CoreBluetooth + CoreLocation). Worked like a charm but hit a standstill when I realized that you can't advertise bluetooth while your app in background using this method.
Method 1
//Create the NSUUID Object
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"6CA7D72E-824E-45D1-99E9-02BD33599A81"];
//Initialize the Beacon Region
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@"com.emjoseph.test"];
//Get the beacon data to advertise
self.myBeaconData = [self.myBeaconRegion peripheralDataWithMeasuredPower:nil];
//Start the peripheral manager
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if(peripheral.state == CBPeripheralManagerStatePoweredOn){
//Bluetooth is on - start broadcasting
[self.peripheralManager startAdvertising:self.myBeaconData];
}
}
My second attempt was using the Instrument/Vicinity API set up by Bentford on Github that replicates the iBeacon API solely using CoreBluetooth. This solved the issue of being able to advertise in the background. The problem though was that I couldn't figure out how to send the equivalent of a major or minor in the bluetooth advertisement data and this is limiting because the receiving phone needs to some unique identifier of the device it received data from so it can make the appropriate server/DB call. It seems I just have access to a UUID and name property in the advertisement data.
Method 2
NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"emjoseph",
CBAdvertisementDataServiceUUIDsKey:@[CBUUID],
};
// Start advertising over BLE
[peripheralManager startAdvertising:advertisingData];
Any hunches on how I can I achieve the desired setup? I thought about using method 2's CBAdvertisementDataLocalNameKey property string as a unique ID, but it has its limits.
Also I'd like all users of the app to be on the same UUID to filter out unwanted signals.
To reiterate once more, the basic flow of the app is: When person A gets close to person B's phone, person A receives some unique ID from person B via bluetooth (e.g major/minor combo) and person A uses that unique ID to retrieve the relevant information from the DB/server. The only stipulation is that I'd like for this process to happen when the app is running in the background as well.