Android 台灣中文網

標題: 在Android手機上是否能夠拿到DHCP Server Domain的資訊 [打印本頁]

作者: deep_dream    時間: 2011-12-16 11:11
標題: 在Android手機上是否能夠拿到DHCP Server Domain的資訊
本帖最後由 deep_dream 於 2011-12-16 11:21 編輯

我目前在開發Android相關的App...
想知道當手機連上網路之後(Wifi or 3G),通常會拿到一個IP,而這個IP通常都是由DHCP Server所配發下來的IP。
原理我想應該就跟一般的IP分享器 OR 無線基地台一樣。
我目前只知道由Wifi的方式來拿到DhcpInfo,不過也沒有Server Domain...
想知道有沒有任何寫過相關部分的前輩可以給我一點意見~~~

以下為我測試的程式~
  1. public class DhcpClientActivity extends Activity {
  2.         /** Called when the activity is first created. */

  3.         private TextView textViewRes;
  4.         private Button button01;

  5.         @Override
  6.         public void onCreate(Bundle savedInstanceState) {
  7.                 super.onCreate(savedInstanceState);
  8.                 setContentView(R.layout.main);

  9.                 textViewRes = (TextView) findViewById(R.id.textViewRes);

  10.                 button01 = (Button) findViewById(R.id.button01);
  11.                 button01.setOnClickListener(new OnClickListener() {

  12.                         @Override
  13.                         public void onClick(View arg0) {
  14.                                 // TODO Auto-generated method stub
  15.                                 WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
  16.                                 DhcpInfo d = wifiMgr.getDhcpInfo();

  17.                                 String s_dns1 = "";
  18.                                 String s_dns2 = "";
  19.                                 String s_gateway = "";
  20.                                 String s_ipAddress = "";
  21.                                 String s_leaseDuration = "";
  22.                                 String s_netmask = "";
  23.                                 String s_serverAddress = "";
  24.                                
  25.                                 s_dns1 = "DNS 1: " + intToIp(d.dns1);
  26.                                 s_dns2 = "DNS 2: " + intToIp(d.dns2);
  27.                                 s_gateway = "Default Gateway: " + intToIp(d.gateway);
  28.                                 s_ipAddress = "IP Address: " + intToIp(d.ipAddress);
  29.                                 s_leaseDuration = "Lease Time: " + String.valueOf(d.leaseDuration);
  30.                                 s_netmask = "Subnet Mask: " + intToIp(d.netmask);
  31.                                 s_serverAddress = "Server IP: "        + intToIp(d.serverAddress);

  32.                                 textViewRes.setText("Network Info\n" + s_dns1 + "\n" + s_dns2
  33.                                                 + "\n" + s_gateway + "\n" + s_ipAddress + "\n"
  34.                                                 + s_leaseDuration + "\n" + s_netmask + "\n"
  35.                                                 + s_serverAddress);
  36.                         }
  37.                 });
  38.         }

  39.         public String intToIp(int i) {
  40.                 return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
  41.                                 + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
  42.         }
  43. }
複製代碼
奇怪的還有一件事,拿到的IP都是反的,可是我覺得String intToIp(int i)裡面的操作應該是正確的才對。
假設拿到的ip是192.168.1.1
那麼就是
-----byte1-----,-----byte2-----,-----byte3-----,-----byte4-----
    11000000    ,   10101000    ,    00000001    ,    00000001   
經過運算之後轉成的才對。
不過我實際上拿到的都是1.1.168.192…這點讓人很不解…   




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1