ios之ibeacon技术

作者: 小羊爱学习 | 来源:发表于2022-06-29 18:53 被阅读0次

1.什么是Beacon ?
Beacon是使用蓝牙4.0(BLE)技术发射信号的小设备
有效范围从几十厘米到几米,电池可用3年
信号为单向发射,只能发送小数据量,例如一个128bit的ID 智能手机通常作为接收方;
2.什么是iBeacon?
iBeacon 是苹果公司2013年9月发布的移动设备用OS(iOS7)上配备的新功能。其工作方式是,配备有低功耗蓝牙(BLE)通信功能的设备使用BLE技术向周围发送自己特有的 ID,接收到该 ID 的应用软件会根据该 ID 采取一些行动。
它采用了基于蓝牙4.0的低功耗蓝牙技术(Bluetooth Low Energy, BLE),主要是用作辅助室内定位的功能.

3.CLBeacon类
iBeacon 在 CoreLocation 框架中抽象为CLBeacon类, 该类有6个属性,分别是:

proximityUUID:是一个 NSUUID,用来标识公司。每个公司、组织使用的 iBeacon 应该拥有同样的 proximityUUID。(proximityUUID' was deprecated in iOS 13.0)ios13.0之后改为uuid

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

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

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

accuracy:与iBeacon的距离。

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

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

4.如何工作
只要进入iBeacon的范围,就能唤醒 App(大约10秒钟),即使在程序被杀掉的情况下。必要时,可以使用UIApplication类的-(UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;方法,请求更多的后台执行时间。
在开启后台模式之后,不管app出于运行、后台、还是挂起状态都会唤醒app执行该回调,你有10秒钟的唤醒时间用于处理数据(本地,或者请求服务器)

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

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

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

6.检测ibeacon会有延时:
检测ibeacon的进入比较及时,当关闭ibeacon时,检测会有10-50秒的延时。

7.检测回调:
当外设开关状态发生变化时(或者蓝牙中心离开靠近外设时),app如果处于[挂起或是关闭状态,会被系统唤醒,活跃10秒,来处理相关事件
应用程序将被启动并通过 locationManager:didDetermineState:forRegion 通知代理,当设备的屏幕打开并且用户在该区域时。 默认情况下,这是 NO。

        beacon.notifyEntryStateOnDisplay = true

**杀掉进程之后的回调,在ibeacon基站边缘时会触发,直接锁屏亮锁也会触发哦 **

8.实测距离
实测手机作为ibeacon基站,ibeacon范围也只有10米左右

注意点:
如果在iBeacon覆盖范围内或覆盖范围外启动startMonitoring,那么didDetermineState可能不会立即反映出当前所处的位置状态(didEnterRegion和didExitRegion都不会正确调用),需要等待app在当前位置运行一会才会有效(大约1分钟延迟)
测试ibeacon时记得把app应用的位置权限设置为始终哦

手机模拟ibeacon基站代码:

import UIKit
import CoreBluetooth
import CoreLocation

class ViewController: UIViewController {
    
    var peripheralManager = CBPeripheralManager()
    
    var btn:UIButton = UIButton.init()
    
    var btnStop:UIButton = UIButton.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //手机秒变ibeacon基站
        iphoneChangeIbeacon()
        
    }

    func iphoneChangeIbeacon(){
        btn.frame = CGRect(x: 100, y: 100, width: 150, height: 50)
        btn.backgroundColor = .red
        self.view.addSubview(btn)
        btn.addTarget(self, action: #selector(btnClick(btn:)), for: UIControl.Event.touchUpInside)
        btn.setTitle("开始广播", for: UIControl.State.normal)
        btn.setTitleColor(.black, for: UIControl.State.normal)
        
        
        
        btnStop.frame = CGRect(x: 100, y: 200, width: 150, height: 50)
        btnStop.backgroundColor = .red
        self.view.addSubview(btnStop)
        btnStop.addTarget(self, action: #selector(btnStopClick(btn:)), for: UIControl.Event.touchUpInside)
        btnStop.setTitle("停止广播", for: UIControl.State.normal)
        btnStop.setTitleColor(.black, for: UIControl.State.normal)
        
        
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }
    @objc func btnClick(btn:UIButton){
        print("开始广播")
//        sendNotification()
        changeIbeacon()
        
        
    }
    
    @objc func btnStopClick(btn:UIButton){
        print("停止广播")
//        sendNotification()
        peripheralManager.stopAdvertising()
        
        
    }
    
    func changeIbeacon(){
        
            let proximityUUID = UUID(uuidString: "644C651A-0DAC-47BB-9E48-78FFAC66F3CF")
            let beaconRegion = CLBeaconRegion(proximityUUID: proximityUUID!, major: CLBeaconMajorValue(exactly: 10)!, minor: CLBeaconMinorValue(exactly: 10)!, identifier: "BCTest")
             let beaconPeripheraData = beaconRegion.peripheralData(withMeasuredPower: nil)
        print("开始广播")
            peripheralManager.startAdvertising((beaconPeripheraData as! [String : Any]))
//        else {
//            peripheralManager.stopAdvertising()
//        }
        
        
    }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
//            print("DispatchQueue")
        }

    }


}
extension ViewController: CBPeripheralManagerDelegate {
    
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        print("peripheralManagerDidUpdateState")
    }
      
}


ibeacon处理代码:
权限请求:在info.plist中添加NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,请求地理位置权限。

import UIKit
import CoreLocation
let Beacon_Device_UUID = "644C651A-0DAC-47BB-9E48-78FFAC66F3CF"


class ViewController: UIViewController {
    
    var btn:UIButton = UIButton.init()
    
    //懒加载
    lazy var locationManager: CLLocationManager = {
        let loca = CLLocationManager()
        loca.distanceFilter = kCLDistanceFilterNone//更新距离精度
        loca.desiredAccuracy = kCLLocationAccuracyBest//所需的位置精度
        loca.pausesLocationUpdatesAutomatically = false//指定位置更新可能会在可能的情况下自动暂停(应用处于后台时系统进行电量节省行为的处理设置)
//        if #available(iOS 9.0, *) {
//            locationManager.allowsBackgroundLocationUpdates = true//后台运行,对于ios9或更高版本,默认为no
//        }
//
        loca.delegate = self
        return loca
    }()
    
    lazy var beaconRegion: CLBeaconRegion = {
        // 监听所有UUID为Beacon_Device_UUID的Beacon设备
        let beacon = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, identifier: "BCTest")
//        // 监听UUID为Beacon_Device_UUID,major为666的所有Beacon设备
//        let beacon = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, identifier: "BCTest")
//        // 监听UUID为Beacon_Device_UUID,major为666,minor为999的唯一一个Beacon设备
//       let beacon2 = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, major:  CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999)!, identifier: "BCTest")
        
        //应用程序将被启动并通过 locationManager:didDetermineState:forRegion 通知代理,当设备的屏幕打开并且用户在该区域时。 默认情况下,这是 NO。
        beacon.notifyEntryStateOnDisplay = true
        return beacon
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        btn.frame = CGRect(x: 100, y: 100, width: 150, height: 50)
        btn.backgroundColor = .red
        self.view.addSubview(btn)
        btn.setTitle("ibeacon测试", for: UIControl.State.normal)
        btn.setTitleColor(.black, for: UIControl.State.normal)
        
        // 在开始监控之前,我们需要判断改设备是否支持,和区域权限请求
        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)
                //此处beaconConstraint和创建beacon处保持一致
                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!)
