iOS ibeacon 使用詳解

來源:互聯網
上載者:User

標籤: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 使用詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.