iOS開發(fā)iBeacon藍(lán)牙技術(shù)

iBeacon使用的是BLE技術(shù),具體而言,利用的是BLE中名為“通告幀”(Advertising)的廣播幀。通告幀是定期發(fā)送的幀,只要是支持BLE的設(shè)備就可以接收到。iBeacon通過(guò)在這種通告幀的有效負(fù)載部分嵌入蘋果自主格式的數(shù)據(jù)來(lái)實(shí)現(xiàn)。

iBeacon的數(shù)據(jù)主要由四種資訊構(gòu)成,分別是UUID(通用唯一標(biāo)識(shí)符)、Major、Minor、Measured Power。

UUID是規(guī)定為ISO/IEC11578:1996標(biāo)準(zhǔn)的128位標(biāo)識(shí)符。

Major和Minor由iBeacon發(fā)布者自行設(shè)定,都是16位的標(biāo)識(shí)符。比如,連鎖店可以在Major中寫入?yún)^(qū)域資訊,可在Minor中寫入個(gè)別店鋪的ID等。另外,在家電中嵌入iBeacon功能時(shí),可以用Major表示產(chǎn)品型號(hào),用Minor表示錯(cuò)誤代碼,用來(lái)向外部通知故障。

Ibeacon一項(xiàng)低耗能藍(lán)牙技術(shù)技術(shù),工作原理類似之前的藍(lán)牙技術(shù),由iBeacon發(fā)射信號(hào),IOS設(shè)備定位接受,反饋信號(hào)。根據(jù)這項(xiàng)簡(jiǎn)單的定位技術(shù)可以做出許多的相應(yīng)技術(shù)應(yīng)用。


#import "ViewController.h"

@interface ViewController ()@property(nonatomic, strong)NSArray *beaconArr;//存放掃描的iBeacon的數(shù)組

@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];//設(shè)置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];//開始啟動(dòng)

}

}

//發(fā)現(xiàn)有ibeacon進(jìn)入檢測(cè)范圍

- (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{

//如果存在不是我們要檢測(cè)的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 = @"遠(yuǎn)";

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • @end 與之前一樣,你需要初始化位置管理器并設(shè)置它們的 delegate 。 在 application:did...
    LiWeiJ閱讀 2,191評(píng)論 0 0
  • 2015.10.19 airlocate 本文摘抄加個(gè)人總結(jié) ========= airlocate顯示如何使用這...
    Puiwah_Wai閱讀 5,381評(píng)論 2 5
  • 更新下 把我之前寫的demo找到了放到git上面了地址 最近時(shí)間一直在研究ibeacon所以把自己遇到的一些問題寫...
    沙瓦迪卡閱讀 21,382評(píng)論 53 55
  • iBeacon在iOS 7之后的版本中有內(nèi)置庫(kù),直接引入就可以使用 #import<CoreLocation/Co...
    ___Lynn閱讀 2,050評(píng)論 0 0
  • 今日觀點(diǎn): 第一,抓大的意思是只管大事。 思考:老板、中層管理、員工好比是金字塔式形狀,老板站在金字塔最高處,員工...
    楊雪雪閱讀 227評(píng)論 0 0