//                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!)
//                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999)!)
                locationManager.startRangingBeacons(satisfying: beaconConstraint)
                localNotification(title: "test", body: "ibeacon启动时扫描")
            default:
                break
            }
        } else {
            print("该设备不支持 CLBeaconRegion 区域检测")
        }
    }


}
extension ViewController: CLLocationManagerDelegate {
    
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        localNotification(title: "test", body: "位置更新了")
    }
    
    //当授权状态或accuracyAuthorization 属性改变时调用
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if #available(iOS 14.0, *) {
            if manager.authorizationStatus == .authorizedAlways || manager.authorizationStatus == .authorizedWhenInUse{
                //此处beaconConstraint和创建beacon处保持一致
                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!)
    //                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!)
    //                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999)!)
                locationManager.startMonitoring(for: beaconRegion)
                locationManager.startRangingBeacons(satisfying: beaconConstraint)
                localNotification(title: "test", body: "ibeacon开始扫描")
            }
        } else {
            let state: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
            if state == .authorizedAlways || state == .authorizedWhenInUse{

                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!)
                locationManager.startMonitoring(for: beaconRegion)
                locationManager.startRangingBeacons(satisfying: beaconConstraint)
                localNotification(title: "test", body: "ibeacon开始扫描")
            }
        }
    }
    
    
