本文主要是介绍WIFI项目--Android 获取本机IP(wifi下),子网掩码,网关,dns等信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
近期在捣鼓一个小项目,利用wifi热点在两手机上传输文件。进度有点慢,下面是遇到的一些小问题:
1获取手机所连接的wifi路由器ip:
ublic class MainActivity extends Activity { private WifiManager my_wifiManager; private WifiInfo wifiInfo; private DhcpInfo dhcpInfo; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); my_wifiManager = ((WifiManager) getSystemService("wifi")); dhcpInfo = my_wifiManager.getDhcpInfo(); wifiInfo = my_wifiManager.getConnectionInfo(); tvResult = (TextView) findViewById(R.id.tvResult); } @Override protected void onResume() { super.onResume(); StringBuilder sb = new StringBuilder(); sb.append("网络信息:"); sb.append("\nipAddress:" + intToIp(dhcpInfo.ipAddress)); sb.append("\nnetmask:" + intToIp(dhcpInfo.netmask)); sb.append("\ngateway:" + intToIp(dhcpInfo.gateway)); sb.append("\nserverAddress:" + intToIp(dhcpInfo.serverAddress)); sb.append("\ndns1:" + intToIp(dhcpInfo.dns1)); sb.append("\ndns2:" + intToIp(dhcpInfo.dns2)); sb.append("\n"); System.out.println(intToIp(dhcpInfo.ipAddress)); System.out.println(intToIp(dhcpInfo.netmask)); System.out.println(intToIp(dhcpInfo.gateway)); System.out.println(intToIp(dhcpInfo.serverAddress)); System.out.println(intToIp(dhcpInfo.dns1)); System.out.println(intToIp(dhcpInfo.dns2)); System.out.println(dhcpInfo.leaseDuration); sb.append("Wifi信息:"); sb.append("\nIpAddress:" + intToIp(wifiInfo.getIpAddress())); sb.append("\nMacAddress:" + wifiInfo.getMacAddress()); tvResult.setText(sb.toString()); } private String intToIp(int paramInt) { return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "." + (0xFF & paramInt >> 24); }
}
项目思路:
思路一:手机A在创建热点后,开启tcp服务等待请求,手机B连上手机A的wifi热点后,B向A发送上线请求(单播)。
思路二:1.开启热点一方使用开启udp监听请求,并且发送上线请求(广播);接下来连上该热点的手机同样采用这个方式,1监听–2广播;这样的好处是大家都知道对方的存在,是真正意义的群聊。2.发送文件阶段,发送方A先向接收方B方发送文件的数量以及名称等参数,发送方A并建立TCP服务等待请求,接收方发送N个数量的请求,发送方A相应创建N个线程发送文件。
今天先写这么多。。。看看资料先
项目github连接:https://github.com/AriesJiang/WIFIAPComm
这篇关于WIFI项目--Android 获取本机IP(wifi下),子网掩码,网关,dns等信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!