First, the application background

In the development of the Internet of Things, the WiFi function of the mobile phone needs to be turned on and connected to the corresponding WiFi hotspot of the device, and the WiFi name and password of the family are sent to the device through TCP connection or UDP broadcast. After the device is connected to the home WiFi name and password, the device can be connected to the home WiFi and registered to the cloud, so as to realize the cloud on the device. So in iOS, how do we get information about the WiFi that our phone is currently connected to?

Second, the premise

IOS to get mobile phone currently connected WiFi information, iOS13.0 must first open positioning, iOS14.0 before packaging precise location, details can refer to the iOS precise location, and then go to set the APP connected to any WiFi.

Iii. Acquisition method

1. Obtain the SSID– WiFi name

+ (NSString *)getWifiSSID { NSString *ssid = nil; CFArrayRef arrRef = CNCopySupportedInterfaces(); NSArray *ifs = ( __bridge id)arrRef; for (NSString *ifnam in ifs) { CFDictionaryRef dicRef = CNCopyCurrentNetworkInfo(( __bridge CFStringRef)ifnam); NSDictionary *info = ( __bridge id)dicRef; if (info[@"BSSID"]) { ssid = info[@"SSID"]; } if(dicRef ! =nil) { CFRelease(dicRef); } } if(arrRef ! = nil) { CFRelease(arrRef); } return ssid; }Copy the code

2. Obtain the MAC address of BSSID- WiFi

+ (NSString *)getWifiBSSID { NSString *bssid = @""; CFArrayRef arrRef = CNCopySupportedInterfaces(); NSArray *ifs = ( __bridge id)arrRef; for(NSString *ifnam in ifs) { CFDictionaryRef dicRef = CNCopyCurrentNetworkInfo(( __bridge CFStringRef)ifnam); NSDictionary *info = ( __bridge id)dicRef; if (info[@"BSSID"]) { bssid = info[@"BSSID"]; } if(dicRef ! = nil) { CFRelease(dicRef); } } if (arrRef ! = nil) { CFRelease(arrRef); } return bssid; }Copy the code

Four, write in the last key point

IOS provides quick access to the WiFi information currently connected to the mobile phone, which enables us to quickly set WiFi information and password to complete the networking of smart devices. However, this is only one of the ways, and the method of directly obtaining the wifi currently connected to the mobile phone also has the risk of network failure. At present, most devices only support the wifi in 2.4g band, and the WIFI equipment in 5G band cannot complete network operation, while the mobile phone can support both 2.4g and 5G. So if the phone is currently connected to 5G band WiFi, we can’t use it to connect the device to the Internet. Here are some other ways to set up wifi:

  • 1. Mobile phone input WiFi name: this method has a high error rate and is very unfriendly, but it can be used as a last-resort solution when the WiFi information connected to mobile phone cannot be obtained through the above method due to system reasons or user Settings (such as unenabled positioning or precise location);
  • 2. The module supports searching for nearby WiFi: in this way, the built-in networking module of the device should support searching for nearby WiFi hotspots, and it is better to support Bluetooth at the same time, so that the WiFi information searched by the device can be easily sent to the mobile app through Bluetooth connection. The mobile terminal can select and set the discovered hotspots. After the password is set, you can configure the network for the device. In this way, the mobile phone does not require whether to turn on the positioning, and does not even need to connect the mobile phone to WiFi, and do not need to consider whether the 5G band of WiFi, because the device can search the WiFi hotspot must also support the connection of WiFi hotspot; However, this method is expensive and requires the device to support both WiFi module and Bluetooth module.