Aru*_*ham 12 java android network-programming jvm-hotspot wifi
我正在构建一个可以在移动设备和Wi-Fi设备之间传输数据的应用程序......移动设备已启用AP(通过代码),另一台设备连接到此特定网络...如何通过代码检测到查看连接到网络(AP)的设备的详细信息?**是否有解决方案?
我在HTC Desire中看到了一个名为Wifi Hot spot的应用程序,它具有显示连接到网络的设备的IP地址的功能.怎么能实现这一目标?
查看评论:HTC EVO 4G上的Sprint Mobile Hotspot.
它显示了一个可以实际显示已连接用户的应用程序.我们怎么能以编程方式做到这一点?那有API吗?
用于创建接入点:
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("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
}
}
}
}
if(apstatus)
{
System.out.println("SUCCESSdddd");
//statusView.append("\nAccess Point Created!");
//finish();
//Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
//startActivity(searchSensorsIntent);
}
else
{
System.out.println("FAILED");
//statusView.append("\nAccess 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");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以读取该/proc/net/arp文件以读取所有ARP条目.请参阅Android博客文章中的示例:如何查找远程主机的硬件MAC地址.在ARP表中,根据IP地址搜索属于您的Wi-Fi网络的所有主机.
以下是示例代码,用于计算连接到AP的主机数.此代码假定一个ARP条目用于连接到网络的电话,其余ARP条目来自连接到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.
}
Run Code Online (Sandbox Code Playgroud)
如果您知道其主机名或IP地址,则可以ping设备.
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ping -c 1 " + hostname);
proc.waitFor();
Run Code Online (Sandbox Code Playgroud)
您可以进行IP地址扫描,使用上面的ping或尝试使用TCP或UDP进行连接,尝试网络上的每个IP地址进行响应.
如果您知道MAC地址,则可以使用ARP表.
如果您在设备上运行了自己的软件,则可以在每台设备上发送UDP数据包并在Android设备上进行监听.有关如何执行此操作,请参阅在Android中发送和接收UDP广播数据包.
| 归档时间: |
|
| 查看次数: |
22302 次 |
| 最近记录: |