標籤:equal 藍芽 blog min moni ide 設計 minor man
前段時間寫項目,設計到了通過藍芽ibeacon 的方式接收資料,最開始自己都之前都沒聽過什麼叫ibeacon,然後就開始查資料,慢慢的也瞭解並知道了ibeacon怎麼使用了。先大概解釋下ibeacon到底是個什麼,在我理解ibeacon可以理解為一個發射訊號的基站,類似於移動的訊號塔。手機作為一個裝置時,進入到了發射基站的訊號覆蓋範圍內,那麼手機就能夠收到基站發出的資訊,這裡就是所謂的ibeacon資料。當然也要基站發送資料手機才能夠接受到資料。
下面說說ibeacon的使用。
1 首先需要需要在項目plist 中配置 Privacy - Location Always Usage Description 讓程式允許使用位置
2 要使用ibeacon ,需要在項目中匯入 CoreLocation 架構
3 執行個體化一個位置管理者對象,這裡叫做 CLLocationManager ,再執行個體化一個ibeacon 對象: CLBeaconRegion
self.locationmanager = [[CLLocationManager alloc] init];//初始化 self.locationmanager.delegate = self; _locationmanager.distanceFilter=10;//即時更新定位位置 _locationmanager.desiredAccuracy=kCLLocationAccuracyBest;//定位精確度 self.beacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID] identifier:@"media"];//初始化監測的iBeacon資訊
[self.locationmanager requestAlwaysAuthorization];
4 當位置管理者的代理被調用,知道了可以時刻使用使用者的位置時然後開始讀取指定beacon的資料
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ if (status == kCLAuthorizationStatusAuthorizedAlways) { [self.locationmanager startMonitoringForRegion:self.beacon1];//開始 [self.locationmanager startRangingBeaconsInRegion:self.beacon1]; }}
5 當手機進入到了硬體裝置的地區之後就會收到硬體裝置發出的beacon 資訊
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{ //如果存在不是我們要監測的iBeacon那就停止掃描他 if (![[region.proximityUUID UUIDString] isEqualToString:BEACONUUID]){ [self.locationmanager stopMonitoringForRegion:region]; [self.locationmanager stopRangingBeaconsInRegion:region]; } //列印所有iBeacon的資訊 for (CLBeacon* beacon in beacons) { NSLog(@"rssi is :%ld-=mj%d-====min%d",beacon.rssi,beacon.major.intValue,beacon.minor.intValue); }}
如果在硬體範圍內,硬體一直在發射訊號,那麼手機就會一直收到硬體的ibeacon資料
iOS ibeacon 使用詳解