当前位置:   article > 正文

Android-系统服务-WifiManager_android wifimanager

android wifimanager

0 快速索引表

  • 权限汇总

https://developer.android.google.cn/reference/android/Manifest.permission

/frameworks/base/core/res/AndroidManifest.xml

android.permission.ACCESS_WIFI_STATE

android.permission.CHANGE_WIFI_STATE

android.permission.LOCAL_MAC_ADDRESS

android.permission.ACCESS_FINE_LOCATION

  • 接口 汇总(5个)

https://developer.android.google.cn/reference/android/net/wifi/WifiManager

/packages/modules/Wifi/framework/java/android/net/wifi/WifiManager.java

android.net.wifi.WifiManager.setWifiEnabled

android.net.wifi.WifiManager.disableNetwork

android.net.wifi.WifiManager.enableNetwork

android.net.wifi.WifiManager.getWifiState

https://developer.android.google.cn/reference/android/net/wifi/WifiInfo

/packages/modules/Wifi/framework/java/android/net/wifi/WifiInfo.java

android.net.wifi.WifiInfo.getMacAddress

android.net.wifi.WifiInfo.getBSSID

  • adb shell svc wifi 命令汇总

adb shell svc wifi disable

adb shell svc wifi enable

  • adb shell cmd wifi 命令汇总

adb shell cmd wifi status

adb shell cmd wifi set-wifi-enabled disabled

adb shell cmd wifi set-wifi-enabled  enabled

  • adb shell dumpsys appops 命令汇总

adb shell dumpsys appops --op WIFI_SCAN

adb shell dumpsys appops --op CHANGE_WIFI_STATE


1 需求

  • 打开Wi-Fi
  • 关闭Wi-Fi
  • 获取Wi-Fi的MAC地址

2 权限

android.permission.ACCESS_WIFI_STATE

  • Added in API level 1
  • Protection level: normal
  1. 1982 <!-- Allows applications to access information about Wi-Fi networks.
  2. 1983 <p>Protection level: normal
  3. 1984 -->
  4. 1985 <permission android:name="android.permission.ACCESS_WIFI_STATE"
  5. 1986 android:description="@string/permdesc_accessWifiState"
  6. 1987 android:label="@string/permlab_accessWifiState"
  7. 1988 android:protectionLevel="normal" />

android.permission.CHANGE_WIFI_STATE

  • Added in API level 1
  • Protection level: normal
  1. 1990 <!-- Allows applications to change Wi-Fi connectivity state.
  2. 1991 <p>Protection level: normal
  3. 1992 -->
  4. 1993 <permission android:name="android.permission.CHANGE_WIFI_STATE"
  5. 1994 android:description="@string/permdesc_changeWifiState"
  6. 1995 android:label="@string/permlab_changeWifiState"
  7. 1996 android:protectionLevel="normal" />

android.permission.LOCAL_MAC_ADDRESS

  1. 5844 <!-- @SystemApi Allows applications to read the local WiFi and Bluetooth MAC address.
  2. 5845 @hide -->
  3. 5846 <permission android:name="android.permission.LOCAL_MAC_ADDRESS"
  4. 5847 android:protectionLevel="signature|privileged" />
  5. 5848 <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>

