1.什么是Beacon ?
Beacon是使用藍(lán)牙4.0(BLE)技術(shù)發(fā)射信號的小設(shè)備
有效范圍從幾十厘米到幾米,電池可用3年
信號為單向發(fā)射,只能發(fā)送小數(shù)據(jù)量,例如一個128bit的ID 智能手機(jī)通常作為接收方;
2.什么是iBeacon?
iBeacon 是蘋果公司2013年9月發(fā)布的移動設(shè)備用OS(iOS7)上配備的新功能。其工作方式是,配備有低功耗藍(lán)牙(BLE)通信功能的設(shè)備使用BLE技術(shù)向周圍發(fā)送自己特有的 ID,接收到該 ID 的應(yīng)用軟件會根據(jù)該 ID 采取一些行動。
它采用了基于藍(lán)牙4.0的低功耗藍(lán)牙技術(shù)(Bluetooth Low Energy, BLE),主要是用作輔助室內(nèi)定位的功能.
3.CLBeacon類
iBeacon 在 CoreLocation 框架中抽象為CLBeacon類, 該類有6個屬性,分別是:
proximityUUID:是一個 NSUUID,用來標(biāo)識公司。每個公司、組織使用的 iBeacon 應(yīng)該擁有同樣的 proximityUUID。(proximityUUID' was deprecated in iOS 13.0)ios13.0之后改為uuid
major:主要值,用來識別一組相關(guān)聯(lián)的 beacon,例如在連鎖超市的場景中,每個分店的 beacon 應(yīng)該擁有同樣的 major。
minor:次要值,則用來區(qū)分某個特定的 beacon。
proximity:遠(yuǎn)近范圍的,一個枚舉值。
accuracy:與iBeacon的距離。
rssi:信號輕度為負(fù)值,越接近0信號越強(qiáng),等于0時(shí)無法獲取信號強(qiáng)度。
Tip:proximityUUID,major,minor 這三個屬性組成 iBeacon 的唯一標(biāo)識符。
4.如何工作
只要進(jìn)入iBeacon的范圍,就能喚醒 App(大約10秒鐘),即使在程序被殺掉的情況下。必要時(shí),可以使用UIApplication類的-(UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler;方法,請求更多的后臺執(zhí)行時(shí)間。
在開啟后臺模式之后,不管app出于運(yùn)行、后臺、還是掛起狀態(tài)都會喚醒a(bǔ)pp執(zhí)行該回調(diào),你有10秒鐘的喚醒時(shí)間用于處理數(shù)據(jù)(本地,或者請求服務(wù)器)
5.接收形式:
接收者提供了兩種方式來接收iBeacon信號:
Monitoring: 可以用來在設(shè)備進(jìn)入/退出某個地理區(qū)域時(shí)獲得通知, 使用這種方法可以在應(yīng)用程序的后臺運(yùn)行時(shí)檢測iBeacon,但是只能同時(shí)檢測20個region區(qū)域,并且不能夠推測設(shè)備與iBeacon的距離.
Ranging: iOS 7之后提供的 API, 用于確定設(shè)備的近似距離iBeacon 技術(shù),可以用來檢測某區(qū)域內(nèi)的所有iBeacons,并且可以精度估計(jì)發(fā)射者與接收者的距離。
6.檢測ibeacon會有延時(shí):
檢測ibeacon的進(jìn)入比較及時(shí),當(dāng)關(guān)閉ibeacon時(shí),檢測會有10-50秒的延時(shí)。
7.檢測回調(diào):
當(dāng)外設(shè)開關(guān)狀態(tài)發(fā)生變化時(shí)(或者藍(lán)牙中心離開靠近外設(shè)時(shí)),app如果處于[掛起或是關(guān)閉狀態(tài),會被系統(tǒng)喚醒,活躍10秒,來處理相關(guān)事件
應(yīng)用程序?qū)⒈粏硬⑼ㄟ^ locationManager:didDetermineState:forRegion 通知代理,當(dāng)設(shè)備的屏幕打開并且用戶在該區(qū)域時(shí)。 默認(rèn)情況下,這是 NO。
beacon.notifyEntryStateOnDisplay = true
**殺掉進(jìn)程之后的回調(diào),在ibeacon基站邊緣時(shí)會觸發(fā),直接鎖屏亮鎖也會觸發(fā)哦 **
8.實(shí)測距離
實(shí)測手機(jī)作為ibeacon基站,ibeacon范圍也只有10米左右
注意點(diǎn):
如果在iBeacon覆蓋范圍內(nèi)或覆蓋范圍外啟動startMonitoring,那么didDetermineState可能不會立即反映出當(dāng)前所處的位置狀態(tài)(didEnterRegion和didExitRegion都不會正確調(diào)用),需要等待app在當(dāng)前位置運(yùn)行一會才會有效(大約1分鐘延遲)
測試ibeacon時(shí)記得把a(bǔ)pp應(yīng)用的位置權(quán)限設(shè)置為始終哦
手機(jī)模擬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.
//手機(jī)秒變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處理代碼:
權(quán)限請求:在info.plist中添加NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,請求地理位置權(quán)限。
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//指定位置更新可能會在可能的情況下自動暫停(應(yīng)用處于后臺時(shí)系統(tǒng)進(jìn)行電量節(jié)省行為的處理設(shè)置)
// if #available(iOS 9.0, *) {
// locationManager.allowsBackgroundLocationUpdates = true//后臺運(yùn)行,對于ios9或更高版本,默認(rèn)為no
// }
//
loca.delegate = self
return loca
}()
lazy var beaconRegion: CLBeaconRegion = {
// 監(jiān)聽所有UUID為Beacon_Device_UUID的Beacon設(shè)備
let beacon = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, identifier: "BCTest")
// // 監(jiān)聽UUID為Beacon_Device_UUID,major為666的所有Beacon設(shè)備
// let beacon = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, identifier: "BCTest")
// // 監(jiān)聽UUID為Beacon_Device_UUID,major為666,minor為999的唯一一個Beacon設(shè)備
// let beacon2 = CLBeaconRegion(uuid: UUID(uuidString: Beacon_Device_UUID)!, major: CLBeaconMajorValue(exactly: 666)!, minor: CLBeaconMinorValue(exactly: 999)!, identifier: "BCTest")
//應(yīng)用程序?qū)⒈粏硬⑼ㄟ^ locationManager:didDetermineState:forRegion 通知代理,當(dāng)設(shè)備的屏幕打開并且用戶在該區(qū)域時(shí)。 默認(rèn)情況下,這是 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)
// 在開始監(jiān)控之前,我們需要判斷改設(shè)備是否支持,和區(qū)域權(quán)限請求
let availableMonitor = CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
if availableMonitor {
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .notDetermined:
locationManager.requestAlwaysAuthorization()
case .denied:
print("權(quán)限受限制")
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startMonitoring(for: beaconRegion)
//此處beaconConstraint和創(chuàng)建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啟動時(shí)掃描")
default:
break
}
} else {
print("該設(shè)備不支持 CLBeaconRegion 區(qū)域檢測")
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
localNotification(title: "test", body: "位置更新了")
}
//當(dāng)授權(quán)狀態(tài)或accuracyAuthorization 屬性改變時(shí)調(diào)用
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
if #available(iOS 14.0, *) {
if manager.authorizationStatus == .authorizedAlways || manager.authorizationStatus == .authorizedWhenInUse{
//此處beaconConstraint和創(chuàng)建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和創(chuàng)建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
/** 進(jìn)入?yún)^(qū)域 */
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
localNotification(title: "test", body: "你已經(jīng)進(jìn)入監(jiān)控區(qū)域")
}
/** 離開區(qū)域 */
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
localNotification(title: "test", body: "你已經(jīng)離開監(jiān)控區(qū)域")
}
/** Monitoring有錯誤產(chǎn)生時(shí)的回調(diào) */
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
localNotification(title: "test", body: "Monitoring有錯")
}
/** Monitoring 成功回調(diào) */
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
localNotification(title: "test", body: "開始監(jiān)聽")
}
// pragma mark -- Ranging
/** 1秒鐘執(zhí)行1次 ,只要進(jìn)入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有錯誤產(chǎn)生時(shí)的回調(diào) */
func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
localNotification(title: "test", body: "rangingBeaconsDidFailFor")
}
/** 殺掉進(jìn)程之后的回調(diào),直接鎖屏亮鎖會觸發(fā),在ibeacon基站邊緣時(shí)會觸發(fā) */
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
//發(fā)送本地通知
print("jsc====app被ibeacon喚醒")
var str: String?
switch state.rawValue {
case 0:
str = "未知位置"
case 1:
str = "范圍內(nèi)"
case 2:
str = "范圍外"
default:
str = "其他"
}
localNotification(title: "test", body: "app被喚醒,位置信息: \(str!)")
}
// MARK: - 發(fā)送本地通知
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注冊通知來調(diào)試驗(yàn)證:
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("本地通知未打開,請授權(quán)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:注冊通知時(shí)注意了,如果是ios10.0以上,則必須實(shí)現(xiàn)func userNotificationCenter此方法,因?yàn)?code>center.delegate = self,否則是不會有通知下發(fā)的。