问题描述
我写一个使用Android手机为通过TCP / IP的远程控制的程序。这款手机承载热点网络设备通过了解SSID和密码连接到。然后它连接到套接字编程设备。我的问题是,我不知道如何在Android的在Java code找到连接设备的IP地址。下图显示了,我需要找到。下面的图片了向连接到网络的东西。 192.168.43.15和192.168.43.102
I am writing a program that is using an android phone as a remote control via TCP/IP. The phone hosts a hotspot network that the devices connect to by knowing the SSID and password. It then connects to the devices with socket programming. My problem is that I don't know how to find connected devices IP-addresses in the Java code in android. The picture below shows what I need to find. The picture below got to things connected to the network. 192.168.43.15 and 192.168.43.102
的有问题的设备
我可以看一下屏幕,知道IP和应用程序输入手动或硬code它来测试应用程序本身。但应用程序需要自动查找IP-地址。用户不应该需要这样的手动输入的东西。
I can look at the screen and know the IP and enter it manually or hard code it in the app to test the app itself. But the app need to find the IP-addresses automatically. The user should not need to enter something like this manually.
我试过this并没有为我工作。我在Android清单以下权限
I tried this and it did not work for me. I have the following permissions in the Android manifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
自从我在实时把更新的图片连接的设备时,设备的连接和断开的名单我想有一个更好的方式来找到他们,而不是在网络上尝试和ping每个地址,看看有什么答案。
Since the list of connected devices in the picture I took update in real time when devices connect and disconnect I assume there is a better way to find them than to try and ping every address in the network to see what answers.
推荐答案
您可以从下面的代码片段获得热点连接设备:
You can get connected devices from Hotspot from following snippet :
public void getListOfConnectedDevice() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
BufferedReader br = null;
boolean isFirstLine = true;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue;
}
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ipAddress = splitted[0];
String macAddress = splitted[3];
boolean isReachable = InetAddress.getByName(
splitted[0]).isReachable(500); // this is network call so we cant do that on UI thread, so i take background thread.
if (isReachable) {
Log.d("Device Information", ipAddress + " : "
+ macAddress);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
这篇关于发现从Java code连接到Android热点的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!