我使用了以下代码,但效果很好,但是一个月后,我得到的结果为 any ,而不是 BSSID 的值。这是我的代码。请以其他替代方式指导我。
@SuppressLint("LongLogTag")
public void loadWifiAvailableList() {
WifiManager wifiMan = (WifiManager) getApplicationContext().getSystemService(
Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMan.getConnectionInfo();
String macAddr = wifiInfo.getMacAddress();
String bssid = wifiInfo.getBSSID();
//here i am getting the proper bssid
Log.d("bssid from get connection info",bssid);
List<WifiConfiguration> list = wifiMan.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.BSSID!=null)
//here i am getting any from i.BSSID
Log.d("bssid from get configured network",i.BSSID);
}
}
答案 0 :(得分:4)
我也遇到了同样的问题。我借助广播接收器解决了这个问题,并围绕它建立了自己的逻辑。
广播接收器类,请确保清单中提供的权限为 ACCESS_WIFI_STATE 和 CHANGE_WIFI_STATE 。
public class WifiChecker extends BroadcastReceiver {
private OnWifiResultArrived onWifiResultArrived = null;
private static boolean CAN_CALL_AGAIN = true;
private WifiManager wifiManager;
/**
* @param context context of activity.
* Remember to provide permission
* <p>
* {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />},
* {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />}</p>
*/
@SuppressLint("MissingPermission")
public WifiChecker(Context context) {
CAN_CALL_AGAIN = true;
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
context.registerReceiver(this, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
rerunAgain();
}
private void rerunAgain() {
new Handler().postDelayed(new Runnable() {
@SuppressLint("MissingPermission")
@Override
public void run() {
if (CAN_CALL_AGAIN)
wifiManager.startScan();
rerunAgain(); //rerun the broadcast again
}
}, 1000);
}
public void addListerForWifiCallback(OnWifiResultArrived onWifiResultArrived) {
this.onWifiResultArrived = onWifiResultArrived;
}
@SuppressLint("MissingPermission")
@Override
public void onReceive(Context context, Intent intent) {
updateUi(wifiManager.getScanResults());
}
private void updateUi(final List<ScanResult> scanResults) {
try {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
if (onWifiResultArrived != null)
onWifiResultArrived.isInWifiRange(scanResults);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
public void unregisterListner(Context context) {
this.onWifiResultArrived = null;
CAN_CALL_AGAIN = false;
}
public interface OnWifiResultArrived {
void isInWifiRange(List<ScanResult> scanResults);
}
}
广播类的用户 实现是广播接收器类的接口,即 OnWifiResultArrived
WifiChecker checker = new WifiChecker(this);
checker.addListerForWifiCallback(this);
@Override
public void isInWifiRange(List<ScanResult> scanResults){
//get your BSSID here
scanResults.get(position).BSSID;
//write your logic for checking weather it is connected or not
}
或
WifiChecker checker = new WifiChecker(this);
checker.addListerForWifiCallback(@Override
public void isInWifiRange(List<ScanResult> scanResults){
//get your BSSID here
scanResults.get(position).BSSID;
//write your logic for checking weather it is connected or not
});
答案 1 :(得分:2)
要获取当前连接的WIFI网络的BSSID,请使用WiFiInfo类。
WifiManager wifiMan = (WifiManager) context.getSystemService(
Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMan.getConnectionInfo();
String macAddr = wifiInfo.getMacAddress();
String bssid = wifiInfo.getBSSID();
答案 2 :(得分:0)
我找到了一些解决方案,为什么我在android的 getConfiguredNetworks() api中得到任何或空字符串。背后的原因是,在某些情况下,如果我们使用相同的SSID和相同的密码,则会以 any或null 字符串的形式发送。因此,当您尝试使用相同的ssid连接时, getConfiguredNetworks()不是正确的方法。因此请使用 getScanResults() Api而不是 getConfiguredNetworks()。经过一些自己的研究,我发现了。仅当您获得具有 ssid和密码的 AP 列表时,才会出现此问题。使用扫描结果并在wifi配置中添加该网络,然后进行连接,但 API级别26 中不推荐使用此方法。因此它无法在api 26级中使用。 reference link
//https://stackoverflow.com/a/8818490
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//checking the right network.
List<ScanResult> scanresults = wifiManager.getScanResults();
{
for (ScanResult scanresult : scanresults) {
Log.d("scan result1", scanresult.BSSID + "");
}}
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.BSSID = Bssid;
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
///this wont work from api level 26
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.BSSID != null && i.BSSID.equals(Bssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reassociate();
Log.d("changing network", "connecting the right network");
break;
}
}