猿问

WiFi getSSID() 总是返回“<unknown ssid>”

很多人已经问过这个问题,但是对他们有用的解决方案对我不起作用。我在 Android 8.1 和 Android 9.0 上测试过,无法获取 SSID 的名称。


用于获取 WiFi 状态变化的广播接收器:


private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION) ||

                intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION) ||

                intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) ||

                intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) ||

                intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

            setWifiIndicator();

        }

    }

};

接收器在 中注册onCreate和注销onDestroy,我认为这是正确的,并且在调用接收器时我看到了日志行。


在onCreate:


IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(ScannerService.ACTION_READ_SCANNER);

intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);

intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);

registerReceiver(mReceiver, intentFilter);

setWifiIndicator();

setWifiIndicator是这样实现的:


private void setWifiIndicator() {

    ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    //getApplicationContext solves memory leak issues prior to Android N (must use Application Context to get wifi system service.

    WifiManager wifiMgr = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    ImageView wifiIndicator = (ImageView)findViewById(R.id.wifi_indicator);

    TextView wifiSSID = (TextView)findViewById(R.id.wifi_ssid);


  

慕神8447489
浏览 409回答 2
2回答

阿晨1998

Wi-Fi 权限安卓 8.0 和安卓 8.1:成功调用 WifiManager.getScanResults() 需要以下任一权限:• ACCESS_FINE_LOCATION • ACCESS_COARSE_LOCATION • CHANGE_WIFI_STATE如果调用应用程序没有任何这些权限,则调用将失败并出现 SecurityException。安卓 9:成功调用 WifiManager.startScan() 需要满足以下所有条件:• 您的应用具有 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 权限。• 您的应用拥有 CHANGE_WIFI_STATE 权限。• 设备上启用了定位服务(在设置 > 位置下)。Android 10(API 级别 29)及更高版本:成功调用 WifiManager.startScan() 需要满足以下所有条件:• 如果您的应用面向Android 10(API 级别29)SDK 或更高版本,则您的应用拥有ACCESS_FINE_LOCATION 权限。• 如果您的应用面向低于 Android 10(API 级别 29)的 SDK,则您的应用具有 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限。• 您的应用拥有 CHANGE_WIFI_STATE 权限。• 设备上启用了定位服务(在设置 > 位置下)。并请求用户级别的权限来实现有关更多信息,请参阅此链接:https ://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-permissionsXAMARIN 表单代码更改:如果(Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O){状态 = 等待 CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);如果(状态!= PermissionStatus.Granted){&nbsp; &nbsp;var 结果 = 等待 CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Location });状态=结果[权限.位置];}}

MYYA

你有没有试过检查是否getSupplicantState()是COMPLETED?if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){&nbsp; &nbsp; &nbsp; &nbsp; String ssid = wifiInfo.getSSID();&nbsp; &nbsp; &nbsp; &nbsp; wifiSSID.setText(ssid);}另外,您在运行之前BroadcastReceiver或什至在内部都可以做到这一点:&nbsp;if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);}else{setWifiIndicator();&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答