2017-06-14 59 views
1

如何檢查Wi-Fi Direct是否在設備上激活?Android支票啓用Wi-Fi直連

我試圖檢查無線網絡連接方式如下:

WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
return wifiManager.isWifiEnabled(); 

但也有不支持同步的Wi-Fi和Wi-Fi直連(華爲Honor 4C設備,它關閉下載Wi-Fi,當你激活Wi-Fi Direct時),所以這種方式不起作用。

WIFI_P2P_STATE_CHANGED_ACTIONBroadcastReceiverthere是不能接受太多,因爲只有當我開始與Wi-Fi直接(如發現)一些動作會被觸發,但我需要之前檢查其狀態。

+0

的[wifi直連狀態(https://stackoverflow.com/questions/20034736/wifi-direct-status)可能的複製 – Basix

+0

檢查出來[https://stackoverflow.com/questions/21533616/is-it-possible-to-connect-two-or-more-wifi-direct-groups?rq=1](https://stackoverflow.com/questions/ 21533616/is-it-it-to-connect-two-or-more-wifi-direct-groups?rq = 1) –

回答

0

您應該可以使用ConnectivityManager獲取Wi-Fi的狀態。

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    if (mWifi.isConnected()) { 
     // Do whatever 
    } 

請檢查您的許可清單中ACCESS_NETWORK_STATE & & INTERNET。

+0

這是關於Wi-Fi,而不是Wi-Fi Direct。此方法已被棄用。 – Artyom

1

,你可以做到這一點WifiDirectBroadcastReceiver這樣的:

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver { 

private WifiP2pManager mManager; 
private WifiP2pManager.Channel mChannel; 

public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel) { 
    super(); 
    this.mManager = manager; 
    this.mChannel = channel; 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    Boolean isEnabled; 

    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { 
     // check if Wi-Fi is enabled 
     int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); 
     if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { 
      isEnabled = true; 
     } else { 
      isEnabled = false; 
     } 
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 
     // get list of peers by WifiP2pManager.requestPeers() 
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 
     // Respond to new connections 
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { 
     // on device's wifi state changes 
    } 
} 
} 
+0

這是一個來自答案的代碼,我鏈接的問題。我的情況是不可接受的。 – Artyom