//    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
//        if status == .authorizedAlways || status == .authorizedWhenInUse {
//            //此处beaconConstraint和创建beacon处保持一致
//            let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!)
////                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!)
////                let beaconConstraint = CLBeaconIdentityConstraint(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999)!)
//            locationManager.startMonitoring(for: beaconRegion)
//            locationManager.startRangingBeacons(satisfying: beaconConstraint)
//            localNotification(title: "test", body: "ibeacon开始扫描")
//        }
//    }
    
    
// pragma mark -- Monitoring
    /** 进入区域 */
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        localNotification(title: "test", body: "你已经进入监控区域")
    }
    /** 离开区域 */
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        localNotification(title: "test", body: "你已经离开监控区域")
    }
    /** Monitoring有错误产生时的回调 */
    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        localNotification(title: "test", body: "Monitoring有错")
        
    }
    /** Monitoring 成功回调 */
    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        localNotification(title: "test", body: "开始监听")
    }
// pragma mark -- Ranging
    /** 1秒钟执行1次 ,只要进入iBeacon的范围,就能唤醒 App(大约10秒钟)*/
    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.uuid)")
            print("major : \(beacon.major.intValue)")
            print("minor : \(beacon.minor.intValue)")
//            localNotification(title: "test", body: "扫描到ibeacon:uuid=\(beacon.proximityUUID)")
            
//            localNotification(title: "test", body: "距离变化")
        }
    }
    /** ranging有错误产生时的回调  */
    func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
        localNotification(title: "test", body: "rangingBeaconsDidFailFor")
        
    }
    
    
    /** 杀掉进程之后的回调,直接锁屏亮锁会触发,在ibeacon基站边缘时会触发 */
    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
        //发送本地通知
        print("jsc====app被ibeacon唤醒")
        var str: String?
        
        switch state.rawValue {
        case 0:
            str = "未知位置"
        case 1:
            str = "范围内"
        case 2:
            str = "范围外"
        default:
            str = "其他"
        }
        localNotification(title: "test", body: "app被唤醒,位置信息: \(str!)")
        
    }
    
    
    
    
    
    
    // MARK: - 发送本地通知
     func localNotification(title: String, body: String, dateComponents: DateComponents? = nil, userInfo : [AnyHashable : Any]? = nil) {
            if #available(iOS 10, *) {
                let content = UNMutableNotificationContent()
                content.title = title
                content.body = body
                content.userInfo = userInfo ?? [:]

                var trigger: UNNotificationTrigger?
                if let dataCompontnts = dateComponents {
                    trigger = UNCalendarNotificationTrigger(dateMatching: dataCompontnts, repeats: false)
                }
                
                
                let random = Int(arc4random_uniform(999999999))
                
                
                let request = UNNotificationRequest(identifier: "id\(random)", content: content, trigger: trigger)
                UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
                    print(error ?? "error:nil")
                })
            } else {
                let localNotification = UILocalNotification()
                localNotification.fireDate = dateComponents?.date ?? Date()
                localNotification.alertBody = body
                localNotification.alertTitle = title
                localNotification.userInfo = userInfo
                localNotification.timeZone = NSTimeZone.default
                localNotification.soundName = UILocalNotificationDefaultSoundName
                UIApplication.shared.scheduleLocalNotification(localNotification)
            }
        }
}

记得AppDelegate注册通知来调试验证:

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // MARK: - 注册本地通知
        regisigerNotification(application)
        
        return true
    }

    func regisigerNotification(_ application: UIApplication){
//        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
//            if !success {
//                NSLog("本地通知未打开,请授权APP使用您的本地通知")
//            }
//        }
        
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted: Bool, error: Error?) in
                DispatchQueue.main.async {
                    if granted { application.registerForRemoteNotifications() }
                }
            }
        } else {
            let settings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
            application.registerForRemoteNotifications()
            application.registerUserNotificationSettings(settings)
        }
    
        }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        NSLog("通知注册完毕")
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        NSLog("通知注册失败error:\(error)")
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(UNNotificationPresentationOptions.alert)
    }
    
}

ps:注册通知时注意了,如果是ios10.0以上,则必须实现func userNotificationCenter此方法,因为center.delegate = self,否则是不会有通知下发的。

相关文章

网友评论

    本文标题:ios之ibeacon技术

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