这不是最直接的解决方案,但您可以通过 SSH 从平板电脑访问主机,因为主机通过 USB 连接到平板电脑并且具有静态 IP 地址192.168.100.80. 然后你可以使用ifconfig.
为了在 Java 中做到这一点,我使用了JSch,但任何 java SSH 实现都应该没问题。
安装 JSch
从这里下载 jsch-0.1.55.jar 。在您的应用程序目录中创建一个新文件夹libs并将 jar 保存在其中。然后将以下内容添加到依赖项下的 build.gradle 中:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
}
获取IP地址的命令
感谢这个答案,您可以对ifconfig输出进行一些处理,以获取 Pepper 头部在 wifi 网络上的 IP 地址。注意:我目前无法在物理 Pepper 上对此进行测试,因此请先通过 SSH 连接到 Pepper 并运行命令来检查这一点。主要检查wlan0网络设备的名称是否正确。
ifconfig wlan0 | grep 'inet addr' | cut -d ':' -f 2 | cut -d ' ' -f 1
在 Java 中运行此命令
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.InputStream;
// Create SSH session
jsch = new JSch();
session = jsch.getSession("nao", "192.168.100.80", 22);
session.setPassword("nao");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
String command = "ifconfig wlan0 | grep 'inet addr' | cut -d ':' -f 2 | cut -d ' ' -f 1";
session.connect();
ChannelExec channelssh = (ChannelExec)session.openChannel("exec");
channelssh.setCommand(command);
InputStream stdout = channelssh.getInputStream();
// Execute command
channelssh.connect();
// Get output
StringBuilder output = new StringBuilder();
int bytesRead = input.read();
while (bytesRead != -1) {
output.append((char) bytesRead);
bytesRead = input.read();
}
// close SSH channel
channelssh.disconnect();
// Here's the IP address of the head, formatted as a string
String headIP = output.toString();