【问题标题】:How to exchange data across two android devices with the same app?如何在具有相同应用程序的两个 android 设备之间交换数据?
【发布时间】:2017-12-14 10:06:04
【问题描述】:

我有以下代码可以连接到另一部手机的热点 wifi。

这个想法是让两个设备相互发送简单的数据;在每一端,手机都连接到我的应用程序。然后应用响应发送的数据。

    private void ConnectWifi(String networkSSID, String networkPass) {

    List<ScanResult> scanResult = wifiManager.getScanResults();
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";
    String lstItems = new String();
    for (ScanResult scanResults : scanResult) {

        lstItems += "\n" + scanResults.SSID;

        if (scanResults.capabilities.contains("WEP")) {
            conf.wepKeys[0] = "\"" + networkPass + "\"";
            conf.wepTxKeyIndex = 0;
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        } else if (scanResults.capabilities.contains("WPA")) {
            conf.preSharedKey = "\"" + networkPass + "\"";
        } else {
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }

    }


    wifiManager.addNetwork(conf);


    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("KEBWARO 3")) {
            wifiManager.disconnect();

            wifiManager.enableNetwork(i.networkId, true);
            wifiManager.reconnect();
            //subscriberData.setNetworkID(i.SSID);
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setIcon(R.drawable.beer);
            alert.setTitle("CONNECTED SUCCESSFULLY");
            alert.setMessage("CONNECTED TO " + i.SSID);
            alert.show();
            networkID = i.SSID;
        }

    }
}

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    这必须通过 Wifi P2P - Android API 14+ 中的 Wifi Direct Network Discovery API 完成。

    你可以在这里找到示例代码

    https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多