iBeacon基础和实施
iBeacon是一种低能耗的蓝牙设备,由Apple在iOS 7上推出。iBeacon类似于室内位置的GPS导航。 它是一种无线传感器设备,可用于在商店中感知位置或上下文。 它还可以用于在移动设备上发送要约或促销的通知(例如,您可能走过商店并在移动设备上收到折扣优惠券)。
苹果公司推出了低功耗蓝牙设备iBeacon。 iBeacon充当低功率设备的发送器,可将附近的iOS 7设备通知其存在。 就像室内位置的GPS导航和无线传感器设备一样,它们可以用作商店或大型购物中心的位置和上下文感知。
iBeacon还可以通过其推送通知用于促销活动,并将促销广告发送到附近的iOS设备。 目前,iBeacon已在245家零售商店和餐馆中使用,这有助于为其商店吸引更多人潮。
另一家大型企业PayPal最近宣布了Beacon,它将允许人们通过PayPal进行付款。 iBeacon价格更便宜,而且电池效率令人难以置信,价格从5美元到33美元不等。
截至目前,iBeacon在包括印度在内的许多国家/地区均不可用。 但是,我们作为一个团队很想对这项新技术进行试验和测试,并希望通过BOYD食品订购应用程序来实现广播和接收目的。
为此,我们将iOS设备转换为iBeacon,并通过我们的应用程序进行了测试,是的,我们取得了成就。 我知道您很好奇我们是如何做到的。 以下是详细的说明和代码,可帮助您创建iBeacon。
首先,您需要创建一个虚拟UUID,以便在广播者和接收者之间进行通信。 您可以从终端获取它,只需运行uuidgen以生成UUID。
让我们开始信标广播。 在Xcode中创建一个单一视图应用程序; 将其命名为“信标广播器”。
在项目中添加“ CoreBluetooth”和“ CoreLocation”框架。
现在进入“ ViewController.h”文件,导入“ CoreBluetooth”和“ CoreLocation”框架,声明“ CLBeaconRegion”和“ CBPeripheralManager”对象,添加一个UILabel插座以显示状态,并添加一个NSDictionary获取信标数据。 您的“ ViewController.h”文件将如下所示:
1#import
2#import
3#import
4
5@ interface
ViewController : UIViewController
6
7@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
8@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
9@property (strong, nonatomic) NSDictionary *myBeaconData;
10@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
11
12@end
13
14Into “ViewController.m” file, initialize NSUUID and CLBeaconRegion objects into ViewDidLoad method:
15
16(void)viewDidLoad
17{
18 [super viewDidLoad];
19 // Do any additional setup after loading the view, typically from a nib.
20
21 // Create a NSUUID object
22 NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@”PLACE UUID HERE WHICH YOU GENERATED INTO TERMINAL”];
23
24 // Initialize the Beacon Region
25 self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@”com.myapp.beacontest”];
26}
实现CBPeripheralManager的委托方法:
1(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral
2{
3 if
(peripheral.state == CBPeripheralManagerStatePoweredOn)
4 {
5 // Bluetooth is on
6
7 // Update our status label
8 self.statusLabel.text = @”Broadcasting…”;
9
10 // Start broadcasting
11 [self.peripheralManager startAdvertising:self.myBeaconData];
12 }
13 else
if
(peripheral.state == CBPeripheralManagerStatePoweredOff)
14 {
15 // Update our status label
16 self.statusLabel.text = @”Stopped”;
17
18 // Bluetooth isn't on. Stop broadcasting
19 [self.peripheralManager stopAdvertising];
20 }
21 else
if
(peripheral.state == CBPeripheralManagerStateUnsupported)
22 {
23 self.statusLabel.text = @”Unsupported”;
24 }
25}
添加一个按钮操作以开始广播:
1 (IBAction)buttonClicked:(id)sender {
2
3 // Get the beacon data to advertise
4 self.myBeaconData = [self.myBeaconRegion peripheralDataWithMeasuredPower:nil];
5
6 // Start the peripheral manager
7 self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
8}
打开情节提要,拖动一个UIButton和一个UILabel,并将标签出口和按钮操作设置为相应的控件。 您的信标广播员现在准备就绪。
打开情节提要,拖动一个UIButton和一个UILabel,并将标签出口和按钮操作设置为相应的控件。 您的信标广播员现在准备就绪。
让我们开始实施信标接收器。 在Xcode中创建新的单视图应用程序; 将其命名为“信标接收器”。 将“ CoreLocation”框架添加到项目中。
进入“ ViewController.h”文件,导入“ CoreLocation”框架,添加一个UILbel插座以显示状态,添加CLBeaconRegion和CLLocationManager对象:
1#import
2#import
3
4@ interface
ViewController : UIViewController
5
6@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
7@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
8@property (strong, nonatomic) CLLocationManager *locationManager;
9
10@end
进入“ ViewController.m”文件,将CLLocationManager,NSUUID和CLBeaconRegion对象初始化为ViewDidLoad方法:
1(void)viewDidLoad
2{
3 [super viewDidLoad];
4 // Do any additional setup after loading the view, typically from a nib.
5
6 // Initialize location manager and set ourselves as the delegate
7 self.locationManager = [[CLLocationManager alloc] init];
8 self.locationManager.delegate = self;
9
10 // Create a NSUUID with the same UUID as the broadcasting beacon
11 NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@”PLACE UUID HERE WHICH YOU GENERATED INTO TERMINAL”];
12
13 // Setup a new region with that UUID and same identifier as the broadcasting beacon
14 self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@”com.myapp.beacontest”];
15 self.myBeaconRegion.notifyOnEntry=YES;
16 self.myBeaconRegion.notifyOnExit=YES;
17 self.myBeaconRegion.notifyEntryStateOnDisplay=YES;
18 [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
19
20 // Check if beacon monitoring is available for this device
21 if
(![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class ]]) {
22 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Monitoring not available” message:nil delegate:nil cancelButtonTitle:@”Ok” otherButtonTitles: nil]; [alert show]; return ;
23 }
24}
实现CLLocationManager委托以接收信标数据:
1(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
2{
3 [self.locationManager requestStateForRegion:self.myBeaconRegion];
4}
5-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
6{
7 if
(state == CLRegionStateInside)
8 {
9 //Start Ranging
10 [manager startRangingBeaconsInRegion:self.myBeaconRegion];
11 }
12 else
13 {
14 //Stop Ranging here
15 }
16}
17- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
18{
19 self.statusLabel.text=@”Entered region”;
20}
21
22-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
23{
24 self.statusLabel.text=@”Exit
region”;
25}
1(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
2{
3 if (beacons.count>0)
4 {
5 CLBeacon *foundBeacon = [beacons firstObject];
6 self.statusLabel.text = [NSString stringWithFormat:@"Beacon found = %@",foundBeacon.proximityUUID.UUIDString];
7 }
8 else
9 self.statusLabel.text = @”Beacon not found”;
10}
打开情节提要,拖动一个标签控件并设置UILabel插座。 您的信标接收器现已准备就绪。
首先将“信标广播器”运行到一个iOS设备中,打开蓝牙并开始广播。 然后在另一台iOS设备上运行“信标接收器”。 当您将广播设备放在附近时,您的接收设备将开始接收数据。
是的,那是工作。 这是将您的iOS设备转换为iBeacons的简单过程。
与我们分享您的意见。
原始资料来源:http://www.azilen.com/blog/ibeacon-basics-implementation/
最初发布@ http://www.azilen.com/