This article was reprinted from: 52192631
I am engaged in Linux embedded development, Android WiFi control in the Android Java layer has done very mature, but I need to write a control WiFi program, need to write a bunch of Java, I did not understand Java, so decided to skip the Java framework and application layer, directly from the C framework layer.
1.Android WiFi Frame
Basic architecture of WiFi:
1, the WiFi user space program and library:
external/wpa_supplicant/
Build library libwpaclient.so and Daemons wpa_supplicant
2, hardware/libhardware_legary/wifi/is the WiFi management library
3, the JNI part:
Frameworks/base/core/jni/android_net_wifi_wifi.cpp
4. Java section:
frameworks/base/services/java/com/android/server/
frameworks/base/wifi/java/android/net/wifi/
5. WIFI Settings app is located at:
packages/apps/settings/src/com/android/settings/wifi/
6. WIFI Driver Module Wlan.ko
Wpa_supplicant via Wireless_ext interface and driver communication
7. WIFI Hardware Module
2.Hardware layer 2.1. Loading and unloading drive
Loading: Insmod/system/lib/modules/bcmdhd.ko iface_name=wlan0
Uninstall: Rmmod BCMDHD
2.2. Start Wpa_supplicant
Execute SetProp Ctl.start wpa_supplicant
Description: When executing the above command, the Android system will invoke the/INIT.MACALLAN.RC statement:
Service wpa_supplicant/system/bin/wpa_supplicant \
-iwlan0-dnl80211-c/data/misc/wifi/wpa_supplicant.conf \
-i/system/etc/wifi/wpa_supplicant_overlay.conf \
-o/data/misc/wifi/sockets \
-e/data/misc/wifi/entropy.bin [Email Protected]:wpa_wlan0
# We'll start as root and wpa_supplicant would switch to user WiFi
# After setting up the capabilities required for Wext
# User WiFi
# Group WiFi inet KeyStore
Class Main
Socket wpa_wlan0 Dgram 660 WiFi wifi
Disabled
OneShot
To prevent the wpa_supplicant from starting, the WiFi is not working properly, then turn off the wpa_supplicant before starting.
2.3. Close Wpa_supplicant
Execute SetProp ctl.stop wpa_supplicant
View more WIFI.C (\android\hardware\libhardware_legacy\wifi)
2.4. Search for APS
Perform wpa_cli-iwlan0 ifname=wlan0 scan start Search
Perform wpa_cli-iwlan0 ifname=wlan0 scan_result display search results
Description: This command differs from the standard (Linux system) Wpa_supplicant, which is performed by the standard:
Wpa_cli-iwlan0 scan is trimmed because of the wpa_supplicant tools of the Android system.
The search results are as follows:
Bssid/frequency/signal Level/flags/ssid
MAC address frequency signal encryption mode AP Name
30:fc:68:19:57:70 2462-41 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] aaaa
30:FC:68:72:36:D2 2462-44 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] Tp-link_dd
b8:20:e7:00:32:9e 2437-46 [Wpa-psk-ccmp][wpa2-psk-ccmp][ess] CCC
B8:20:e7:00:0c:ce 2422-46 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] dd
E0:05:C5:AC:6D:FC 2472-49 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] Tee
14:CF:92:6E:1D:5E 2437-50 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] Xdaf
Bc:46:99:73:1d:42 2412-80 [wpa-psk-ccmp][wpa2-psk-ccmp][ess] Testdfege
00:0c:43:30:52:a8 2437-43 [wep][ess] G5000_joykoln
b8:20:e7:00:0a:62 2437-65 [wep][ess] G5000_joykoln
28:f0:76:18:c1:16 2462-48 [ESS] Imacqiu\xe7\x9a\x84imac
2.5.wpa_supplicant.conf configuration file
Located in/data/misc/wifi/wpa_supplicant.conf
When WiFi is not enabled (Wpa_supplicant not on) file display:
Ctrl_interface=/data/misc/wifi/sockets
Disable_scan_offload=1
Update_config=1
Device_name=macallan
Manufacturer=nvidia
Model_name=macallan
Model_number=macallan
serial_number=jkd01v20030000022
Device_type=10-0050f204-5
Config_methods=physical_display Virtual_push_button
P2p_disabled=1
When you turn on WiFi, the file displays:
Ctrl_interface=/data/misc/wifi/sockets
Disable_scan_offload=1
Update_config=1
Device_name=macallan
Manufacturer=nvidia
Model_name=macallan
Model_number=macallan
serial_number=jkd01v20030000022
Device_type=10-0050f204-5
Config_methods=physical_display Virtual_push_button
P2p_disabled=1
network={
Ssid= "JET-1"
psk= "abcd1234"
Key_mgmt=wpa-psk
Priority=1
}
2.6. Restart Wpa_supplicant
Execute Wpa_cli-iwlan0 ifname=wlan0 Reconfigure
Note: When initializing startup Wpa_supplicant, the AP information must be obtained through the Scan_result command to the currently searched APS, and the AP is written to the configuration file, and then the RECONFIGURE command restarts wpa_supplicant.
2.7. Start DHCP and start shutdown network
Need to start the network before starting Wpa_supplicant
Perform netcfg wlan0 up
When you start DHCP, you need to set up IP when you are connected to the AP, typically using dynamic IP
Perform netcfg wlan0 DHCP
When you close the wpa_supplicant, you need to close the NIC network
Execute netcfg wlan0 down
Such a process is complete. Of course, there is also a need to write a small program to search all AP information, and put the AP information needed to connect, and write to the configuration file.
This approach is to bypass the Android Java framework and Java application layer, directly and C-frame communication control of the WiFi function example, so that the user can fully control the WiFi function, with the wired network card using automatic switching function.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 52192631
Control WiFi "Turn" in Android environment via C-Frame layer