iOS 中 iBeacon 开发

作者: Mr大喵喵 | 来源:发表于2019-03-27 15:54 被阅读40次
    iBeacon 介绍

    iBeacon 是苹果公司2013年9月发布的移动设备用OS(iOS7)上配备的新功能。其工作方式是,配备有低功耗蓝牙(BLE)通信功能的设备使用BLE技术向周围发送自己特有的 ID,接收到该 ID 的应用软件会根据该 ID 采取一些行动。
    它采用了基于蓝牙4.0的低功耗蓝牙技术(Bluetooth Low Energy, BLE),主要是用作辅助室内定位的功能.

    iBeacon的用途

    我们可以用iBeacon可以进行室内定位(车库,商场),智能打卡,提醒(离开某物体的时候,比如离开家)。

    iBeacon 原理

    iBeacon中有两个角色:
    发射者: 一般都是各种硬件
    接收者: 一般都是智能终端(手机)
    发射者通过BLE 的广告通信通道,以一定时间间隔向外广播数据包(一般是每秒两三次),接收者可以通过终端提供的功能来接收,达到信息的交互.
    从iOS开发者的角度看: iBeacon 在 CoreLocation 框架中抽象为CLBeacon类, 该类有6个属性,分别是:

    proximityUUID:是一个 NSUUID,用来标识公司。每个公司、组织使用的 iBeacon 应该拥有同样的 proximityUUID。

    major:主要值,用来识别一组相关联的 beacon,例如在连锁超市的场景中,每个分店的 beacon 应该拥有同样的 major。

    minor:次要值,则用来区分某个特定的 beacon。

    proximity:远近范围的,一个枚举值。

    typedef NS_ENUM(NSInteger, CLProximity) {
        CLProximityUnknown,// 无效
        CLProximityImmediate,//在几厘米内
        CLProximityNear,//在几米内
        CLProximityFar//超过 10 米以外,不过在测试中超不过10米就是far
    }
    

    accuracy:与iBeacon的距离。

    rssi:信号轻度为负值,越接近0信号越强,等于0时无法获取信号强度。

    • Tip:proximityUUID,major,minor 这三个属性组成 iBeacon 的唯一标识符。

    只要进入iBeacon的范围,就能唤醒 App(大约10秒钟),即使在程序被杀掉的情况下。必要时,可以使用UIApplication类的

    -(UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;
    方法,请求更多的后台执行时间。

    接收形式

    接收者提供了两种方式来接收iBeacon信号:

    Monitoring: 可以用来在设备进入/退出某个地理区域时获得通知, 使用这种方法可以在应用程序的后台运行时检测iBeacon,但是只能同时检测20个region区域,并且不能够推测设备与iBeacon的距离.

    // 开始检测区域
    [self.locationManager startMonitoringForRegion:beaconRegion]; 
    
    // 停止检测区域
    [self.locationManager stopMonitoringForRegion:beaconRegion]; 
    
    // Monitoring成功对应回调函数
    - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region;
    
    // 设备进入该区域时的回调
    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;
    
    // 设备退出该区域时的回调
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;
    
    // Monitoring有错误产生时的回调
    - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(nullable CLRegion *)region withError:(NSError *)error;
    

    Ranging: iOS 7之后提供的 API, 用于确定设备的近似距离iBeacon 技术,可以用来检测某区域内的所有iBeacons,并且可以精度估计发射者与接收者的距离。

    // 开始检测区域
    [self.locationManager startRangingBeaconsInRegion:beaconRegion];
    
    // 停止检测区域
    [self.locationManager stopRangingBeaconsInRegion:beaconRegion];
    
    // Ranging成功对应回调函数  1秒钟执行1次
    - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region 
    
    // Ranging有错误产生时的回调
    - (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
    

    iBeacon 与 BLE 的区别

    iOS 中 iBeacon 是基于地理位置的微定位技术,虽然借助手机蓝牙进行接收Majro、Minor,但是他们在开发工程中没有任何关系。
    iBeacon使用苹果提供CoreLocation库,然而在 BLE 在开发过程中使用CoreBluetooth库。从上面提供的库来看就很清楚了,特别是在 iOS8.0 之后的时候如果想使用iBeacon,必须让用户点击是否允许XXapp使用地理位置。如果在第一次使用 iOS App 扫描iBeacon的时候没有提示这句话,是不可能接收到iBeacon的信号(除非iOS 8.0之下)。如果是 BLE 则的开发过程中之需要提示用户打开蓝牙,并不要求其他的地理位置任何信息。

    iBeacon 在 iOS 中的运用

    • 权限请求
      在info.plist中添加NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,请求地理位置权限。
      开启Background Modes
      bm.png

    代码

    import UIKit
    import CoreLocation
    
    let Beacon_Device_UUID = "063FA845-F091-4129-937D-2A189A86D844"
    
    class ViewController: UIViewController {
        
        
        lazy var locationManager: CLLocationManager = {
            let loca = CLLocationManager()
            loca.delegate = self
            return loca
        }()
        
        lazy var beaconRegion: CLBeaconRegion = {
            // 监听所有UUID为Beacon_Device_UUID的Beacon设备
            let beacon = CLBeaconRegion(proximityUUID: UUID(uuidString: Beacon_Device_UUID)!, identifier: "BCTest")
            // 监听UUID为Beacon_Device_UUID,major为666的所有Beacon设备
    //        let beacon1 = CLBeaconRegion(proximityUUID: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, identifier: "BCTest")
            // 监听UUID为Beacon_Device_UUID,major为666,minor为999的唯一一个Beacon设备
    //       let beacon2 = CLBeaconRegion(proximityUUID: UUID(uuidString: Beacon_Device_UUID)!, major:  CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999), identifier: "BCTest")
            beacon.notifyEntryStateOnDisplay = true
            return beacon
        }()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // 在开始监控之前,我们需要判断改设备是否支持,和区域权限请求
            let availableMonitor = CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
            if availableMonitor {
                let authorizationStatus = CLLocationManager.authorizationStatus()
                switch authorizationStatus {
                case .notDetermined:
                    locationManager.requestAlwaysAuthorization()
                case .denied:
                    print("权限受限制")
                case .authorizedWhenInUse, .authorizedAlways:
                    locationManager.startMonitoring(for: beaconRegion)
                    locationManager.startRangingBeacons(in: beaconRegion)
                default:
                    break
                }
            } else {
                print("该设备不支持 CLBeaconRegion 区域检测")
            }
            
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    }
    
    extension ViewController: CLLocationManagerDelegate {
        
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            if status == .authorizedAlways || status == .authorizedWhenInUse {
                locationManager.startMonitoring(for: beaconRegion)
                locationManager.startRangingBeacons(in: beaconRegion)
            }
        }
    // pragma mark -- Monitoring
        /** 进入区域 */
        func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
                 print("你已经进入监控区域")
        }
        /** 离开区域 */
        func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
                  print("你已经离开监控区域")
        }
        /** Monitoring有错误产生时的回调 */
        func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
            
        }
        /** Monitoring 成功回调 */
        func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
            
        }
    // pragma mark -- Ranging
        /** 1秒钟执行1次 */
        func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
            for beacon in beacons {
                print("rssis is:\(beacon.rssi)")
                print("beacon proximity :\(beacon.proximity)")
                print(" accuracy : \(beacon.accuracy)")
                print("proximityUUID : \(beacon.proximityUUID)")
                print("major : \(beacon.major.intValue)")
                print("minor : \(beacon.minor.intValue)")
            }
        }
        /** ranging有错误产生时的回调  */
        func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
            
        }
    //    pragma mark -- Kill callBack
        
        /** 杀掉进程之后的回调,直接锁屏解锁,会触发 */
        func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
          // 发送本地通知
            let localNotification = BCLocalPush()
            let msgText: String = state == .unknown ? "未知" : state == .inside ? "区域外":"区域内"
            let msg = "你监听的Beacon区域状态:\(msgText),锁屏点亮屏幕会收到此推送"
            if region.isKind(of: CLBeaconRegion.self) {
                let bregion = region as? CLBeaconRegion
                let body = "status = \(msg),uuid = \(String(describing: bregion?.proximityUUID.uuidString)),major = \(String(describing: bregion?.major?.intValue)),minor = \(String(describing: bregion?.minor?.intValue))"
                localNotification.body = body
                localNotification.soundName = ""
                localNotification.delayTimeInterval = 0.0
                localNotification.pushLocalNotification()
            }
            
        }
    }
    
    
    用 iPhone 手机模拟 iBeacon

    任何支持使用蓝牙低功耗共享数据的 iOS 设备都可以用作 iBeacon。

    • 代码
    import UIKit
    import CoreBluetooth
    import CoreLocation
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var UUIDTextField: UITextField!
        @IBOutlet weak var majorTextField: UITextField!
        @IBOutlet weak var minorTextField: UITextField!
        
        var peripheralManager = CBPeripheralManager()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
        }
    
        @IBAction func clickedStartBtn(_ sender: UIButton) {
            if sender.currentTitle == "开始广播" {
                let proximityUUID = UUID(uuidString: UUIDTextField.text ?? "")
                let beaconRegion = CLBeaconRegion(proximityUUID: proximityUUID!, major: CLBeaconMajorValue(exactly: Float(majorTextField.text ?? "10")!)!, minor: CLBeaconMinorValue(exactly: Float(minorTextField.text ?? "10")!)!, identifier: "BCTest")
                 let beaconPeripheraData = beaconRegion.peripheralData(withMeasuredPower: nil)
                peripheralManager.startAdvertising((beaconPeripheraData as! [String : Any]))
            } else {
                peripheralManager.stopAdvertising()
            }
        }
        
    }
    
    extension ViewController: CBPeripheralManagerDelegate {
        
        func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
            
        }
          
    }
    
    
    用 iPhone 手机模拟 iBeacon 注意
    • 需要访问地理位置权限。
    • 设备需要开启蓝牙。
    • 利用 iOS 设备模拟 beacon信号,Home 出去之后是不能发送信号的。

    相关文章

      网友评论

        本文标题:iOS 中 iBeacon 开发

        本文链接:https://www.haomeiwen.com/subject/qmbzvqtx.html