ios之ibeacon技术

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,否则是不会有通知下发的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容