Ip address of PC in USB Tethering mode in Android without WIFI, Programatically

后端 未结 1 970
南旧
南旧 2021-01-13 22:25

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 Man

相关标签:
1条回答
  • 2021-01-13 22:48

    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){ }
             }
    
    0 讨论(0)
提交回复
热议问题