尝试以编程方式将运行android的Raspberry pi连接到我的wifi网络时遇到问题。
如果我使用adb
命令,它将毫无问题地连接。
这是我的Logcat
2018-12-19 05:57:02.552 296-2336/system_process I/WifiService: addOrUpdateNetwork uid=10032
2018-12-19 05:57:02.552 296-2336/system_process I/addOrUpdateNetwork: uid = 10032 SSID "home8" nid=-1
2018-12-19 05:57:02.553 296-374/system_process E/WifiConfigManager: Cannot find network with networkId -1 or configKey "home8"WPA_PSK
2018-12-19 05:57:02.553 296-374/system_process E/WifiConfigManager: Cannot find network with configKey "home8"WPA_PSK
2018-12-19 05:57:02.641 296-374/system_process D/WifiConfigStore: Writing to stores completed in 84 ms.
2018-12-19 05:57:02.642 296-927/system_process I/WifiService: disconnect uid=10032
2018-12-19 05:57:02.643 296-445/system_process I/WifiService: enableNetwork uid=10032 disableOthers=true
2018-12-19 05:57:02.644 460-460/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
2018-12-19 05:57:02.644 460-460/? E/wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
2018-12-19 05:57:02.645 296-374/system_process D/WifiStateMachine: connectToUserSelectNetwork netId 5, uid 10032, forceReconnect = false
2018-12-19 05:57:02.687 296-374/system_process D/WifiConfigStore: Writing to stores completed in 36 ms.
2018-12-19 05:57:02.688 296-374/system_process E/WifiConfigManager: UID 10032 does not have permission to update configuration "home8"WPA_PSK
2018-12-19 05:57:02.688 296-374/system_process I/WifiStateMachine: connectToUserSelectNetwork Allowing uid 10032 with insufficient permissions to connect=5
2018-12-19 05:57:02.673 374-374/? I/WifiStateMachin: type=1400 audit(0.0:220): avc: denied { write } for name="tracing_on" dev="tracefs" ino=20136 scontext=u:r:system_server:s0 tcontext=u:object_r:debugfs_tracing:s0 tclass=file permissive=1
2018-12-19 05:57:02.673 374-374/? I/WifiStateMachin: type=1400 audit(0.0:221): avc: denied { open } for path="/sys/kernel/debug/tracing/instances/wifi/tracing_on" dev="tracefs" ino=20136 scontext=u:r:system_server:s0 tcontext=u:object_r:debugfs_tracing:s0 tclass=file permissive=1
2018-12-19 05:57:02.673 374-374/? I/WifiStateMachin: type=1400 audit(0.0:222): avc: denied { getattr } for path="/sys/kernel/debug/tracing/instances/wifi/tracing_on" dev="tracefs" ino=20136 scontext=u:r:system_server:s0 tcontext=u:object_r:debugfs_tracing:s0 tclass=file permissive=1
2018-12-19 05:57:02.689 296-374/system_process D/WifiStateMachine: CMD_START_CONNECT sup state DisconnectedState my state DisconnectedState nid=5 roam=false
2018-12-19 05:57:02.690 296-308/system_process I/WifiService: reconnect uid=10032
2018-12-19 05:57:02.690 188-188/? W/wificond: Scan is not started. Ignore abort request
2018-12-19 05:57:02.691 296-444/system_process I/WifiService: getConfiguredNetworks uid=10032
2018-12-19 05:57:02.691 296-374/system_process D/SupplicantStaIfaceHal: connectToNetwork "home8"WPA_PSK
2018-12-19 05:57:02.692 296-374/system_process I/SupplicantStaIfaceHal: addSupplicantStaNetwork via HIDL
2018-12-19 05:57:02.757 296-584/system_process I/WifiService: removeNetwork uid=10032
2018-12-19 05:57:02.791 460-460/? I/wpa_supplicant: wlan0: CTRL-EVENT-SCAN-FAILED ret=-16 retry=1
2018-12-19 05:57:02.849 296-374/system_process D/WifiConfigStore: Writing to stores completed in 89 ms.
2018-12-19 05:57:02.856 460-460/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
2018-12-19 05:57:02.856 460-460/? E/wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
2018-12-19 05:57:02.921 296-2336/system_process I/WifiService: getConnectionInfo uid=10032
我还给了它以下权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.things.permission.SET_TIME" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"
android:protectionLevel="signature|privileged" />
这是我的代码
private void connectToWifi(final String networkSSID, final String networkPassword) {
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = String.format("\"%s\"", networkSSID);
conf.preSharedKey = String.format("\"%s\"", networkPassword);
int netId = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}