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.
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();
}
});
}
}
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).
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?
Thanks for the attention.