WiFi Direct状态 [英] WiFi Direct status
问题描述
是否可以专门检查WiFi Direct是打开还是关闭? 我写了一个代码,无论是接入点还是WiFi Direct,它都只能更新wifi状态,无论它是否已连接
Is it possible to specifically check that whether WiFi Direct is On or Off ? I wrote a code which can only update about the wifi status that whether it is connected or not,no matter it is Access point or WiFi Direct
ConnectivityManager connManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
}
if (!mWifi.isConnected()) {
}
我想专门检查一下WiFi Direct状态.请帮忙.
I want to specifically check about the WiFi Direct status.Please help.
推荐答案
是的,有一种方法可以做到这一点.您可以使用此 WIFI_P2P_STATE_CHANGED_ACTION 进行检查启用或禁用.一些示例代码如下:
Yes, there is a way to do this. You use call this WIFI_P2P_STATE_CHANGED_ACTION to check if it is enabled or disabled. Some sample code is below:
首先,您需要一个广播接收者:
First, you need a broadcast reciever:
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private Channel mChannel;
private MyWiFiActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
MyWifiActivity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
}
然后,在广播接收者代码的OnRecieve
部分上,您具有以下代码:
Then, on the OnRecieve
part of your code for the broadcast reciever, you have this code:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Boolean isEnabled;
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
isEnabled = true;
} else {
isEnabled = false;
}
}
}
这篇关于WiFi Direct状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!