WiFi P2P Connection用户交互绕过?

时间:2017-04-05 23:23:51

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

我正在使用WiFi Direct开发Android应用程序。应用程序应该在每个设备中创建一个组或搜索对等体并将字符串传输到每个对等体,具体取决于设备是否具有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();
            }
        });
    }
}

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

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

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

类似问题here

您可以跟踪此功能的众多请求here

最后,我在共享的第二个链接中的一条评论中找到了work around仔细阅读给定代码中的注释。