gpt4 book ai didi

android - 如何获取所有已连接网络的 BSSID?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:51:42 24 4
gpt4 key购买 nike

我使用了下面的代码,但它运行良好,但几个月后我得到的结果是 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);

}
}

enter image description here

最佳答案

我也遇到了同样的问题。我在广播接收器的帮助下解决了它,并围绕它构建了我自己的逻辑。

Broadcast Receiver 类,确保在 list 中提供ACCESS_WIFI_STATECHANGE_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);
}
}

广播类的用户实现广播接收器类接口(interface),即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
});

关于android - 如何获取所有已连接网络的 BSSID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com