【问题标题】:WiFi P2P Connection user interaction bypass?WiFi P2P 连接用户交互绕过?
【发布时间】:2017-08-31 18:39:13
【问题描述】:

我正在使用 WiFi Direct 开发一个 Android 应用程序。该应用程序应该在每个设备中创建一个组或搜索对等点并将字符串传输给每个对等点,具体取决于设备是否具有 Internet 连接。如果设备有 Internet,我还可以发现所有附近对等点的 MAC。

这是我获取附近设备的代码,其中 groupCreated 是一个变量,用于说明该设备是否已通过 createGroup() 创建了组或确实在寻找对等设备:

private WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {

    @Override
    public void onPeersAvailable(WifiP2pDeviceList peerList) {

        if (!groupCreated) {

            Collection<WifiP2pDevice> refreshedPeers = peerList.getDeviceList();
            String peerInfo = "Available peers: \n";

            if (!refreshedPeers.equals(peers)) {
                peers.clear();
                peers.addAll(refreshedPeers);
                for (WifiP2pDevice peer : peers) {
                    peerInfo += "\nMAC: " + peer.deviceAddress + " - Name: " + peer.deviceName;
                }
                TextView peerDisplay = (TextView) findViewById(R.id.peerListText);
                peerDisplay.setText(peerInfo);

                connectPeers();
            }
            if (peers.size() == 0) {
                Toast.makeText(ProviderActivity.this, "No peers found!",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
};

这是实际连接到对等点的代码:

public void connectPeers(){
    for (WifiP2pDevice peer : peers) {
        WifiP2pConfig config = new WifiP2pConfig();
        config.deviceAddress = peer.deviceAddress;
        mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {

                String host = "192.168.69.1";
                int port = 8888;
                int len;
                Socket socket = new Socket();
                String sent = "Hi!";
                byte buf[] = new byte[1024];

                try {
                    /**
                     * Create a client socket with the host,
                     * port, and timeout information.
                     */
                    socket.bind(null);
                    socket.connect((new InetSocketAddress(host, port)), 1000);

                    /**
                     * Create a byte stream from a JPEG file and pipe it to the output stream
                     * of the socket. This data will be retrieved by the server device.
                     */
                    OutputStream outputStream = socket.getOutputStream();
                    InputStream inputStream = new ByteArrayInputStream(sent.getBytes(Charset.forName("UTF-8")));
                    while ((len = inputStream.read(buf)) != -1) {
                        outputStream.write(buf, 0, len);
                    }
                    outputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    System.out.println(e.toString());
                }

                /**
                 * Clean up any open sockets when done
                 * transferring or if an exception occurred.
                 */ finally {
                    if (socket != null) {
                        if (socket.isConnected()) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                Toast.makeText(ProviderActivity.this, "Failed to close the connection!",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }
            @Override
            public void onFailure(int reason) {
                Toast.makeText(ProviderActivity.this, "Failed to connect to peer!",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这是客户端,服务器端是异步任务,取自“传输数据”部分的https://developer.android.com/guide/topics/connectivity/wifip2p.html#setup。由于我总是连接到组所有者,因此我将 Android 属性的 IP 地址硬编码到 WiFi Direct (192.168.69.1) 中的 GO。

使用此代码,我可以创建组并连接到对等点,但是当我尝试创建套接字并传输数据时,在对等点中会出现提示,让用户接受或拒绝连接。在 API 指南中没有涉及用户交互。我做错了什么?

感谢您的关注。

【问题讨论】:

    标签: android sockets android-studio wifi-direct wifip2p


    【解决方案1】:

    类似问题here

    您可以跟踪对此功能的大量请求here

    最后,我在分享的第二个链接中的一个 cmets 中找到了一个 work around仔细阅读给定代码中的 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多