我正在尝试创建一个可以找到局域网中所有设备的应用程序。我已经能够通过ping IP范围来做到这一点。有没有其他方法可以做同样的事情?
是否可以通过侦听android中的传入数据包来检测同一网络中的设备?
答案 0 :(得分:1)
以下代码将搜索设备并将其信息放入列表中: -
//an array list of DeviceList type, which will take the info from getter setters
ArrayList<DeviceList> Dlist = new ArrayList<DeviceInfo>();
wifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()){
List<ScanResult>wifilist =wifi.getScanResults();
if(wifilist != null){
for(int m = 0; m < wifilist.size(); m++)
{
int signallevel = WifiManager.calculateSignalLevel(wifilist.get(m).level,20);
int signalStrength=(signallevel*100)/20;
DeviceList dInfo = new DeviceList();
dInfo.setDeviceName((wifilist.get(m).SSID));
dInfo.setSignalStrength(String.valueOf((signalStrength+"%")));
dInfo.setDeviceType("WiFi");
Dlist.add(dInfo);
}
}
你需要一些吸气剂设定器。您可以按如下方式将它们添加到不同的类中: -
public class DeviceList {
String DeviceName="";
String SignalStrength="";
String deviceType="";
public String getDeviceName() {
return DeviceName;
}
public void setDeviceName(String deviceName) {
DeviceName = deviceName;
}
public String getSignalStrength() {
return SignalStrength;
}
public void setSignalStrength(String signalStrength) {
SignalStrength = signalStrength;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
}
答案 1 :(得分:0)