我怎么知道 wifi 连接有效?
How do I know the wifi connection worked?
终于自己找到了答案,请看下面我的答案
我目前正在启动 wifi 扫描
wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
并在我的广播接收器中连接到 wifi
class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("DEBUG", "Wifi scanned");
SmartWifiChange(); //connect to certain wifi if stuff.
unregisterReceiver(this);
stopSelf(); //end of a running service
}
}
SmartWifiChange() 部分:
WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
success = wifiManager.enableNetwork(wifiToConnect.networkId, true);
if(success)
Toast.makeText(getApplicationContext(), "Now connected to: "
+resultToConnect.SSID,Toast.LENGTH_LONG).show();
else presentWifis.remove(resultToConnect);
我真的想这样做,然后检查我是否真的连接上了,如果没有连接上,再试一次。现在,我只是 do{
ing 这个 }while(!success)
,但似乎成功总是正确的,无论连接是否有效。
最后,我怎样才能询问或广播有关 wifi 连接是否有效的信息?
编辑:
找到ConnectivityManager.NetworkCallback
并想在这里使用它,我只需要正确理解它:
最后,我想知道我当前命令的连接是否失败,以便我继续前进。所以我实际上需要一种 onConnectionFailed()。如果理解给定的回调,也许我仍然可以构建它。
通常,当通过 OS 手动连接到网络时,您会得到关于 "connecting..." 和 "authentification failed" 的状态更新。这正是我所需要的 "failed" 消息。在这些回调中,我基本上得到了 onAvailable()、onLost() 和 onLosing()...如何使用它们来获取我的 "authentification failed" 消息?
编辑 2:
经过一个小时的谷歌搜索之后,我实际上找到了一个可能的解决方案,我将尝试利用它然后报告回来:
Wifi Authentication Error in Android
编辑 3:尝试过该解决方案,似乎是正确的,但由于某种原因不起作用。我仍然不明白这个连接的东西足以说明为什么。我没有想法,所以这是我的代码和 LogCat; WLAN-R44 是我的,我将其更改为密码错误,因此希望我的 phone 连接到我邻居的 "Fritzbox...bla..." Wifi,我的 phone 知道它,但是甚至从来没有在这里接受审判:
接收方:
class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction()) {
case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
Log.e("DEBUG", "Wifi scanned");
unregisterReceiver(this);
if(SmartWifiChange(true)){
registerReceiver(this, new IntentFilter(WifiManager
.SUPPLICANT_STATE_CHANGED_ACTION));
}else {
Log.e("Debug", "Off");
stopSelf();
}
break;
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
Log.e("Debug", "State changed action");
Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING)
Log.e("Debug", "Error authenticating");
if (!SmartWifiChange(false)){
unregisterReceiver(this);
Log.e("AfterError", "Off");
stopSelf();
}
else if ((intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
Log.e("Debug", "Completed");
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
unregisterReceiver(this);
Log.e("Completed", "Off");
stopSelf();
}else{
Toast.makeText(context, "Problem", Toast.LENGTH_SHORT).show();
Log.e("Problem", intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
Log.e("Problem", "Off");
unregisterReceiver(this);
stopSelf();
}
break;
default:
stopSelf();
}
}
}
连接方式:
private boolean SmartWifiChange(boolean success) {
boolean connectToWifi = true;
parseWifis();
//Keine bekannten Wifis vorhanden
if (presentWifis.isEmpty()){
wifiManager.setWifiEnabled(false); //WLAN aus
Toast.makeText(getApplicationContext(), "No known Wifis -> mobile data",
Toast.LENGTH_LONG).show();
setMobileData(true);//Datenverbindung an
connectToWifi=false;
}else{
if(success) {
ScanResult[] keysArray = presentWifis.keySet().toArray(new ScanResult[presentWifis
.keySet().size()]);
resultToConnect = keysArray[0];
for (int i = 1; i < presentWifis.size(); i++) {
if (keysArray[i].level > resultToConnect.level) {
resultToConnect = keysArray[i];
}
}
WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
wifiManager.enableNetwork(wifiToConnect.networkId, true);
Toast.makeText(getApplicationContext(), "Now connecting to: " + resultToConnect
.SSID,
Toast.LENGTH_SHORT).show();
Log.e("Debug", resultToConnect.SSID);
}
else {
Log.e("Debug", "Neuer Versuch");
presentWifis.remove(resultToConnect);
connectToWifi=SmartWifiChange(true);
}
if(connectToWifi)
setMobileData(false);
}
return connectToWifi;
}
LogCat:
04-21 15:39:30.025 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Service started
04-21 15:39:30.057 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:32.828 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Wifi scanned
04-21 15:39:33.105 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.230 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.237 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.244 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ State changed action
04-21 15:39:33.245 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ New state: FOUR_WAY_HANDSHAKE
04-21 15:39:33.263 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ Neuer Versuch
04-21 15:39:33.280 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.285 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.285 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.286 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.288 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.291 16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ FOUR_WAY_HANDSHAKE
04-21 15:39:33.291 16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ Off
我想检查您是否有 WiFi 的最简单方法是实际检索页面
来自互联网,例如 Google 的主页。我通常在我的应用程序中这样做
测试我是否有连接。
class TestConnectionTask extends AsyncTask<Void, Void, Boolean>
{
private String mUrl;
public TestConnectionTask(String url)
{
this.mUrl = url;
}
@Override
protected Boolean doInBackground(Void... params)
{
try
{
URL urlConnection = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return Boolean.TRUE;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean result)
{
if ( result != null)
{
// Connection was successful
// Do something here
}
super.onPostExecute(result);
}
}
然后在您的代码中的某处您可以这样调用它:
new TestConnectionTask("http://www.google.com").execute();
所以,在尝试了很多不成功的事情之后,我终于找到了答案:
为了按预期停止服务,我的 onReceive 现在几乎只做这个:
public void onReceive(Context context, Intent intent) {
Log.e("DEBUG", "Received");
try {
switch(intent.getAction()) {
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
Log.e("Debug", "State changed action");
Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING) {
Log.e("Debug", "Error authenticating");
if (!SmartWifiChange(false)) { //retry with next available Wifi
unregisterReceiver(this);
Log.e("AfterError", "Off");
stopSelf();
}
}
else if ((intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
Log.e("Debug", "Completed");
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
unregisterReceiver(this);
Log.e("Completed", "Off");
stopSelf();
}
break;
default:
stopSelf();
}
} catch (Exception e) {
e.printStackTrace();
unregisterReceiver(this);
stopSelf();
}
}
现在我的 Smart Wifi Widget 真的很好用,而且有人真的为我支付了一个开发帐户,它在 Play 商店是免费的,因为没有类似的东西,但应该有.
终于自己找到了答案,请看下面我的答案
我目前正在启动 wifi 扫描
wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
并在我的广播接收器中连接到 wifi
class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("DEBUG", "Wifi scanned");
SmartWifiChange(); //connect to certain wifi if stuff.
unregisterReceiver(this);
stopSelf(); //end of a running service
}
}
SmartWifiChange() 部分:
WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
success = wifiManager.enableNetwork(wifiToConnect.networkId, true);
if(success)
Toast.makeText(getApplicationContext(), "Now connected to: "
+resultToConnect.SSID,Toast.LENGTH_LONG).show();
else presentWifis.remove(resultToConnect);
我真的想这样做,然后检查我是否真的连接上了,如果没有连接上,再试一次。现在,我只是 do{
ing 这个 }while(!success)
,但似乎成功总是正确的,无论连接是否有效。
最后,我怎样才能询问或广播有关 wifi 连接是否有效的信息?
编辑:
找到ConnectivityManager.NetworkCallback
并想在这里使用它,我只需要正确理解它:
最后,我想知道我当前命令的连接是否失败,以便我继续前进。所以我实际上需要一种 onConnectionFailed()。如果理解给定的回调,也许我仍然可以构建它。
通常,当通过 OS 手动连接到网络时,您会得到关于 "connecting..." 和 "authentification failed" 的状态更新。这正是我所需要的 "failed" 消息。在这些回调中,我基本上得到了 onAvailable()、onLost() 和 onLosing()...如何使用它们来获取我的 "authentification failed" 消息?
编辑 2:
经过一个小时的谷歌搜索之后,我实际上找到了一个可能的解决方案,我将尝试利用它然后报告回来:
Wifi Authentication Error in Android
编辑 3:尝试过该解决方案,似乎是正确的,但由于某种原因不起作用。我仍然不明白这个连接的东西足以说明为什么。我没有想法,所以这是我的代码和 LogCat; WLAN-R44 是我的,我将其更改为密码错误,因此希望我的 phone 连接到我邻居的 "Fritzbox...bla..." Wifi,我的 phone 知道它,但是甚至从来没有在这里接受审判:
接收方:
class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction()) {
case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
Log.e("DEBUG", "Wifi scanned");
unregisterReceiver(this);
if(SmartWifiChange(true)){
registerReceiver(this, new IntentFilter(WifiManager
.SUPPLICANT_STATE_CHANGED_ACTION));
}else {
Log.e("Debug", "Off");
stopSelf();
}
break;
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
Log.e("Debug", "State changed action");
Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING)
Log.e("Debug", "Error authenticating");
if (!SmartWifiChange(false)){
unregisterReceiver(this);
Log.e("AfterError", "Off");
stopSelf();
}
else if ((intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
Log.e("Debug", "Completed");
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
unregisterReceiver(this);
Log.e("Completed", "Off");
stopSelf();
}else{
Toast.makeText(context, "Problem", Toast.LENGTH_SHORT).show();
Log.e("Problem", intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
Log.e("Problem", "Off");
unregisterReceiver(this);
stopSelf();
}
break;
default:
stopSelf();
}
}
}
连接方式:
private boolean SmartWifiChange(boolean success) {
boolean connectToWifi = true;
parseWifis();
//Keine bekannten Wifis vorhanden
if (presentWifis.isEmpty()){
wifiManager.setWifiEnabled(false); //WLAN aus
Toast.makeText(getApplicationContext(), "No known Wifis -> mobile data",
Toast.LENGTH_LONG).show();
setMobileData(true);//Datenverbindung an
connectToWifi=false;
}else{
if(success) {
ScanResult[] keysArray = presentWifis.keySet().toArray(new ScanResult[presentWifis
.keySet().size()]);
resultToConnect = keysArray[0];
for (int i = 1; i < presentWifis.size(); i++) {
if (keysArray[i].level > resultToConnect.level) {
resultToConnect = keysArray[i];
}
}
WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
wifiManager.enableNetwork(wifiToConnect.networkId, true);
Toast.makeText(getApplicationContext(), "Now connecting to: " + resultToConnect
.SSID,
Toast.LENGTH_SHORT).show();
Log.e("Debug", resultToConnect.SSID);
}
else {
Log.e("Debug", "Neuer Versuch");
presentWifis.remove(resultToConnect);
connectToWifi=SmartWifiChange(true);
}
if(connectToWifi)
setMobileData(false);
}
return connectToWifi;
}
LogCat:
04-21 15:39:30.025 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Service started
04-21 15:39:30.057 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:32.828 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Wifi scanned
04-21 15:39:33.105 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.230 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.237 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.244 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ State changed action
04-21 15:39:33.245 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ New state: FOUR_WAY_HANDSHAKE
04-21 15:39:33.263 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ Neuer Versuch
04-21 15:39:33.280 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.285 16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.285 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.286 16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.288 16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.291 16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ FOUR_WAY_HANDSHAKE
04-21 15:39:33.291 16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ Off
我想检查您是否有 WiFi 的最简单方法是实际检索页面 来自互联网,例如 Google 的主页。我通常在我的应用程序中这样做 测试我是否有连接。
class TestConnectionTask extends AsyncTask<Void, Void, Boolean>
{
private String mUrl;
public TestConnectionTask(String url)
{
this.mUrl = url;
}
@Override
protected Boolean doInBackground(Void... params)
{
try
{
URL urlConnection = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return Boolean.TRUE;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean result)
{
if ( result != null)
{
// Connection was successful
// Do something here
}
super.onPostExecute(result);
}
}
然后在您的代码中的某处您可以这样调用它:
new TestConnectionTask("http://www.google.com").execute();
所以,在尝试了很多不成功的事情之后,我终于找到了答案:
为了按预期停止服务,我的 onReceive 现在几乎只做这个:
public void onReceive(Context context, Intent intent) {
Log.e("DEBUG", "Received");
try {
switch(intent.getAction()) {
case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
Log.e("Debug", "State changed action");
Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE).toString());
if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING) {
Log.e("Debug", "Error authenticating");
if (!SmartWifiChange(false)) { //retry with next available Wifi
unregisterReceiver(this);
Log.e("AfterError", "Off");
stopSelf();
}
}
else if ((intent.getParcelableExtra(WifiManager
.EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
Log.e("Debug", "Completed");
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
unregisterReceiver(this);
Log.e("Completed", "Off");
stopSelf();
}
break;
default:
stopSelf();
}
} catch (Exception e) {
e.printStackTrace();
unregisterReceiver(this);
stopSelf();
}
}
现在我的 Smart Wifi Widget 真的很好用,而且有人真的为我支付了一个开发帐户,它在 Play 商店是免费的,因为没有类似的东西,但应该有.