android - 通过热点在两个 Android 设备之间进行数据交换

标签 android sockets personal-hotspot

我想在两个 Android 设备之间通过热点交换数据。我已尝试正确连接。

1st。我创建了可移植热点:

网络 SSID - my_hotspot
安全 - WPA PSK
密码-密码

第二。我正在尝试在应用程序启动时进行连接。这是我的代码

    mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";   
    conf.wepKeys[0] = "\"" + networkPass + "\""; 
    conf.wepTxKeyIndex = 0;
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
    conf.preSharedKey = "\""+ networkPass +"\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    
    int res = mWifiManager.addNetwork(conf);
    boolean b = setWifiApEnabled(null, true);

我认为它是不成功的。然后我尝试通过套接字发送数据。我从 JavaCodeGeeks 中学到了.我配置了SERVER_IP 192.168.49.1,SERVER_PORT:8888。

如何使用热点在两个 Android 设备之间正确通信?

提前致谢。

最佳答案

您为什么不使用 Wi-Fi Direct?它是一种 p2p 协议(protocol)。您无需热点即可在两台 Android 设备之间共享数据。它使用 android.net.wifi.p2p 包。

Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point (Android's Wi-Fi P2P framework complies with the Wi-Fi Alliance's Wi-Fi Direct™ certification program). Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi P2P, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application.

您可以在 Android 开发者网站上找到有关此技术的更多信息。以下是一些有用的链接:

Android Wi-Fi p2p

android.net.wifi.p2p package

Creating the application

关于android - 通过热点在两个 Android 设备之间进行数据交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21926158/

相关文章:

ios - 如何通过 iPhone 个人热点在设备之间传输文件?

android - 在 Android 上查找 SD 卡卷标

Android:制作 "shrinkResources true"以保留所有可绘制对象,但删除其他未使用的资源

android - 在 Android 上使用 Smack 4.1 时出现初始化异常 "NoClassDefFoundError: javax.naming.directory.InitialDirContext"

java - 如何在我的 android Activity 中的类中使用抽象方法?

iphone - SSH 到通过 iPhone 热点连接的 Raspberry Pi

连接 VMCI 流套接字

ios - iPad 之间的安全通信

node.js - 使用 React 和 Socket.io 的聊天应用程序在发送过多消息后挂起

Wifi 中继器和强制门户