項目中用到了ibeacon技術(shù)缓熟,一直想記錄下來,寫了一個關(guān)于掃描ibeacon設(shè)備的Demo蕴纳,有錯誤之處会油,歡迎指正个粱。
1、工程配置
在info.plist文件中添加如下參數(shù)
上圖是添加位置定位使用翻翩。
上圖可以不添加都许,我的項目中需要后臺也掃描ibeacon并發(fā)送Ibeacon信息給服務(wù)器,所以采用播放一段無聲音樂強(qiáng)制常駐后臺嫂冻,這個可以根據(jù)你的需求來胶征,一般是不需要的。
2桨仿、ibeacon知識講解
什么是ibeacon:通過使用低功耗藍(lán)牙技術(shù)(Bluetooth Low Energy睛低,也就是通常所說的Bluetooth 4.0或者Bluetooth Smart),iBeacon基站可以創(chuàng)建一個信號區(qū)域,當(dāng)設(shè)備進(jìn)入該區(qū)域時钱雷,相應(yīng)的應(yīng)用程序便會提示用戶是否需要接入這個信號網(wǎng)絡(luò)骂铁。通過能夠放置在任何物體中的小型無線傳感器和低功耗藍(lán)牙技術(shù),用戶便能使用iPhone來傳輸數(shù)據(jù)罩抗。
beacon可以用來做什么:
- ibeacon做室內(nèi)導(dǎo)航:
用戶可以連接到最近的iBeacon基站拉庵,從而獲得該基站的GPS位置信息,從而知道目前所處的地點套蒂。當(dāng)用戶進(jìn)入或離開某個iBeacon基站的通信范圍時都會收到相應(yīng)的通知信息钞支,從而實現(xiàn)導(dǎo)航的目的。 - 娛樂行業(yè)
使用者安裝了了官方 iOS app 并打開藍(lán)牙后操刀,在檢驗員或者會場附近路過時烁挟,手機(jī)會自動收到提醒,使用者可以選擇觀看電影買票馍刮。 - 教育行業(yè)
白皙的移動開發(fā)公司開發(fā)了一款app BeHere信夫,主要用于幫助老師幫助老師點名查看學(xué)生的考勤情況,當(dāng)學(xué)生走進(jìn)教室的時候卡啰,該APP自動簽到静稻,這個APP可以省去老師們點名的時間,APP實現(xiàn)了自動簽到匈辱,該產(chǎn)品已經(jīng)在大學(xué)試用振湾,其實未來更應(yīng)該借助諸如穿戴設(shè)備推廣到中小學(xué)生,及時檢測兒童的是否出勤亡脸,防止學(xué)生發(fā)生意外押搪。
3、程序講解
.m文件中聲明相關(guān)對象示例浅碾,AVAudioSession大州、AVAudioPlayer是播放音樂時使用的對象。BEACONUUID一定要換成自己的ibeacon模塊的垂谢,很多同學(xué)拿到Demo沒有改代碼厦画,是掃描不到的。
#import "ViewController.h"
#define BEACONUUID @"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"http://iBeacon的uuid可以換成自己設(shè)備的uuid
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>{
AVAudioSession *session;
AVAudioPlayer *_player;
}
@property (strong, nonatomic) UITableView *tableView;
@end
下面是播放一段無聲音樂的代碼滥朱,保證應(yīng)用強(qiáng)制常駐后臺
- (void)playbackgroud
{
/*
這里是隨便添加得一首音樂根暑。
真正的工程應(yīng)該是添加一個盡可能小的音樂。徙邻。排嫌。
0~1秒的
沒有聲音的。
循環(huán)播放就行缰犁。
這個只是保證后臺一直運(yùn)行該軟件淳地。
使得該軟件一直處于活躍狀態(tài).
你想操作的東西該在哪里操作就在哪里操作怖糊。
*/
session = [AVAudioSession sharedInstance];
/*打開應(yīng)用會關(guān)閉別的播放器音樂*/
// [session setCategory:AVAudioSessionCategoryPlayback error:nil];
/*打開應(yīng)用不影響別的播放器音樂*/
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[session setActive:YES error:nil];
//設(shè)置代理 可以處理電話打進(jìn)時中斷音樂播放
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *URLPath = [[NSURL alloc] initFileURLWithPath:musicPath];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:URLPath error:nil];
[_player prepareToPlay];
[_player setDelegate:self];
_player.numberOfLoops = -1;
[_player play];
}
/*下面是重點*/
//初始化ibeacon方法
- (void)initIbeacon{
self.beaconArr = [[NSArray alloc] init];
self.locationmanager = [[CLLocationManager alloc] init];//初始化
self.locationmanager.delegate = self;
//初始化監(jiān)測的iBeacon信息
self.beacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID] identifier:@"tencent1"];
[self.locationmanager requestAlwaysAuthorization];//設(shè)置location是一直允許
[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//開始
//[self playbackgroud];
}
//CLLocationManager總是使用位置
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationmanager startMonitoringForRegion:self.beacon1];//開始MonitoringiBeacon
}
}
手機(jī)作為中心設(shè)備,掃描周邊的ibeacon設(shè)備颇象,會執(zhí)行下面的代理方法蓬抄。
CLBeacon對象有以下幾個屬性
//ibeacon的UUID
@property (readonly, nonatomic, copy) NSUUID *proximityUUID;
//ibeacon的major,可以通過軟件修改
@property (readonly, nonatomic, copy) NSNumber *major;
//ibeacon的minor,可以通過軟件修改
@property (readonly, nonatomic, copy) NSNumber *minor;
@property (readonly, nonatomic) CLProximity proximity;
//ibeacon的accuracy,ibeacon距離中心設(shè)備的距離,轉(zhuǎn)換成浮點型即可
@property (readonly, nonatomic) CLLocationAccuracy accuracy;
//ibeacon的rssi,ibeacon信號輻射強(qiáng)度夯到,這個強(qiáng)度也可以反應(yīng)距離
@property (readonly, nonatomic) NSInteger rssi;
//找的iBeacon后掃描它的信息
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
//如果存在不是我們要監(jiān)測的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",beacon.rssi);
NSLog(@"beacon.proximity %ld",beacon.proximity);
NSLog(@"beacon.proximity %@",beacon.major);
NSLog(@"beacon.proximity %@",beacon.minor);
}
self.beaconArr = beacons;
[self.tableView reloadData];
}
在tableview中顯示出來嚷缭,包括掃描到的Ibeacon UUID,信號強(qiáng)度、距離等信息
- (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 %@ %@ %.2f",str,beacon.rssi,beacon.major,beacon.minor,(double)(beacon.accuracy)];
return cell;
}
4耍贾、界面截圖
如有錯誤之處阅爽,歡迎指正,有疑問可以聯(lián)系QQ:623778119
Github演示Demo: https://github.com/Harvyluo/iBeaconDemo