WiFi P2P 连接用户交互绕过? [英] WiFi P2P Connection user interaction bypass?

查看:60
本文介绍了WiFi P2P 连接用户交互绕过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an Android application using WiFi Direct. The application is supposed to create a group in each device or search for peers and transfer a string to each peer, depending if the device has Internet connection or not. Also I'm able to discover the MACs of all nearby peers in case the device has Internet.

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

This is my code for getting the nearby devices, where groupCreated is a variable that states if this device has created a group via createGroup() or is indeed looking for peers:

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();
            }
        }
    }
};

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

This is the code for actually connecting to the peers:

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.

This is the client side, the server side is an async task, taken from https://developer.android.com/guide/topics/connectivity/wifip2p.html#setup in the "Transferring Data" section. Since I always connect to group owners I hardcoded the IP address Android attributes to a GO in WiFi Direct (192.168.69.1).

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

With this code I'm able to create the group and connect to the peers, but when I try to create a socket and transfer data, in the peers a prompt appears for the user to accept or decline the connection. In the API guide there was no user interaction involved. What am I doing wrong?

感谢关注.

推荐答案

类似问题 此处.

您可以在此处跟踪对此功能的大量请求.

You can track the numerous requests for this feature here.

最后,一个 解决这个问题,我在我分享的第二个链接的评论之一中找到了这一点.仔细阅读给定代码中的注释.

And finally, one work around for this that i found in one of the comments in the second link i shared. Read the comments in the given code carefully.

这篇关于WiFi P2P 连接用户交互绕过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