电脑在Android的USB绑定模式没有WIFI,编程方式的IP地址绑定、模式、地址、方式

2023-09-07 09:26:02 作者:该滚的滚该留的留i

我如何编程得到其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();
                     打破;
                 }
             }赶上(例外前){}
         }
 

Google I O 汇总,又一个新的时代开始了

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){ }
         }

 
精彩推荐
图片推荐