Android wi-fi directly connected
Wi-fi direct connection is a new feature added in Android 4.0(API Level 14) or later. You can use the Wi-Fi direct connection related API to discover and connect to devices that support Wi-Fi direct connection. After the connection, devices can communicate with each other over a much longer distance than bluetooth
Summary of the API
- Wi-fi direct connection method
classWifiP2pManager
Provides methods to discover connected devices using associated interfaces for Wi-Fi direct connection
methods | describe |
---|---|
initialize() | Registered in the Wi-Fi framework, must be called before other methods |
connect() | Connect to another directly connected device |
cancelConnect() | Cancels the action of connecting |
requestConnectInfo() | Request information that has been connected |
createGroup() | Create a directly connected device group |
removeGroup() | Deletes the current setting group |
requestGroupInfo() | Request information about the current group |
discoverPeers() | Initial search |
requestPeers() | Request a list of discovered devices |
- Wi-fi connection monitoring
classWifiP2pManager
Also provides a lot of listening interfaces, timing notifications of the currentactivity
Related to searching and linking
The results of
interface | Related operations |
---|---|
WifiP2pManager.ActionListener | Related operations: connect(), cancelConnect(), createGroup(), removeGroup(), and discoverPeers() |
WifiP2pManager.ChannelListener | Related operations: initialize() |
WifiP2pManager.ConnectionInfoListener | Related operations: requestConnectInfo() |
WifiP2pManager.GroupInfoListener | Related operations: requestGroupInfo() |
WifiP2pManager.PeerListListener | Related operations: requestPeers() |
- Wi-fi directly connected
Intent
Intent | describe |
---|---|
WIFI_P2P_CONNECTION_CHANGED_ACTION | Triggered when the Wi-Fi connection status of the device changes |
WIFI_P2P_PEERS_CHANGED_ACTION | In the calldiscoverPeers() When triggered, can be calledrequestPeers() Method to update the device list |
WIFI_P2P_STATE_CHANGED_ACTION | Triggered when the status of the Wi-Fi direct connection changes |
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION | Triggered when the details of a directly connected Wi-Fi device change |
Create an application for direct Wi-Fi connection
-
Initialization Settings
First of all, make sure to set up the protocol to support wi-fi direct connection. If so, we can get an instance of WifiP2pManager, create and register the related broadcast. Android :minSdkVersion=”14″ Android :minSdkVersion=”14″
<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" />Copy the code
-
Initialize the WifiP2pManager instance and register the related broadcasts to listen for wi-fi direct connection status
private WifiP2pManager mManager; private Channel mChannel; private IntentFilter directFilter; private WiFiDirectReceiver directReceiver ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mManager = (WifiP2pManager)this.getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mManager.initialize(this.this.getMainLooper(), null); directReceiver = new WiFiDirectReceiver(mManager, mChannel, this); directFilter = new IntentFilter(); directFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); directFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); directFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); directFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); } // Register a broadcast listener @Override protected void onResume(a) { super.onResume(); this.registerReceiver(directReceiver, directFilter); } // Cancel registration @Override protected void onPause(a) { super.onPause(); this.unregisterReceiver(directReceiver); }Copy the code
Radio and accept
public class WiFiDirectReceiver extends BroadcastReceiver{
private WifiP2pManager mManager;
private Channel mChannel;
private MainActivity mActivity;
private PeerListListener mListener;
private WifiP2pConfig mConfig = new WifiP2pConfig();
public WiFiDirectReceiver(){}
public WiFiDirectReceiver(WifiP2pManager manager,Channel channel,MainActivity activity){
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("tag"."===============wifi direct action: "+action);
if(action.equals(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)){
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, - 1);
if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){
/ / open
}else if(state == WifiP2pManager.WIFI_P2P_STATE_DISABLED){
/ / close}}else if(action.equals(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)){}else if(action.equals(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)){}else if(action.equals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)){}}}Copy the code
-
Find equipment
Wifip2pmanager.wifi_p2p_state_changed_action is broadcast after the initialize() method is called. If the status of WifiP2pManager.WIFI_P2P_STATE_ENABLED is enabled, call the discoverPeers method in BroadcastReceiver, and call the onSuccess method if the device is found
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() { @Override public void onSuccess(a) { Log.e("tag"."===================discovery success"); } @Override public void onFailure(int reason) { Log.e("tag"."===================discovery failed"); }});Copy the code
Wifip2pmanager. WIFI_P2P_PEERS_CHANGED_ACTION if a device is found, the system fires the broadcast wifiP2pManager. WIFI_P2P_PEERS_CHANGED_ACTION, in which the requestPeers method is called to list all devices
if(null! = mManager){ mManager.requestPeers(mChannel,new WifiP2pManager.PeerListListener() { @Override public void onPeersAvailable(WifiP2pDeviceList peers) { Log.e("tag"."==================peers list size: "+peers.getDeviceList().size()); for(WifiP2pDevice device: peers.getDeviceList()){ Log.e("tag"."==================device addr: "+device.deviceName+" name: "+device.deviceName); }}}); }Copy the code
-
Connected devices
For discovered devices we can call the connect() method to connect. We need to initialize WifiP2pConfig and set the deviceAddress of config
private WifiP2pConfig mConfig = new WifiP2pConfig(); mConfig.deviceAddress = device.deviceAddress; mManager.connect(mChannel, mConfig, new WifiP2pManager.ActionListener() { @Override public void onSuccess(a) { Log.e("tag"."==============connnect success"); } @Override public void onFailure(int reason) { Log.e("tag"."=================connect failed"); }});Copy the code
A successful connection is called back to the onSuccess method
Data transmission to be continued…