Android application Wi-Fi device - AP connectivity(Android 应用 Wi-Fi 设备 - AP 连接)
问题描述
我正在构建一个可以在移动设备和 Wi-Fi 设备之间传输数据的应用程序...移动设备具有 AP 已启用(通过代码)并且另一台设备连接到此特定网络...如何通过代码检测以查看连接到网络(AP)的设备的详细信息?** 是有解决办法吗?
I am building an application which can transfer data between a mobile and a Wi-Fi device... The mobile has got the AP enabled (through code) and another device connects to this specific network... How can I detect through code to see the details of the devices connected to the network(AP)?** Is there a solution for this?
我在 HTC Desire 中看到了一个名为 Wifi 热点 的应用程序a> 执行此功能,显示连接到网络的设备的 IP 地址.如何实现?
I have seen an application called Wifi Hot spot in HTC Desire that does this functionality of showing the IP addresses of the devices connected to the network. How can this be achieved?
查看 评论:HTC EVO 4G 上的 Sprint 移动热点.
它显示了一个可以实际显示连接用户的应用程序.我们如何以编程方式做到这一点?有相应的 API 吗?
It shows an application that can actually display the connected users. How can we do that programmatically? Is there an API for that?
用于创建接入点:
private void createWifiAccessPoint() {
if (wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class
boolean methodFound = false;
for (Method method: wmMethods){
if (method.getName().equals("setWifiApEnabled")){
methodFound = true;
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = """+ssid+""";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
//netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//netConfig.preSharedKey = password;
//netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
try {
boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig,true);
//statusView.setText("Creating a Wi-Fi Network ""+netConfig.SSID+""");
for (Method isWifiApEnabledmethod: wmMethods)
{
if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
while (!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
};
for (Method method1: wmMethods){
if(method1.getName().equals("getWifiApState")){
int apstate;
apstate = (Integer)method1.invoke(wifiManager);
// netConfig = (WifiConfiguration)method1.invoke(wifi);
//statusView.append("
SSID:"+netConfig.SSID+"
Password:"+netConfig.preSharedKey+"
");
}
}
}
}
if(apstatus)
{
System.out.println("SUCCESSdddd");
//statusView.append("
Access Point Created!");
//finish();
//Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
//startActivity(searchSensorsIntent);
}
else
{
System.out.println("FAILED");
//statusView.append("
Access Point Creation failed!");
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
if (!methodFound){
//statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point");
}
}
推荐答案
你可以阅读 /proc/net/arp
文件来阅读所有的 ARP 条目.请参阅博客文章中的示例 Android:如何找到远程主机的硬件 MAC 地址.在 ARP 表中,根据 IP 地址搜索属于您的 Wi-Fi 网络的所有主机.
You could read the /proc/net/arp
file to read all the ARP entries. See the example in the blog post Android: Howto find the hardware MAC address of a remote host. In the ARP table, search for all the hosts that belong to your Wi-Fi network based on the IP address.
这里是示例代码,它计算连接到 AP 的主机数量.此代码假定一个 ARP 条目用于连接到网络的电话,其余的来自连接到 AP 的主机.
Here is example code, which counts the number of hosts connected to the AP. This code assumes that one ARP entry is for the phone connected to the network and the remaining ones are from hosts connected to the AP.
private int countNumMac()
{
int macCount = 0;
BufferedReader br = null;
try {
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("..:..:..:..:..:..")) {
macCount++;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (macCount == 0)
return 0;
else
return macCount-1; //One MAC address entry will be for the host.
}
这篇关于Android 应用 Wi-Fi 设备 - AP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 应用 Wi-Fi 设备 - AP 连接
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01