我需要检测何时有与特定WIFI网络的网络连接。
例如:一旦你走进你的房子,你的手机拿起你的家庭WiFi网络,我想要一条通知说:“你不在你的家庭网络,你想连接到你家吗? “但我希望这只会发生在我特定的房子里。
我应该注意什么以及我应该做些什么测试来确保它是我的特定家庭网络,而不是另一个网络?
答案 0 :(得分:28)
您可以使用BroadcastReceiver找出wifi网络已更改:
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;
}
}
答案 1 :(得分:12)
只要我们完全没有像您需要的代码,免费服务,我只能推荐您,在创建此类应用时阅读有关Android及其网络/ Wifi可能性的所有内容
http://developer.android.com/reference/android/net/wifi/package-summary.html
how to see if wifi is connected in android
How to get my wifi hotspot ssid in my current android system
How to get name of wifi-network out of android using android API?
Get Wifi Interface name on Android
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" />
(最后一个,只有当你想让它检测你的位置时,才能防止不必要的电话)
AndroidManifest.xml
<uses-feature android:name="android.hardware.wifi" />
答案 2 :(得分:4)
使用标准代码列出所有可用网络:
开始扫描
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
识别网络时,可以运行您的魔法并采取行动答案 3 :(得分:3)
我的项目遇到了完全相同的问题,需要一段时间才能找到解决方案。
首先,“连接到WiFi”非常抽象,事实证明是正确的。在实践中,人们通常意味着以下所有内容:
所有这些阶段(以及其他几个阶段)都与不同的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, e.g. 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"/>