Peer to peer data sharing using Wi-Fi direct

﹥>﹥吖頭↗ 提交于 2019-12-02 08:07:45

The communication between client and WiFi Direct Group Owner is based on Socket server running on the Group Owner and clients connected to that server.

Once WiFi Direct group is formed(you can know that by checking "onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)"), check if the current device is the Group Owner and if so, start the sockets server (similar to your code):

mServerSocket = new ServerSocket(mPort);
Log.e(getClass().getSimpleName(), "Running on port: " + mServerSocket.getLocalPort());

Then next, add this line to accept connections and store a reference of the client socket:

Socket mSocket = mServerSocket.accept();

Now, you have a reference of the client socket, you can use it to send data / messages to it. Next, the other device (client) should initiate a connection to the socket server:

mSocket = new Socket();
mSocket.bind(null);
mSocket.connect(new InetSocketAddress(mAddress, mPort), 500);

To send message from server to client:

DataOutputStream mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());

Send simple message:

mDataOutputStream.writeUTF(message);
mDataOutputStream.flush();

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!