iBeacon使用的是BLE技術,具體而言,利用的是BLE中名為“通告幀”(Advertising)的廣播幀。通告幀是定期發送的幀,只要是支持BLE的設備就可以接收到。iBeacon通過在這種通告幀的有效負載部分嵌入蘋果自主格式的數據來實現。
iBeacon的數據主要由四種資訊構成,分別是UUID(通用唯一標識符)、Major、Minor、Measured Power。
UUID是規定為ISO/IEC11578:1996標準的128位標識符。
Major和Minor由iBeacon發布者自行設定,都是16位的標識符。比如,連鎖店可以在Major中寫入區域資訊,可在Minor中寫入個別店鋪的ID等。另外,在家電中嵌入iBeacon功能時,可以用Major表示產品型號,用Minor表示錯誤代碼,用來向外部通知故障。
Ibeacon一項低耗能藍牙技術技術,工作原理類似之前的藍牙技術,由iBeacon發射信號,IOS設備定位接受,反饋信號。根據這項簡單的定位技術可以做出許多的相應技術應用。
#import "ViewController.h"
@interface ViewController ()@property(nonatomic, strong)NSArray *beaconArr;//存放掃描的iBeacon的數組
@property(nonatomic, strong)CLBeaconRegion *beacon; //被掃描的iBeacon
@property(nonatomic, strong)CLLocationManager *locationManager;
@property(nonatomic, strong)UITableView *tableView;
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
@property (strong, nonatomic) NSDictionary *myBeaconData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_beaconArr? = [[NSArray alloc] init];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_beacon = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"] identifier:@"media"];
//_beacon = [[CLBeaconRegion alloc] init];
[_locationManager requestWhenInUseAuthorization];//設置location一直允許
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 600) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
BOOL enable = [CLLocationManager locationServicesEnabled];
if (enable) {
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startMonitoringForRegion:_beacon];
[self.locationManager startRangingBeaconsInRegion:_beacon];
}
}
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if (peripheral.state == CBPeripheralManagerStatePoweredOn)
{
// Bluetooth is on
// Update our status label
//self.statusLabel.text = @"Broadcasting...";
// Start broadcasting
//? [self.peripheralManager startAdvertising:self.myBeaconData];
}
else if (peripheral.state == CBPeripheralManagerStatePoweredOff)
{
// Update our status label
// self.statusLabel.text = @"Stopped";
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[_locationManager startMonitoringForRegion:_beacon];//開始啟動
}
}
//發現有ibeacon進入檢測范圍
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
[_locationManager startRangingBeaconsInRegion:_beacon];
[_locationManager startMonitoringForRegion:_beacon];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[_locationManager stopRangingBeaconsInRegion:_beacon];
}
//找到IBeacon后掃描- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion *)region{
//如果存在不是我們要檢測的IBeacon那就停止掃描他
if (![[region.proximityUUID UUIDString] isEqualToString:@"12334566-7173-4889-9579-954995439125"]) {
[_locationManager stopMonitoringForRegion:region];
[_locationManager stopRangingBeaconsInRegion:region];
}
//打印所有的IBeacon的信息
for (CLBeacon *beacon in beacons) {
NSLog(@"rssi is : %ld", beacon.rssi);
NSLog(@"beacon.proximity %ld", beacon.proximity);
}
_beaconArr = beacons;
[_tableView reloadData];
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"monitoringDidFailForRegion %@ %@",region, error.localizedDescription);
for (CLRegion *monitoredRegion in manager.monitoredRegions) {
NSLog(@"monitoredRegion: %@", monitoredRegion);
}
if ((error.domain != kCLErrorDomain || error.code != 5) &&
[manager.monitoredRegions containsObject:region]) {
NSString *message = [NSString stringWithFormat:@"%@ %@",
region, error.localizedDescription];
NSLog(@"%@", message);
//? [AlertView alert:@"monitoringDidFailForRegion" message:message];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location manager failed: %@", error);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.beaconArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ident = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ident];
}
CLBeacon *beacon = [self.beaconArr objectAtIndex:indexPath.row];
cell.textLabel.text = [beacon.proximityUUID UUIDString];
NSString *str;
switch (beacon.proximity) {
case CLProximityNear:
str = @"近";
break;
case CLProximityImmediate:
str = @"超近";
break;
case CLProximityFar:
str = @"遠";
break;
case CLProximityUnknown:
str = @"不見了";
break;
default:
break;
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %ld %@ %@",str,beacon.rssi,beacon.major,beacon.minor];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end