iOS WiFi - NEHotspotHelper

最近項目有個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)
        }

注意

  1. NEHotspotNetwork.fetchCurrent的系統(tǒng)支持版本最低是iOS14.0,其中 securityType需要iOS15.0才能支持。
  2. 如果你按照上面操作,依然無法獲取signalStrength,返回一直為0. 這時你需要看下isChosenHelper返回是否為false。如果是,那么恭喜,我們需要去官方開發(fā)者平臺申請對應(yīng)權(quán)限,才能夠獲取。
  3. 獲取權(quán)限方式,參考NEHotspotHelper權(quán)限獲取方式,挺麻煩的,后面如果我通過了,再來反饋申請時間多長。
    4.最后提醒,如果我們的目的是想表明當前網(wǎng)絡(luò)好還是不好,蘋果工程師不建議我們通過使用信號值這種方式去表現(xiàn),建議通過文件測速的方式,去判斷當前網(wǎng)絡(luò)的有效可用性。相關(guān)論壇討論發(fā)言稿地址參考 。 當然,如果只是簡單的展現(xiàn)信號值,這種方式是可取的。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容