android.permission.ACCESS_FINE_LOCATION

  1. 1210 <!-- Allows an app to access precise location.
  2. 1211 Alternatively, you might want {@link #ACCESS_COARSE_LOCATION}.
  3. 1212 <p>Protection level: dangerous
  4. 1213 -->
  5. 1214 <permission android:name="android.permission.ACCESS_FINE_LOCATION"
  6. 1215 android:permissionGroup="android.permission-group.UNDEFINED"
  7. 1216 android:label="@string/permlab_accessFineLocation"
  8. 1217 android:description="@string/permdesc_accessFineLocation"
  9. 1218 android:backgroundPermission="android.permission.ACCESS_BACKGROUND_LOCATION"
  10. 1219 android:protectionLevel="dangerous|instant" />

3 接口

android.net.wifi.WifiManager.setWifiEnabled

  • Added in API level 1
  • Deprecated in API level 29
  • 8.0:无权限要求
  • 8.1:CHANGE_WIFI_STATE权限,protectionLevel是normal
  • 10:接口废弃,10及以后返回false;9及以前正常

  1. 4074 /**
  2. 4075 * Enable or disable Wi-Fi.
  3. 4076 * <p>
  4. 4077 * Applications must have the {@link android.Manifest.permission#CHANGE_WIFI_STATE}
  5. 4078 * permission to toggle wifi.
  6. 4079 *
  7. 4080 * @param enabled {@code true} to enable, {@code false} to disable.
  8. 4081 * @return {@code false} if the request cannot be satisfied; {@code true} indicates that wifi is
  9. 4082 * either already in the requested state, or in progress toward the requested state.
  10. 4083 * @throws {@link java.lang.SecurityException} if the caller is missing required permissions.
  11. 4084 *
  12. 4085 * @deprecated Starting with Build.VERSION_CODES#Q, applications are not allowed to
  13. 4086 * enable/disable Wi-Fi.
  14. 4087 * <b>Compatibility Note:</b> For applications targeting
  15. 4088 * {@link android.os.Build.VERSION_CODES#Q} or above, this API will always fail and return
  16. 4089 * {@code false}. If apps are targeting an older SDK ({@link android.os.Build.VERSION_CODES#P}
  17. 4090 * or below), they can continue to use this API.
  18. 4091 * <p>
  19. 4092 * Deprecation Exemptions:
  20. 4093 * <ul>
  21. 4094 * <li>Device Owner (DO), Profile Owner (PO) and system apps.
  22. 4095 * </ul>
  23. 4096 *
  24. 4097 * Starting with Build.VERSION_CODES#T, DO/COPE may set a user restriction
  25. 4098 * (DISALLOW_CHANGE_WIFI_STATE) to only allow DO/PO to use this API.
  26. 4099 */
  27. 4100 @Deprecated
  28. 4101 public boolean setWifiEnabled(boolean enabled) {
  29. 4102 try {
  30. 4103 return mService.setWifiEnabled(mContext.getOpPackageName(), enabled);
  31. 4104 } catch (RemoteException e) {
  32. 4105 throw e.rethrowFromSystemServer();
  33. 4106 }
  34. 4107 }

android.net.wifi.WifiManager.disableNetwork

  • Added in API level 1
  • Deprecated in API level 29
  1. 2958 /**
  2. 2959 * Disable a configured network. The specified network will not be
  3. 2960 * a candidate for associating. This may result in the asynchronous
  4. 2961 * delivery of state change events.
  5. 2962 *
  6. 2963 * Applications are not allowed to disable networks created by other
  7. 2964 * applications.
  8. 2965 *
  9. 2966 * @param netId the ID of the network as returned by {@link #addNetwork} or {@link
  10. 2967 * #getConfiguredNetworks}.
  11. 2968 * @return {@code true} if the operation succeeded
  12. 2969 *
  13. 2970 * @deprecated
  14. 2971 * a) See {@link WifiNetworkSpecifier.Builder#build()} for new
  15. 2972 * mechanism to trigger connection to a Wi-Fi network.
  16. 2973 * b) See {@link #addNetworkSuggestions(List)},
  17. 2974 * {@link #removeNetworkSuggestions(List)} for new API to add Wi-Fi networks for consideration
  18. 2975 * when auto-connecting to wifi.
  19. 2976 * <b>Compatibility Note:</b> For applications targeting
  20. 2977 * {@link android.os.Build.VERSION_CODES#Q} or above, this API will always fail and return
  21. 2978 * {@code false}.
  22. 2979 * <p>
  23. 2980 * Deprecation Exemptions:
  24. 2981 * <ul>
  25. 2982 * <li>Device Owner (DO), Profile Owner (PO) and system apps.
  26. 2983 * </ul>
  27. 2984 */
  28. 2985 @Deprecated
  29. 2986 public boolean disableNetwork(int netId) {
  30. 2987 try {
  31. 2988 return mService.disableNetwork(netId, mContext.getOpPackageName());
  32. 2989 } catch (RemoteException e) {
  33. 2990 throw e.rethrowFromSystemServer();
  34. 2991 }
  35. 2992 }

android.net.wifi.WifiManager.enableNetwork

  • Added in API level 1
  • Deprecated in API level 29
  1. 2909 /**
  2. 2910 * Allow a previously configured network to be associated with. If
  3. 2911 * <code>attemptConnect</code> is true, an attempt to connect to the selected
  4. 2912 * network is initiated. This may result in the asynchronous delivery
  5. 2913 * of state change events.
  6. 2914 * <p>
  7. 2915 * <b>Note:</b> Network communication may not use Wi-Fi even if Wi-Fi is connected;
  8. 2916 * traffic may instead be sent through another network, such as cellular data,
  9. 2917 * Bluetooth tethering, or Ethernet. For example, traffic will never use a
  10. 2918 * Wi-Fi network that does not provide Internet access (e.g. a wireless
  11. 2919 * printer), if another network that does offer Internet access (e.g.
  12. 2920 * cellular data) is available. Applications that need to ensure that their
  13. 2921 * network traffic uses Wi-Fi should use APIs such as
  14. 2922 * {@link Network#bindSocket(java.net.Socket)},
  15. 2923 * {@link Network#openConnection(java.net.URL)}, or
  16. 2924 * {@link ConnectivityManager#bindProcessToNetwork} to do so.
  17. 2925 *
  18. 2926 * Applications are not allowed to enable networks created by other
  19. 2927 * applications.
  20. 2928 *
  21. 2929 * @param netId the ID of the network as returned by {@link #addNetwork} or {@link
  22. 2930 * #getConfiguredNetworks}.
  23. 2931 * @param attemptConnect The way to select a particular network to connect to is specify
  24. 2932 * {@code true} for this parameter.
  25. 2933 * @return {@code true} if the operation succeeded
  26. 2934 *
  27. 2935 * @deprecated
  28. 2936 * a) See {@link WifiNetworkSpecifier.Builder#build()} for new
  29. 2937 * mechanism to trigger connection to a Wi-Fi network.
  30. 2938 * b) See {@link #addNetworkSuggestions(List)},
  31. 2939 * {@link #removeNetworkSuggestions(List)} for new API to add Wi-Fi networks for consideration
  32. 2940 * when auto-connecting to wifi.
  33. 2941 * <b>Compatibility Note:</b> For applications targeting
  34. 2942 * {@link android.os.Build.VERSION_CODES#Q} or above, this API will always fail and return
  35. 2943 * {@code false}.
  36. 2944 * Deprecation Exemptions:
  37. 2945 * <ul>
  38. 2946 * <li>Device Owner (DO), Profile Owner (PO) and system apps.
  39. 2947 * </ul>
  40. 2948 */
  41. 2949 @Deprecated
  42. 2950 public boolean enableNetwork(int netId, boolean attemptConnect) {
  43. 2951 try {
  44. 2952 return mService.enableNetwork(netId, attemptConnect, mContext.getOpPackageName());
  45. 2953 } catch (RemoteException e) {
  46. 2954 throw e.rethrowFromSystemServer();
  47. 2955 }
  48. 2956 }

android.net.wifi.WifiManager.getWifiState

  • Added in API level 1
  1. 4273 /**
  2. 4274 * Gets the Wi-Fi enabled state.
  3. 4275 * @return One of {@link #WIFI_STATE_DISABLED},
  4. 4276 * {@link #WIFI_STATE_DISABLING}, {@link #WIFI_STATE_ENABLED},
  5. 4277 * {@link #WIFI_STATE_ENABLING}, {@link #WIFI_STATE_UNKNOWN}
  6. 4278 * @see #isWifiEnabled()
  7. 4279 */
  8. 4280 public int getWifiState() {
  9. 4281 try {
  10. 4282 return mService.getWifiEnabledState();
  11. 4283 } catch (RemoteException e) {
  12. 4284 throw e.rethrowFromSystemServer();
  13. 4285 }
  14. 4286 }

android.net.wifi.WifiInfo.getMacAddress

  • Added in API level 1
  • 权限
    • android.permission.LOCAL_MAC_ADDRESS
    • android.permission.ACCESS_FINE_LOCATION
  1. 1079 /**
  2. 1080 * Returns the MAC address used for this connection.
  3. 1081 * @return MAC address of the connection or {@code "02:00:00:00:00:00"} if the caller has
  4. 1082 * insufficient permission.
  5. 1083 *
  6. 1084 * Requires {@code android.Manifest.permission#LOCAL_MAC_ADDRESS} and
  7. 1085 * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}.
  8. 1086 */
  9. 1087 public String getMacAddress() {
  10. 1088 return mMacAddress;
  11. 1089 }

android.net.wifi.WifiInfo.getBSSID

  •  Added in API level 1
  1. 851 /**
  2. 852 * Return the basic service set identifier (BSSID) of the current access point.
  3. 853 * <p>
  4. 854 * The BSSID may be
  5. 855 * <lt>{@code null}, if there is no network currently connected.</lt>
  6. 856 * <lt>{@code "02:00:00:00:00:00"}, if the caller has insufficient permissions to access the
  7. 857 * BSSID.<lt>
  8. 858 * </p>
  9. 859 *
  10. 860 * @return the BSSID, in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX}
  11. 861 */
  12. 862 public String getBSSID() {
  13. 863 return mBSSID;
  14. 864 }

4 示例

  1. // 更改 Wi-Fi 状态
  2. WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
  3. if (wifiManager.isWifiEnabled()) {
  4. wifiManager.setWifiEnabled(false);
  5. } else {
  6. wifiManager.setWifiEnabled(true);
  7. }
  8. // Wi-Fi MAC地址
  9. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  10. Log.i(TAG, "onClick: " + wifiInfo.getMacAddress());

5 adb shell svc wifi

adb shell svc help

adb shell svc wifi -h

 


6 adb shell cmd wifi 命令

adb shell ls /system/bin | findstr wifi

adb shell cmd -l | findstr wifi

adb shell cmd wifi -h


7 adb shell dumpsys appops

adb shell dumpsys appops --op WIFI_SCAN

  • APP_OP_WIFI_SCAN = 10;
  • android.Manifest.permission.ACCESS_WIFI_STATE,

adb shell dumpsys appops --op CHANGE_WIFI_STATE

  • APP_OP_CHANGE_WIFI_STATE = 71;
  •  Manifest.permission.CHANGE_WIFI_STATE,

参考资料

enums.proto - OpenGrok cross reference for /frameworks/proto_logging/stats/enums/app/enums.proto

AppOpsManager.java - OpenGrok cross reference for /frameworks/base/core/java/android/app/AppOpsManager.java


8 参考资料(权限)

https://developer.android.google.cn/reference/android/Manifest.permission

/frameworks/base/core/res/AndroidManifest.xml


参考资料(接口)

https://developer.android.google.cn/reference/android/net/wifi/WifiManager

/packages/modules/Wifi/framework/java/android/net/wifi/WifiManager.java

https://developer.android.google.cn/reference/android/net/wifi/WifiInfo

/packages/modules/Wifi/framework/java/android/net/wifi/WifiInfo.java


参考资料(指南)

WLAN 扫描功能概览  |  Android 开发者  |  Android Developers

唯一标识符最佳做法  |  Android 开发者  |  Android Developers

Android 10 功能和 API  |  Android 开发者  |  Android Developers

以 Android 10 或更高版本为目标平台的应用无法启用或停用 WLAN。WifiManager.setWifiEnabled()方法始终返回 false。

如果您需要提示用户启用或停用 WLAN,请使用设置面板。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/295518
推荐阅读
相关标签
  

闽ICP备14008679号