我正在使用本地网络扫描仪。今天我用这个获取所有的ip和mac地址:
@Override
protected List<Camera> doInBackground(String... params) {
for (int i = 0; i < 255 && !isCancelled(); i++) {
String ip = params[0] + String.valueOf(i);
try {
Runtime.getRuntime().exec("ping -b " + ip);
} catch (IOException e) {
if (Constants.ENABLE_LOGS) {
e.printStackTrace();
}
Log.e(getClass().getName(), e.getMessage());
}
}
return searchCameras();
}
private List<Camera> searchCameras() {
List<Camera> cams = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
int cameraCompleted = 0, cameraPosition = -1;
while ((line = br.readLine()) != null /*|| cameraCompleted < mLanCamList.size()*/) {
String[] tokens = line.split("\\s+");
String CAM_MAC = "ec:ba:fe";
if (tokens.length >= 4 && tokens[3].startsWith(CAM_MAC)) {
if (cameraPosition != -1) {
mLanCamList.get(cameraPosition).setIp(tokens[0]);
cameraCompleted+=1;
}
}
}
br.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
return cams;
}
如您所见,所有项目都缺少IP地址。我尝试过InetAddress,但它只返回相同的IP。