任何人都可以建议我如何将所有网络连接到我的wifi。我想在其中识别特定的网络,然后所有特定的网络都应该显示在列表视图中。
答案 0 :(得分:0)
以下是如何获取wifi连接设备ip的示例
https://github.com/SupunArunoda/ChatWithoutInternet
private WifiApiManager wifiApManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_sharing);
wifiApManager = new WifiApiManager(this);
listView = (ListView) findViewById(R.id.listView2);
username = (String) getIntent().getExtras().get("name");
SelectClient sendImage = new SelectClient();
sendImage.execute((Void) null);
}
//Asynchronous class to select Client
private class SelectClient extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
final ArrayList<ClientScanResult> clients = wifiApManager.getClientList(false);
runOnUiThread(new Runnable() {
@Override
public void run() {
ClientScanResult clientScanResult;
values = new String[clients.size()];
for (int i = 0; i < clients.size(); i++) {
clientScanResult = clients.get(i);
values[i] = "IpAddress: " + clientScanResult.getIpAddress();//store client's ip addresses in values array
}
adapter = new ArrayAdapter<>(ClientSelectActivity.this, R.layout.list_white_text, R.id.list_content, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
i = new Intent(ClientSelectActivity.this, PrivateChatActivity.class);
i.putExtra("ipAddress",clients.get(position).getIpAddress());
i.putExtra("name", username);
startActivity(i);//start activity from this activity to PrivateChatActivity
}
});
}
});
return true;
}
}
//class to manage WiFi technology
public class WifiApiManager {
private final WifiManager mWifiManager;
public WifiApiManager(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
return getClientList(onlyReachables, 300);
}
//function to return available clients
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
try {
result = new ArrayList<>();
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
return result;
}
}