首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用wifi直接在android中进行一对多移动设备文件传输的问题

使用wifi直接在android中进行一对多移动设备文件传输的问题
EN

Stack Overflow用户
提问于 2015-06-07 11:50:48
回答 2查看 1.4K关注 0票数 2

我正在尝试创建一个应用程序,其中一个安卓应用程序可以将文件(文本、视频、照片)传输给其他多个安卓devices.initially,我认为它可以在安卓系统中直接使用wifi在多个设备上共享文件。

但是我与WiFi直接面临的问题是,它在维护连接和查找其他设备方面不一致。

1)有时候,应用程序必须等待大约5分钟或更长时间,然后才能连接。

( 2)在多次通过其他设备的对话接受邀请后,切换到connected状态需要花费大量的时间,到那时,设备无法获得其他设备的IP地址。

在经历了这种矛盾之后,我想放弃直接使用wifi的想法。有没有人更好地建议FASTER将多个文件从一个移动设备传输到另一个没有访问点的设备。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-08 18:10:04

Hotspot使用使用反射调用的隐藏方法。从本质上讲,hotspot是其他人连接到普通wifi网络时可以连接到的一个接入点。

如前所述,它是一个接入点,因此它们是需要支持的两个主要功能。

  1. 创建热点
  2. 连接到一个。

1.创建热点

代码语言:javascript
复制
/**
     * 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)

代码语言:javascript
复制
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.连接到热点

代码语言:javascript
复制
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;
    }

想想看,您可能还需要连接到hotspot的设备的列表。

代码语言:javascript
复制
/**
     * 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网络,以获得连接到的网络。

代码语言:javascript
复制
//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();

希望这些都是你需要的指点!

票数 3
EN

Stack Overflow用户

发布于 2015-06-08 01:38:00

Wifi直接技术对于点对点communication.However来说是非常理想的,它只适用于单个连接,即只有1-1用例,.It可以创建一对多的连接,每台设备都连接到组所有者(软AP).But,即使你构建了一对一的连接,并且尝试扩展到多用例,在某些无法连接perfectly.Devices的设备上会遇到问题。下载此应用程序https://play.google.com/store/apps/details?id=com.budius.WiFiShoot&hl=en后,请检查下面列出的一些问题

如果您想要进行多个连接,最好使用hotspot .One设备作为热点,其他客户端连接到hotspot,.It正在被许多应用程序所使用,比如xender、zapiya。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30693182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档