I want to simulate multiple ibeacon signal using my ipad's bluetooth, is it possible
问题:
回答1:
You cannot make multiple transmissions go out simultaneously, but you can simulate this by switching between two or more transmitters with a timer. iOS devices normally send out 10 advertising packets per second when transmitting as an iBeacon. But receivers only expect that packets be received at a minimum of once every second for normal operations.
Try setting up a timer to switch back and forth between two iBeacon transmitters (turn one off then the other on). Like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(@"We are going to simulate advertising multiple iBeacons simultaneously!");
CLBeaconRegion *iBeacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"] major:15555 minor:35001 identifier:@"iBeacon1"];
CLBeaconRegion *iBeacon2 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"] major:15555 minor:35002 identifier:@"iBeacon2"];
iBeacons = [[NSMutableArray alloc] init];
[iBeacons addObject: iBeacon1];
[iBeacons addObject: iBeacon2];
measuredPower = [NSNumber numberWithInt:-59];
currentIBeaconNumber = 0;
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
[self rotateAdvertising];
return YES;
}
- (void) configureAdvertisingForIBeaconNumber: (int) iBeaconNumber {
if(self.peripheralManager.state!=CBCentralManagerStatePoweredOn) {
NSLog(@"Core Bluetooth is off");
return;
}
[self.peripheralManager stopAdvertising];
NSLog(@"Transmitting as iBeacon number %d", currentIBeaconNumber);
NSDictionary *peripheralData;
peripheralData = [[iBeacons objectAtIndex:iBeaconNumber] peripheralDataWithMeasuredPower:measuredPower];
[self.peripheralManager startAdvertising:peripheralData];
}
- (void) rotateAdvertising {
[self configureAdvertisingForIBeaconNumber:currentIBeaconNumber];
currentIBeaconNumber = (currentIBeaconNumber + 1) % iBeacons.count;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1000* NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[self rotateAdvertising];
});
}
I tested this and it works -- a second iOS device ranged both iBeacons.
If I tried to switch between the two identifiers more than once per second, the receiving iOS device would periodically lose track of one of the beacons. Because this code is only switching once per second, the receiver will have gaps of a little over one second when it won't be receiving one of the two iBeacon transmissions. This may or may not cause some unexpected side effects on ranging/monitoring on the receiver side. But you can try it and see.