电脑在Android的USB绑定模式没有WIFI,编程方式的IP地址 [英] Ip address of PC in USB Tethering mode in Android without WIFI, Programatically

查看:145
本文介绍了电脑在Android的USB绑定模式没有WIFI,编程方式的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编程得到其Android手机连接在USB绑定模式的PC的IP地址,默认网关和端口号,而无需使用WIFI经理?

我使用的网络接口,但它不给我正确的信息,还有没有别的办法?

 的(列举<的NetworkInterface> EN = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements()){
            NetworkInterface的INTF = en.nextElement();
            对于(枚举< InetAddress类> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements()){
                InetAddress类InetAddress类= enumIpAddr.nextElement();
                如果(!inetAddress.isLoopbackAddress()){
                    。地址+ = inetAddress.getHostAddress()的toString();
                }
            }
        }
 

解决方案

确定这里是一轮关于我达到了,从通过网络接口接收的地址,我在致辞中删除最后一个部分,如192.168.1.40解决方案我取出40,并通过循环遍历从192.168.1.0出发,找出实际连接的端口。该片段是低于

 的(IDX = 0; IDX< = 255; IDX ++)
         {
             尝试
             {
                 T [IDX] =新主题(新的Runnable(){
                        公共无效的run(){
                            海峡= IPpart +。 + IDX;
                                            插座=新的Socket(STR,PORT);
                                   IP = socket.getInetAddress()的toString()。
                             网关= socket.getLocalAddress()的toString()。
                        }
                    });
                 T [IDX] .setName(IPclass);
                 T [IDX]。开始();
                 如果(IP!=)
                 {
                     closeThread();
                     打破;
                 }
             }赶上(例外前){}
         }
 

How can I programatically get the Ip address, default gateway and port number of the PC to which the android phone is connected in USB Tethering mode, without using WIFI Manager?

I used network interfaces, but it doesnt give me the correct information, is there any other way?

for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    address += inetAddress.getHostAddress().toString() ;
                }
            }
        }

解决方案

Ok here is the round about solution that I reached, from the address received through the network interface, I remove the last section in the address, like 192.168.1.40, i remove 40 and iterate through a loop starting from 192.168.1.0 and find out the port which actually connects. The snippet is below

for(idx=0; idx <=255; idx++)
         {
             try
             {
                 t[idx] = new Thread(new Runnable() {
                        public void run() {
                            str = IPpart + "." + idx;
                                            socket = new Socket(str, PORT);
                                   IP = socket.getInetAddress().toString();
                             Gateway = socket.getLocalAddress().toString();
                        }
                    });
                 t[idx].setName("IPclass");
                 t[idx].start();
                 if(IP != "")
                 {
                     closeThread();
                     break;
                 }
             }catch(Exception ex){ }
         }

这篇关于电脑在Android的USB绑定模式没有WIFI,编程方式的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