2011年4月18日 星期一

WiFi功能

WiFi功能
這邊簡單介紹WIFI開關的功能
    public class wifi extends Activity implements OnCheckedChangeListener{
        private WifiManager wifiManager;
        private WifiInfo wifiInfo;
        private CheckBox chkOpenCloseWifiBox;
        private List<WifiConfiguration> wifiConfigurations;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);
            // 獲得WifiManager對象
            wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
       
            // 獲得連接訊息
            wifiInfo = wifiManager.getConnectionInfo();
            chkOpenCloseWifiBox = (CheckBox) findViewById(R.id.chkOpenCloseWifi);
            TextView tvWifiConfigurations = (TextView) findViewById(R.id.tvWifiConfigurations);
            TextView tvWifiInfo = (TextView) findViewById(R.id.tvWifiInfo);       
            chkOpenCloseWifiBox.setOnCheckedChangeListener(this);
            // 根據目前WiFi狀態設置輸出訊息
            if (wifiManager.isWifiEnabled()) {
                chkOpenCloseWifiBox.setText("Wifi opened");
                chkOpenCloseWifiBox.setChecked(true);
            }
            else {
                chkOpenCloseWifiBox.setText("Wifi closed");
                chkOpenCloseWifiBox.setChecked(false);
            }
       
            // 輸出WIFI訊息
            StringBuffer sb = new StringBuffer();
            sb.append("Wifi Info\n");
            sb.append("MAC Address:" + wifiInfo.getMacAddress() + "\n");
            sb.append("SSID:" + wifiInfo.getSSID() + "\n");
            sb.append("IP Address(int):" + wifiInfo.getIpAddress() + "\n");
            sb.append("IP Address(Hex):" + Integer.toHexString(wifiInfo.getIpAddress()) + "\n");
            sb.append("IP Address:" + ipIntToString(wifiInfo.getIpAddress()) + "\n");
            sb.append("Network ID:" + wifiInfo.getNetworkId() + "\n");
            sb.append("Network Speed:" + Integer.toString(wifiInfo.getLinkSpeed()) + "\n");
            sb.append("Signal Strength:" + Integer.toString(wifiInfo.getRssi()) + "\n");
            tvWifiInfo.setText(sb.toString());
            // 得到配置好的網路
            wifiConfigurations = wifiManager.getConfiguredNetworks();
            tvWifiConfigurations.setText("Had connected wifi\n");
            for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
                tvWifiConfigurations.setText(tvWifiConfigurations.getText() + wifiConfiguration.SSID + "\n");
            }
        }

        // 將int型態的IP轉換成文字型態
        private String ipIntToString(int ip) {
            try {
                byte[] bytes = new byte[4];
                bytes[0] = (byte) (0xff & ip);
                bytes[1] = (byte) ((0xff00 & ip) >> 8);
                bytes[2] = (byte) ((0xff0000 & ip) >> 16);
                bytes[3] = (byte) ((0xff000000 & ip) >> 24);
                return Inet4Address.getByAddress(bytes).getHostAddress();
            }
            catch (Exception e) {
                return "";
            }
        }
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // 當選中複選框時打開WiFi
            if (isChecked) {
                wifiManager.setWifiEnabled(true);
                chkOpenCloseWifiBox.setText("Wifi opened");
            }
            // 當取消複選框時關閉WiFi
            else {
                wifiManager.setWifiEnabled(false);
                chkOpenCloseWifiBox.setText("Wifi closed");
            }
        }
    }

沒有留言:

張貼留言