根据 getSSID
方法的文档:
Returns the service set identifier (SSID) of the current 802.11 network. If the SSID can be decoded as UTF-8, it will be returned surrounded by double quotation marks. Otherwise, it is returned as a string of hex digits. The SSID may be {@code null} if there is no network currently connected.
此方法的源代码在一定程度上符合文档(null
情况除外),因此我不会将其作为代码 fragment 附在此处,but here it is for API 21 .
看来我可以删除双重代码,但根据this SO question , WifiInfo
的 getSSID()
方法可以说Android 版本之间的实现可能不同。
关于 SSID 的非正式消息来源提到它是字母数字,但我发现了一个有争议的说法:根据 this source , SSID 可以包含双引号,所以它是一个有效的字符。
因此,UTF-8 编码的 SSID 要么用双引号括起来,要么不用。
问题:这是一种可靠方法来识别 SSID 是否在结尾和开头都包含双引号,或者它来自 android 的 getSSID
实现?
最佳答案
WifiInfo.getSSID() 返回字符串,因此您可以为此使用 String.matches 和 String.replace
检测:
String ssid = "\"MYSSID\"";
Boolean matches = ssid.matches("^\".*\"$");
Log.v("Check quotes", "Matching " + matches);
删除:
ssid = ssid.replace("\"", "");
关于Android 的 WifiInfo#getSSID() : is there a way to understand if SSID has double quotes by itself or is it Android created ones?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53284899/