Android WIFI如何检测特定的WIFI连接可用
我需要检测何时可以与特定的WIFInetworkingbuild立networking连接。
例如:一旦你走进你的家,你的手机拿起你的家庭WiFinetworking,我想要一个通知,说:“你不在你的家庭networking,你想连接到你家吗? 但是,我希望只有当我在我家的时候才会发生。
我应该听什么,我应该做什么testing,以确保它是我的具体的家庭networking,而不是另一个networking?
你可以使用BroadcastReceiver来找出WiFinetworking已经改变了:
BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); context.registerReceiver(broadcastReceiver, intentFilter);
BroadcastReceiver可能看起来像这样。 要检查特定的MAC地址,请参阅下面的checkConnectedToDesiredWifi()方法。
public class WifiBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) { SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE); if (SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED) { boolean connected = checkConnectedToDesiredWifi(); } } } /** Detect you are connected to a specific network. */ private boolean checkConnectedToDesiredWifi() { boolean connected = false; String desiredMacAddress = "router mac address"; WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifi = wifiManager.getConnectionInfo(); if (wifi != null) { // get current router Mac address String bssid = wifi.getBSSID(); connected = desiredMacAddress.equals(bssid); } return connected; } }
只要我们不需要任何代码就可以获得免费服务 ,我只能推荐你,在创build这样的应用程序时,阅读关于Android及其networking/ Wifi可能性的一切可能。
- 你应该阅读和研究的来源
http://developer.android.com/reference/android/net/wifi/package-summary.html
如何看看WiFi是否连接到Android
如何让我的无线热点SSID在我目前的Android系统
如何从Android使用android API获取wifinetworking的名称?
在Android上获取Wifi接口名称
- 创build应用程序清单时应该要求的权限
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
( 最后一个,只有当你想要它检测你的位置,以防止不必要的电话 )
- 您还应该声明,您的应用程序需要WiFi才能在设备中使用,才能正常工作:
AndroidManifest.xml
<uses-feature android:name="android.hardware.wifi" />
使用标准代码列出所有可用的networking:
-
开始扫描
String connectivity_context = Context.WIFI_SERVICE; final WifiManager wifi = (WifiManager) getSystemService(connectivity_context); if (wifi.isWifiEnabled()) { wifi.startScan(); }
-
为数据注册一个接收器
IntentFilter i = new IntentFilter(); i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent i) { // TODO Auto-generated method stub ScanWiFiActivity a = ScanWiFiActivity.instance(); WifiManager w = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); List<ScanResult> l = w.getScanResults(); a.Clear(); for (ScanResult r : l) { //use r.SSID or r.BSSID to identify your home network and take action a.setText(r.SSID + "" + r.level + "\r\n"); } } }; registerReceiver(receiver, i);
在FOR块中工作你的魔法,并采取行动,当你通过SSID或BSSID识别您的networking
对于我的一个项目,我遇到了同样的问题,需要一段时间才能find解决scheme。
首先,“连接到WiFi”是非常抽象的,事实certificate是正确的。 在实践中,人们通常意味着以下全部:
- 通过WiFi接入点authentication
- 与接入点相关联
- 从networking上获得一个IP地址
所有这些阶段(和更多)与不同的Andoid事件相关联。 所以,不要紧张,这是我的(稍加修改)代码:
public class MyService extends Activity { // or Service //... Other stuff BroadcastReceiver awaitIPAddress = null; private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { if (intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE) == SupplicantState.COMPLETED) { //WiFi is associated WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wifiManager.getConnectionInfo(); if (wi != null) { // Wifi info available (should be, we are associated) if (wi.getIpAddress() != 0) { // Lucky us, we already have an ip address. // This happens when a connection is complete, eg after rekeying if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) { // ... Do your stuff here // ... // ... } } else { // No ip address yet, we need to wait... // Battery friendly method, using events if (awaitIPAddress == null) { awaitIPAddress = new BroadcastReceiver() { @Override public void onReceive(Context ctx, Intent in) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wifiManager.getConnectionInfo(); if (wi != null) { if (wi.getIpAddress() != 0) { if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) { // ... Do your stuff here // ... // ... } } } else { ctx.unregisterReceiver(this); awaitIPAddress = null; } } }; // We register a new receiver for connectivity events // (getting a new IP address for example) context.registerReceiver(awaitIPAddress, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } } } } else { // wifi connection not complete, release ip address receiver if registered if (awaitIPAddress != null) { context.unregisterReceiver(awaitIPAddress); awaitIPAddress = null; } } } } }; //... Other stuff @Override public void onCreate() { super.onCreate(); //... Other stuff IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); registerReceiver(receiver, filter); //... Other stuff } //... Other stuff }
另外,不要忽略清单的适当权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
我强烈怀疑你还需要:
<uses-permission android:name="android.permission.INTERNET"/>