最近項目有個Heatmap的功能,大致就是需要掃描附近WiFi網(wǎng)絡(luò),并且獲取他們的信號強度,BSSID, SSID、加密等級等的功能。
技術(shù)選型:
通過一頓搜索,得知WiFi可以通過NEHotspotHelper 去獲取對應(yīng)的信息。
支持的信息如下:
@available(iOS 9.0, *)
open class NEHotspotNetwork : NSObject {
/**
* @property SSID
* @discussion The SSID of the Wi-Fi network.
*/
@available(iOS 9.0, *)
open var ssid: String { get }
/**
* @property BSSID
* @discussion The BSSID of the Wi-Fi network.
*/
@available(iOS 9.0, *)
open var bssid: String { get }
/**
* @property securityType
* @discussion The security type of the Wi-Fi network.
*/
@available(iOS 15.0, *)
open var securityType: NEHotspotNetworkSecurityType { get }
@available(iOS 14.0, *)
open class func fetchCurrent(completionHandler: @escaping (NEHotspotNetwork?) -> Void)
@available(iOS 14.0, *)
open class func fetchCurrent() async -> NEHotspotNetwork?
}
@available(iOS 9.0, *)
extension NEHotspotNetwork {
/**
* @property signalStrength
* @discussion
* The signal strength for the Wi-Fi network. The value lies within
* the range 0.0 (weak/no signal) to 1.0 (strong signal).
*/
@available(iOS 9.0, *)
open var signalStrength: Double { get }
/**
* @property secure
* @discussion Indicates whether the network is secure
*/
@available(iOS 9.0, *)
open var isSecure: Bool { get }
/**
* @property autoJoined
* @discussion
* Indicates whether the network was joined automatically
* (YES) or joined by the user (NO).
*/
@available(iOS 9.0, *)
open var didAutoJoin: Bool { get }
/**
* @property justJoined
* @discussion
* Indicates whether the network was just joined. Useful in the
* Maintaining state to differentiate whether the Maintain command
* is for the initial join, or the subsequent periodic callback.
*/
@available(iOS 9.0, *)
open var didJustJoin: Bool { get }
/**
* @property chosenHelper
* @discussion
* Indicates whether the HotspotHelper is the chosen helper for
* the network. The NEHotspotNetwork must have been instantiated via a
* call to the +[NEHotspotHelper supportedNetworkInterfaces] method. This
* is useful to restore state after the HotspotHelper application is quit
* and restarted.
*/
@available(iOS 9.0, *)
open var isChosenHelper: Bool { get }
@available(iOS 9.0, *)
open func setConfidence(_ confidence: NEHotspotHelperConfidence)
@available(iOS 9.0, *)
open func setPassword(_ password: String)
}
基本符合我們的需求預(yù)期,于是開整:
if #available(iOS 13.0, *) {
Task {
if #available(iOS 14.0, *) {
NEHotspotNetwork.fetchCurrent { info in
self.wifiName = info?.ssid ?? ""
let signalStrength = info?.signalStrength
let helper = info?.isChosenHelper
result(true, self.wifiName, signalStrength ?? 0)
}
} else {
// Fallback on earlier versions
XCGLog.info("不支持當前系統(tǒng)版本")
result(false, "", 0)
}
}
} else {
// Fallback on earlier versions
XCGLog.info("不支持當前系統(tǒng)版本")
result(false, "", 0)
}
注意
-
NEHotspotNetwork.fetchCurrent的系統(tǒng)支持版本最低是iOS14.0,其中securityType需要iOS15.0才能支持。 - 如果你按照上面操作,依然無法獲取
signalStrength,返回一直為0. 這時你需要看下isChosenHelper返回是否為false。如果是,那么恭喜,我們需要去官方開發(fā)者平臺申請對應(yīng)權(quán)限,才能夠獲取。 - 獲取權(quán)限方式,參考NEHotspotHelper權(quán)限獲取方式,挺麻煩的,后面如果我通過了,再來反饋申請時間多長。
4.最后提醒,如果我們的目的是想表明當前網(wǎng)絡(luò)好還是不好,蘋果工程師不建議我們通過使用信號值這種方式去表現(xiàn),建議通過文件測速的方式,去判斷當前網(wǎng)絡(luò)的有效可用性。相關(guān)論壇討論發(fā)言稿地址參考 。 當然,如果只是簡單的展現(xiàn)信號值,這種方式是可取的。