我正在尝试搜索连接到我的设备所连接的同一wifi网络的设备和设备。我使用以下代码,但直到现在我还没有取得任何成功
new AsyncTask<Void, Void, Boolean>() {
URL[] urls;
URL proveUrl;
HttpURLConnection con;
private boolean next(int position){
if (position++ < urls.length){
try {
proveUrl = new URL(urls[position].getHost() + ":36000");
} catch (MalformedURLException e) {
Toast.makeText(SDCardImagesActivity.this, "Malformed URL", Toast.LENGTH_SHORT).show();
}
return true;
}else{
return false;
}
}
@Override
protected void onPreExecute() {
wifiManager.getScanResults().toArray(urls);
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
proveUrl =new URL(urls[0].getHost() + ":36000");
} catch (MalformedURLException e) {
Toast.makeText(SDCardImagesActivity.this, "Malformed URL", Toast.LENGTH_SHORT).show();
}
return prove();
}
@Override
protected void onProgressUpdate(Void... values) {
direccionIp = proveUrl.getHost();
super.onProgressUpdate(values);
}
private Boolean prove(){
int position = 0;
try {
con = (HttpURLConnection) proveUrl.openConnection();
BufferedInputStream req = new BufferedInputStream(con.getInputStream());
} catch (IOException e) {
if (next(position)){
position++;
prove();
}else{
return false;
}
}
return true;
}
};
有人知道怎么做吗?感谢