iBeacon使用

来源:互联网 发布:linux exec < > 编辑:程序博客网 时间:2024/05/17 18:26
  • 有这样一个装置,将它放在你的店里,它自动给路过的行人发送广告数据。对于行人来说,只有他有接收装置,就能收到你的广告。

  • 给小狗脖子上挂个配饰,它能将它的名字、住址、喜好广播出去。如果它走丢了,只要好心人收到它的信号,就能把它送回去。而且一旦它走远了,主人也会知道。

现在,apple推出的iBeacon技术就能实现上面的要求。
beacon |ˈbiːkən| 灯塔

根据Developing iBeacons Apps with Swift这篇教程,下面也把广播数据的装置称为Pop, 将探测Pop的装置称为Spot.

iBeacon使用了CoreBluetooth和CoreLocation技术。Pop就是利用蓝牙将数据广播出去的。

详细内容请参考官网的这篇Region Monitoring and iBeacon,下面主要说一下使用的步骤。其中pop很好完成,只要会CoreBluetooth就好。Spot遇到了一些问题,下面都有记录。

Pop使用步骤

swift描述

1.导入CoreBluetooth,CoreLocation

import CoreBluetoothimport CoreLocation

2.遵从CBPeripheralManagerDelegate协议

class ViewController: UIViewController, CBPeripheralManagerDelegate

必须实现peripheralManagerDidUpdateState方法,否则会报错

3.创建CBPeripheralManager

var peripheralManager: CBPeripheralManager!

viewDidLoad()中添加

peripheralManager = CBPeripheralManager.init(delegate: self, queue: nil)

4.CBPeripheralManager广告信息

let major: CLBeaconMajorValue = 231let minor: CLBeaconMinorValue = 789let uuid = NSUUID.init(UUIDString: "79F515C7-83C1-40AA-955B-A04F4CD3DC12")let beaconReagon = CLBeaconRegion(proximityUUID: uuid!, major: major, minor: minor, identifier:"com.apphello.beaconpop")dataDic = beaconReagon.peripheralDataWithMeasuredPower(nil)peripheralManager.startAdvertising(dataDic as? [String: AnyObject])switchButton.setTitle("Stop", forState: UIControlState.Normal)

只需以上四步,就成功创建了一个iBeacon广播数据源。其它的代理方法根据实际需要进行添加处理。

可以把这个iBeacon装置缩小,挂到小狗脖子上,这样它走远了你就能知道。


Spot使用步骤

1.导入CoreLocation模块

import CoreLocation

2.遵从CLLocationManagerDelegate协议

class ViewController: UIViewController, CLLocationManagerDelegate

3.设置CLLocationManager, 并打开monitor

locationManager = CLLocationManager()locationManager.delegate = selflocationManager.requestAlwaysAuthorization()let uuid = NSUUID.init(UUIDString: "79F515C7-83C1-40AA-955B-A04F4CD3DC12")beaconRegion = CLBeaconRegion.init(proximityUUID: uuid!, identifier: "com.apphello.beaconpop")beaconRegion.notifyOnEntry = truebeaconRegion.notifyOnExit = truelocationManager.startUpdatingLocation()locationManager.startMonitoringForRegion(beaconRegion)


  • locationManager.requestAlwaysAuthorization需要和info.plist配合使用,见CoreLocation使用步骤
  • 发现只能用requestAlwaysAuthorization,如果用requestWhenInUseAuthorization,就会出现如下的错误:

monitoringDidFailForRegion: The operation couldn’t be completed. (kCLErrorDomain error 4.)

4.monitor开启之后,问beacon在不在范围内

func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {    print("didStartMonitoringForRegion: \(region.description)")    locationManager.requestStateForRegion(region)}

5.在范围内则执行range(监测距离变化)

func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {    if state == CLRegionState.Inside {        locationManager.startRangingBeaconsInRegion(beaconRegion)    } else {        locationManager.stopRangingBeaconsInRegion(beaconRegion)    }}

6.反馈距离变化

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {    if beacons.count > 0 {        let closestBeacon: CLBeacon = beacons[0]        switch closestBeacon.proximity {        case CLProximity.Near:            print("Near")        case CLProximity.Far:            print("Far")        case CLProximity.Immediate:            print("Very close")        case CLProximity.Unknown:            print("Unknown")        }    } else {        print("No beacons")    }}

7.跨越边界通知

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {    print("Enter!!!")}func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {    print("Exit!!!")}

由于beacon ranging已经作了处理,所以这里只打印一下即可。如果有关联UI的话,在这里更新即可。

8.Error handling

前面用requestWhenInUseAuthorization出现的错误就是下面第一个函数打印的。

func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {    print("monitoringDidFailForRegion: \(error.localizedDescription)")}func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {    print("didFailWithError: \(error.localizedDescription)")}
0 0
原创粉丝点击