最近做网络这块,需要获取wifi 局域网内在线。 以下为查资料所得
写的有点乱 , 有需要的朋友慢慢看吧
以通过测试:
public class WifiDemoActivity extends Activity {
privateButtonmTestButton;
private WifiManager mWifiManager;
private TextView iptextview;
staticintthreadCount= 0;
booleanisOver=false;
staticintpingSize= 0;
public ArrayList> ping; // ping 后的结果集
privateListViewshowiplistview;
public ArrayList> getPing() { // 用来得到ping后的结果集
returnping;
}
public void setPing(ArrayList> ping){
this.ping=ping;
}
@Override
protected void onPause() {
System.exit(0);
finish();
super.onPause();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ping = new ArrayList>();
iptextview = (TextView) findViewById(R.id.IPTEXTVIEWID);
mTestButton = (Button) findViewById(R.id.button1);
showiplistview = (ListView) findViewById(R.id.SHOWIPLISTVIEWID);
mTestButton.setOnClickListener(mAllClickListener);
mWifiManager = (WifiManager) this
.getSystemService(Context.WIFI_SERVICE);
}
@Override
protected void onResume() {
isOver = true;
mUpdaHandler.sendMessage(createMessage(10003, null));
super.onResume();
}
private View.OnClickListener mAllClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (ping.size() > 0) {
ping.clear();
((IpAdapter) showiplistview.getAdapter()).setIpData(ping);
}
new Thread() {
public void run() {
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
mWifiManager.getConfiguredNetworks();
List list = mWifiManager
.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : list) {
String ssid = wifiConfiguration.SSID;
String mSsid = ssid.substring(1, ssid.length() - 1);
String result = wifiConfiguration.toString();
if (mSsid.equalsIgnoreCase(wifiInfo.getSSID())) {
Log.d("Tag", wifiConfiguration.toString());
try {
int i = result.indexOf("LinkAddresses");
int i1 = result.indexOf("Routes");
String ipAddress = result
.substring(i, i1)
.trim()
.substring(
16,
result.substring(i, i1)
.length() - 6);
Log.d("Tag", ipAddress);
int i2 = result.indexOf("DnsAddresses");
String mWifiIntAddress = result
.substring(i2)
.trim()
.substring(
15,
result.substring(i2)
.length() - 4);
Log.d("Tag","WifiInetAddresses "
+ mWifiIntAddress);
String[] s = { mWifiIntAddress, ipAddress };
mUpdaHandler.sendMessage(createMessage(
10002, s));
PingAll(mWifiIntAddress);
} catch (Exception e) {
Log.e("Tag", "erro" + e);
}
}
}
}
}.start();
break;
default:
break;
}
}
};
public void PingAll(String hostAddress) {
// 首先得到本机的IP,得到网段
try {
int k = 0;
k = hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0, k + 1);
for (int i = 1; i <= 250; i++) { // 对所有局域网Ip
String iip = ss + i;
creatLog(WifiDemoActivity.class, "PingAll", iip, null);
Ping(iip);
}
// 等着所有Ping结束
while (isOver) {
Thread.sleep(3000);
if (threadCount == 0) {
isOver = false;
int k1 = 0;
String result="";
result=(String)ping.get(0).get("ip");
k1=result.lastIndexOf(".");
result=result.substring(k1+1,result.length());
int mResult=Integer.valueOf(result);
for(Map map:ping){
String s=(String) map.get("ip");
String s1= s.substring(k1+1,s.length());
int mS1=Integer.valueOf(s1);
mResult=mResult>mS1?mResult:mS1;
}
Log.d("Tag", ""+mResult);
mUpdaHandler.sendMessage(createMessage(10003, null));
}
creatLog(WifiDemoActivity.class, "pingall", "wait......", null);
}
} catch (Exception e) {
creatLog(WifiDemoActivity.class, "pingAll", "", e);
}
}
public void Ping(String ip) {
// 最多30个线程
try {
while (threadCount > 30) {
Thread.sleep(2);
}
creatLog(WifiDemoActivity.class, "ping", threadCount + "", null);
threadCount+= 1;
runPingIPprocess(ip);
} catch (Exception e) {
creatLog(WifiDemoActivity.class, "ping", null, e);
}
}
public void runPingIPprocess(final String ipString) {
final Thread pingThread = new Thread() {
@Override
public void run() {
try {
//ping ip地址
Process p = Runtime.getRuntime().exec(
"ping -c 1 -w 5 " + ipString);
int status = p.waitFor();
if (status == 0) {
Map map = new HashMap();
creatLog(WifiDemoActivity.class, "runpingIpProcesses",
"true" + ipString, null);
map.put("ip", ipString);
ping.add(map);
pingSize++;
if (pingSize > 10) {
pingSize = 0;
//setPing(ping);
mUpdaHandler
.sendMessage(createMessage(10001, ping));
Log.d("Tag","you are number");
}
creatLog(WifiDemoActivity.class, "run",
ping.toString(), null);
} else {
creatLog(WifiDemoActivity.class, "runpingIPprocess",
"false", null);
}
threadCount--;
creatLog(WifiDemoActivity.class, "run now ",
threadCount + "", null);
} catch (Exception e) {
threadCount--;
creatLog(WifiDemoActivity.class, "runPingIpprocess", null,
e);
} finally {
}
}
};
pingThread.start();
}
private Handler mUpdaHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
IpAdapter adapter ;
switch (msg.what) {
case 10001:
try {
Log.d("Tag", ping.toString() + "over");
ArrayList> pings = (ArrayList>) msg.obj;
adapter = (IpAdapter) showiplistview.getAdapter();
adapter.setIpData(getPing());
creatLog(WifiDemoActivity.class, "10001", pings + "", null);
adapter.notifyDataSetChanged();
isOver=true;
} catch (Exception e) {
creatLog(WifiDemoActivity.class, "1001", null, e);
}
break;
case 10002:
String[] s = (String[]) msg.obj;
iptextview.setText("本机IP:" + s[1] + "\n路由器IP:" + s[0]);
break;
case 10003:
adapter = new IpAdapter(WifiDemoActivity.this, ping);
showiplistview.setAdapter(adapter);
break;
default:
break;
}
}
};
public static Message createMessage(int what, Object object) {
Message message = new Message();
message.what = what;
message.obj = object;
return message;
}
public static void creatLog(Class class1, String where, String data,
Exception e) {
if (e != null) {
Log.e(class1.getSimpleName() + " "+where, "erro \n" + e);
} else if (e == null) {
Log.d(class1.getSimpleName() + " "+where + "", data + "");
}
}
}
如果哪位朋友有更好点方法,求分享哈撒