Overview of Wi-Fi direct
From android4.0 (API level = 14), allow direct connection between two mobile devices through the Wi-Fi module (this technology is called Wi-Fi direct ), this connection does not require wireless routing as an intermediary, but is a data transmission channel directly established between two devices like Bluetooth. The android SDK provides APIs to detect other devices that support Wi-Fi direct and receive Wi-Fi direct requests from other devices. Before the emergence of Wi-Fi direct, Bluetooth or wireless routing is usually used to connect multiple Android devices. The valid distance of Bluetooth is short, and the transmission rate is not as good as that of Wi-Fi. However, with Wi-Fi direct, the two devices can be directly connected, which is convenient and fast and suitable for data sharing.
Core Wi-Fi direct APIs
The Wi-Fi direct API consists of the following parts.
#Wifip2pmanager class. This class provides APIs that allow users to discover, request, and connect to other Wi-Fi direct devices.
#The broadcast receiver that listens to wi-fi direct requests.
#Events Detected by the Wi-Fi direct framework, such as terminating connections and discovering new Wi-Fi direct devices.
Wi-Fi Direct Broadcast Receiver
#Wifip2pmanager. wifi_p2p_state_changed_action: checks whether Wi-Fi is available and notifies the corresponding window of the detection result.
#Wifip2pmanager. wifi_p2p_peers_changed_action: Call the wifip2pmanager. requestpeers method to obtain the list of successfully connected devices.
#Wifip2pmanager. wifi_p2p_connection_changed_action: responds to the Wi-Fi connection status (connection or disconnection ).
#Wifip2pmanager. wifi_p2p_this_device_changed_action: responds to changes in the Wi-Fi status of the device.
Broadcast receiver authorization
<Uses-SDK Android: minsdkversion = "14"/>
<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. Internet"/>
<Uses-Permission Android: Name = "android. Permission. access_network_state"/>
Wi-Fi direct device found
To use Wi-Fi direct for communication, you must first search for other Wi-Fi direct devices. To do this, call the wifip2pmanager. discoverpeers method.
1 mchannel = mmanager. initialize (this, getmainlooper (), null); 2 mmanager. discoverpeers (mchannel, new wifip2pmanager. actionlistener () 3 {4 // Wi-Fi direct Device 5 @ override 6 Public void onsuccess () {7... 8} 9 // No Wi-Fi direct device found 10 @ override11 public void onfailure (INT reasoncode) {12... 13} 14 });
1 peerlistlistener mypeerlistener; 2... 3 if (wifip2pmanager. wifi_p2p_peers_changed_action.equals (Action) {4 5 If (mmanager! = NULL) 6 {7 // gets available Wi-Fi direct device 8 mmanager asynchronously. requestpeers (mchannel, mypeerlistlistener); 9} 10} 11 12 public interface peerlistlistener 13 {14 public void onpeersavailable (wifip2pdevicelist peers); 15}
Connect to a Wi-Fi Device
1 wifip2pdevice device; 2... ... // You need to obtain the corresponding device (wifip2pdevice object) from the device list 3 // create the wifip2pconfig object 4 wifip2pconfig Config = new wifip2pconfig (); 5 // the MAC address of the storage device 6 config. deviceaddress = device. deviceaddress; 7 // start to connect to the device 8 mmanager. connect (mchannel, config, new actionlistener () {9 // call this method 10 @ override11 public void onsuccess () when the device is successfully connected () {12 // success logic13} 14 call this method after the device fails to connect 15 @ override16 public void onfailure (INT reason) {17 // failure logic18} 19 });
Data Transmission
1 wifip2pmanager. requestconnectioninfo (...) 2 3 Public void requestconnectioninfo (channel C, connectioninfolistener listener); 4 5 6 public interface connectioninfolistener 7 {8 Public void onconnectioninfoavailable (wifip2pinfo info); 9} 10 11 12 wifip2pinfo. groupowneraddress. gethostaddress13 14 the client uses Socket to connect and the server uses serversocket. accept waiting for client connection