android 使用 wifi direct 的一对多移动设备文件传输问题
issues with one to many mobile device file transfer in android using wifi direct
我正在尝试创建一个应用程序,其中一个 android 应用程序可以将文件(文本、视频、照片)传输到其他多个应用程序 android devices.initially 我想直接使用 wifi android 跨多个设备共享文件。
但我在使用 WiFi direct 时遇到的问题是,它在保持连接和寻找其他设备方面不一致。
1) 有时应用程序必须等待大约 5 分钟或更长时间才能连接。
2) 多次通过其他设备的对话接受邀请后,需要花费大量时间将连接更改为 connected
状态,直到此时设备无法获取其他设备的 IP 地址。
在经历了不一致之后,我想放弃使用 wifi direct 的想法。有没有人有更好的建议 FASTER
将多个文件从一个移动设备传输到另一个没有接入点的设备。
Wifi direct 作为一种技术非常适合点对点 communication.However 谈论 wifi direct android,它仅适用于仅 1-1 用例的单个连接。它可以创建一对多,其中每个设备都连接到组所有者(软 AP)。但即使您构建一对一并尝试扩展到多用例,您也会遇到某些不连接的设备的问题 perfectly.Devices 陷入受邀状态。下载此应用程序后检查问题下列出的一些问题 https://play.google.com/store/apps/details?id=com.budius.WiFiShoot&hl=en
如果你想进行多重连接,最好使用热点。一个设备最终成为热点,其他客户端连接到热点。它被大量应用程序使用,如 xender、zapiya。
Hotspot 使用通过反射调用的隐藏方法。从本质上讲,热点是其他人连接到普通 wifi 网络时可以连接到的接入点。
如上所述,它是一个接入点,因此它们是需要支持的两个主要功能
- 创建热点
- 正在连接一个。
1.创建热点
/**
* Start AccessPoint mode with the specified
* configuration. If the radio is already running in
* AP mode, update the new configuration
* Note that starting in access point mode disables station
* mode operation
* @param wifiConfig SSID, security and channel details as part of WifiConfiguration
* @return {@code true} if the operation succeeds, {@code false} otherwise
*/
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
try {
if (enabled) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
设置有密码的热点(下例为WPA2)
WifiConfiguration wifiCon = new WifiConfiguration();
wifiCon.SSID = "ssid";
wifiCon.preSharedKey = "password";
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
try
{
Method setWifiApMethod = wm.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus=(Boolean) setWifiApMethod.invoke(wm, wifiCon,true);
}
catch (Exception e)
{
Log.e(this.getClass().toString(), "", e);
}
2。连接到热点
public Boolean connectToHotspot(WifiManager wifiManager, String ssid)
{
this.wifiManager = wifiManager;
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" +encodeSSID(ssid) +"\"";
wc.preSharedKey = "\"" + generatePassword(new StringBuffer(ssid).reverse().toString()) + "\"";
wifiManager.addNetwork(wc);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i!=null && i.SSID != null && i.SSID.equals(wc.SSID))
{
wifiManager.disconnect();
boolean status = wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
return status;
}
}
return false;
}
想起来,您可能还需要连接到热点的设备列表
/**
* Gets a list of the clients connected to the Hotspot, reachable timeout is 300
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param finishListener, Interface called when the scan method finishes
*/
public void getClientList(boolean onlyReachables, FinishScanListener finishListener) {
getClientList(onlyReachables, 300, finishListener );
}
/**
* Gets a list of the clients connected to the Hotspot
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param reachableTimeout Reachable Timout in miliseconds
* @param finishListener, Interface called when the scan method finishes
*/
public void getClientList(final boolean onlyReachables, final int reachableTimeout, final FinishScanListener finishListener) {
Runnable runnable = new Runnable() {
public void run() {
BufferedReader br = null;
final ArrayList<String> resultIPAddr = new ArrayList<String>();
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("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
resultIPAddr.add(splitted[0]);
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.toString());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
finishListener.onFinishScan(result);
}
};
mainHandler.post(myRunnable);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
此外,您可能需要扫描附近的 Wifi 网络才能连接到该网络。
//This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
希望这些都是您需要的指点..!!
我正在尝试创建一个应用程序,其中一个 android 应用程序可以将文件(文本、视频、照片)传输到其他多个应用程序 android devices.initially 我想直接使用 wifi android 跨多个设备共享文件。
但我在使用 WiFi direct 时遇到的问题是,它在保持连接和寻找其他设备方面不一致。
1) 有时应用程序必须等待大约 5 分钟或更长时间才能连接。
2) 多次通过其他设备的对话接受邀请后,需要花费大量时间将连接更改为 connected
状态,直到此时设备无法获取其他设备的 IP 地址。
在经历了不一致之后,我想放弃使用 wifi direct 的想法。有没有人有更好的建议 FASTER
将多个文件从一个移动设备传输到另一个没有接入点的设备。
Wifi direct 作为一种技术非常适合点对点 communication.However 谈论 wifi direct android,它仅适用于仅 1-1 用例的单个连接。它可以创建一对多,其中每个设备都连接到组所有者(软 AP)。但即使您构建一对一并尝试扩展到多用例,您也会遇到某些不连接的设备的问题 perfectly.Devices 陷入受邀状态。下载此应用程序后检查问题下列出的一些问题 https://play.google.com/store/apps/details?id=com.budius.WiFiShoot&hl=en
如果你想进行多重连接,最好使用热点。一个设备最终成为热点,其他客户端连接到热点。它被大量应用程序使用,如 xender、zapiya。
Hotspot 使用通过反射调用的隐藏方法。从本质上讲,热点是其他人连接到普通 wifi 网络时可以连接到的接入点。
如上所述,它是一个接入点,因此它们是需要支持的两个主要功能
- 创建热点
- 正在连接一个。
1.创建热点
/**
* Start AccessPoint mode with the specified
* configuration. If the radio is already running in
* AP mode, update the new configuration
* Note that starting in access point mode disables station
* mode operation
* @param wifiConfig SSID, security and channel details as part of WifiConfiguration
* @return {@code true} if the operation succeeds, {@code false} otherwise
*/
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
try {
if (enabled) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
设置有密码的热点(下例为WPA2)
WifiConfiguration wifiCon = new WifiConfiguration();
wifiCon.SSID = "ssid";
wifiCon.preSharedKey = "password";
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
try
{
Method setWifiApMethod = wm.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus=(Boolean) setWifiApMethod.invoke(wm, wifiCon,true);
}
catch (Exception e)
{
Log.e(this.getClass().toString(), "", e);
}
2。连接到热点
public Boolean connectToHotspot(WifiManager wifiManager, String ssid)
{
this.wifiManager = wifiManager;
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" +encodeSSID(ssid) +"\"";
wc.preSharedKey = "\"" + generatePassword(new StringBuffer(ssid).reverse().toString()) + "\"";
wifiManager.addNetwork(wc);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i!=null && i.SSID != null && i.SSID.equals(wc.SSID))
{
wifiManager.disconnect();
boolean status = wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
return status;
}
}
return false;
}
想起来,您可能还需要连接到热点的设备列表
/**
* Gets a list of the clients connected to the Hotspot, reachable timeout is 300
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param finishListener, Interface called when the scan method finishes
*/
public void getClientList(boolean onlyReachables, FinishScanListener finishListener) {
getClientList(onlyReachables, 300, finishListener );
}
/**
* Gets a list of the clients connected to the Hotspot
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param reachableTimeout Reachable Timout in miliseconds
* @param finishListener, Interface called when the scan method finishes
*/
public void getClientList(final boolean onlyReachables, final int reachableTimeout, final FinishScanListener finishListener) {
Runnable runnable = new Runnable() {
public void run() {
BufferedReader br = null;
final ArrayList<String> resultIPAddr = new ArrayList<String>();
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("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
resultIPAddr.add(splitted[0]);
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.toString());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
finishListener.onFinishScan(result);
}
};
mainHandler.post(myRunnable);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
此外,您可能需要扫描附近的 Wifi 网络才能连接到该网络。
//This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
希望这些都是您需要的指点..!!