1. 程式人生 > >macOS 開發 - CoreWlan 獲取 WiFi 資訊 (CWInterface、CWChannel、CWConfiguration)

macOS 開發 - CoreWlan 獲取 WiFi 資訊 (CWInterface、CWChannel、CWConfiguration)

文章目錄


核心程式碼

#import <CoreWLAN/CoreWLAN.h>
#import <SystemConfiguration/SystemConfiguration.h>
- (NSString *)returnNetWorkJudge{

    NSString *pingHost = @"abc.apple.com";
    SCNetworkConnectionFlags flags = 0;

    if (pingHost && [pingHost length] > 0) {
        flags = 0;
        BOOL found = NO;
        //判斷當前網路連線
        SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [pingHost UTF8String]);
        if (reachabilityRef) {
            found = SCNetworkReachabilityGetFlags(reachabilityRef, &flags)
            &&  (flags & kSCNetworkFlagsReachable)
            && !(flags & kSCNetworkFlagsConnectionRequired);
            CFRelease(reachabilityRef);
            reachabilityRef = NULL;
        }
        if (found) {
            NSLog(@"Connection established");

            NSLog(@"\n\n----- CWInterface \n\n")
        CWInterface* wifi = [[CWWiFiClient sharedWiFiClient] interface];

        NSLog(@"interfaceNames : %@",[CWWiFiClient interfaceNames]);
        /*
         interfaceNames : (
         en1
         )
         */

        NSLog(@"interfaceName: %@", wifi.interfaceName); //BSD if name: en1
        NSLog(@"powerOn: %d", wifi.powerOn);
        NSLog(@"ssid: %@", wifi.ssid);
        NSLog(@"bssid: %@", wifi.bssid);
        NSLog(@"rssiValue: %ld", wifi.rssiValue);
        NSLog(@"noiseMeasurement: %ld", wifi.noiseMeasurement);
        NSLog(@"transmitRate: %f", wifi.transmitRate);
        NSLog(@"countryCode: %@", wifi.countryCode);
        NSLog(@"interfaceMode: %ld", wifi.interfaceMode);
        NSLog(@"transmitPower: %ld", wifi.transmitPower);
        NSLog(@"hardwareAddress: %@", wifi.hardwareAddress);
        NSLog(@"serviceActive: %d", wifi.serviceActive);

        NSLog(@"security: %ld", wifi.security);
        NSLog(@"cachedScanResults: %@ \n", wifi.cachedScanResults);

            NSLog(@"\n\n----- CWChannel \n")
        CWChannel *channel = wifi.wlanChannel;
        NSLog(@"channel: %@",channel);
        NSLog(@"channelNumber: %ld",channel.channelNumber);
        NSLog(@"channelWidth: %ld",channel.channelWidth);
        NSLog(@"channelBand: %ld ",channel.channelBand);

            NSLog(@"\n\n----- CWConfiguration \n")
        CWConfiguration *config = wifi.configuration;
        NSLog(@"config: %@",config);
        NSLog(@"requireAdministratorForAssociation: %d",config.requireAdministratorForAssociation);
        NSLog(@"requireAdministratorForPower: %d",config.requireAdministratorForPower);
        NSLog(@"requireAdministratorForIBSSMode: %d",config.requireAdministratorForIBSSMode);
        NSLog(@"rememberJoinedNetworks: %d ",config.rememberJoinedNetworks);

            NSLog(@"\n\n----- CWNetworkProfile \n")
            NSOrderedSet *networkProfiles = config.networkProfiles;
            for (CWNetworkProfile *profile in networkProfiles) {
                NSLog(@"profile : %@",profile);
                NSLog(@"ssid : %@ \n\n",profile.ssid);

                NSLog(@"\n----- \n")
            }


        if (wifi.ssid.length > 0) {
            return @"WIFI";
        } else{
            return @"本地網路";
        }
        }
        if (!found) {
            NSLog(@"Connection not established");
            return @"無網路";
        }
    }
    return @"無網路";
}


執行結果

沒有開啟 WiFi 時(無論是否連線有線)

----- CWInterface

interfaceNames : (
    en1
)
interfaceName: en1
powerOn: 0
ssid: (null)
bssid: (null)
rssiValue: 0
noiseMeasurement: 0
transmitRate: 0.000000
countryCode: (null)
interfaceMode: 0
transmitPower: 0
hardwareAddress: d4:dc:cd:f2:35:3c
serviceActive: 1
security: 9223372036854775807

開啟wifi時

Connection established

----- CWInterface

interfaceNames : (
    en1
)
interfaceName: en1
powerOn: 1
ssid: PH
bssid: 
rssiValue: -63
noiseMeasurement: -93
transmitRate: 486.000000
countryCode: (null)
interfaceMode: 1
transmitPower: 1496
hardwareAddress: d4:dc:cd:f2:35:3c
serviceActive: 1
security: 9

cachedScanResults: {(
    <CWNetwork: 0x600000001ff0> [ssid=PH-SH-G, bssid=..., security=Open, rssi=-58, channel=<CWChannel: 0x604000003170> [channelNumber=3(2GHz), channelWidth={20MHz}], ibss=0],
    
    
)}

----- CWChannel

channel: <CWChannel: 0x6000000023d0> [channelNumber=44(5GHz), channelWidth={40MHz(+1)}]
channelNumber: 44
channelWidth: 2
channelBand: 2

----- CWConfiguration

config: <CWConfiguration: 0x600000034020>
requireAdministratorForAssociation: 0
requireAdministratorForPower: 0
requireAdministratorForIBSSMode: 0
rememberJoinedNetworks: 1

----- CWNetworkProfile

profile : SSID: PH-Dormitory (<4e657445 6173652d 446f726d 69746f72 79>)
Captive: No
Was Captive: No
Domain Name: (null)
Passpoint: No
Display Name: (null)
Roaming Consortium List: (null)
NAI Realm List: (null)
SP Roaming Enabled: No
Security: WPA2 Enterprise
Hidden: No
Possibly Hidden: No
System Mode: No
Roaming Profile Type: Multi
Personal Hotspot: No
BSSID List: (
        {
        "LEAKY_AP_BSSID" = "11:11:11:11:11:11";
        "LEAKY_AP_LEARNED_DATA" = <>;
    },
        ...
    }
)
Share Mode: NotSupported
User Role: Owner
PayloadUUID: (null)

ssid : PH-Dormitory

-----

profile : SSID: PH (<4e657445 6173652d 3547>)
Captive: No
Was Captive: No
Domain Name: (null)
Passpoint: No
Display Name: (null)
Roaming Consortium List: (null)
NAI Realm List: (null)
SP Roaming Enabled: No
Security: WPA/WPA2 Enterprise
Hidden: No
Possibly Hidden: No
System Mode: No
Roaming Profile Type: Multi
Personal Hotspot: No
BSSID List: (
        {
        "LEAKY_AP_BSSID" = "11:11:11:11:11:11";
        "LEAKY_AP_LEARNED_DATA" = <>;
    },
       ...
)
Share Mode: NotSupported
User Role: Owner
PayloadUUID: (null)

ssid : PH