本帖最后由 老衲玩IT 于 2013-8-25 15:56 编辑
- //在layout.xml中为 toggleButton设置onclick属性为onWifiButton
- public void onWifiButton(View view) {//按下之后
- toggleButton=(ToggleButton) view;
- Intent intent=new Intent(Settings.ACTION_WIFI_SETTINGS);
- startActivity(intent);
- wifiManager.setWifiEnabled(toggleButton.isChecked());
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- long timeout=10000;//10秒钟
- while (flag) {
- if(wifiManager.isWifiEnabled()){
- StringBuilder sb=new StringBuilder();
- WifiInfo wifiInfo=wifiManager.getConnectionInfo();
- sb.append("MAC地址:"+wifiInfo.getMacAddress()+"\n");
- sb.append("接入点BSSID:"+wifiInfo.getBSSID()+"\n");
- sb.append("IP地址(int):"+wifiInfo.getIpAddress()+"\n");
- sb.append("IP地址(hex):"+Integer.toHexString(wifiInfo.getIpAddress())+"\n");
- sb.append("IP地址:"+ipIntToString(wifiInfo.getIpAddress())+"\n");
- sb.append("连接速度:"+wifiInfo.getLinkSpeed()+"Mbps\n");
-
- sb.append("\n已配置的无线网络\n");
- for (int i = 0; i < wifiManager.getConfiguredNetworks().size(); i++) {
- WifiConfiguration wifiConfiguration=wifiManager.getConfiguredNetworks().get(i);
- sb.append(wifiConfiguration.SSID+
- ((wifiConfiguration.status==0)?"已连接":"未连接")+
- "\n");
- }
- handler.sendMessage(handler.obtainMessage(1, sb));
- break;
- }
- try {
- Thread.sleep(1000);
- timeout-=1000;
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(timeout<=0){
- if (!wifiManager.isWifiEnabled()) {
- tvWifi.setText("");
- toggleButton.setCheced(false);
- }else{
- toggleButton.setCheced(true);
- }
- }
- }
- }
- }).start();
- }
- private String ipIntToString(int ip) {
- try {
- byte[] bytes=new byte[4];
- bytes[0]=(byte) (0xff & ip);
- bytes[0]=(byte) ((0xff00 & ip)>>8);
- bytes[0]=(byte) ((0xff0000 & ip)>>16);
- bytes[0]=(byte) ((0xff000000 & ip)>>24);
- return Inet4Address.getByAddress(bytes).getHostAddress();
- } catch (Exception e) {
- return "";// TODO: handle exception
- }
-
- }
复制代码 |