I am unable to create a group chat where more than 2 (up to 15) nearby android users are able to join a chat room.
startMeshNetwork()
starts advertising the connection and discovering.
This is called in onCreate()
, as well as in the callback for a successful connection. This is so that the device will continue to join all nearby devices.
public void startMeshNetwork(){
Nearby.getConnectionsClient(this)
.startAdvertising(
/* endpointName= */ "Name here",
/* serviceId= */ "ID here",
mConnectionLifecycleCallback,
new AdvertisingOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));
Nearby.getConnectionsClient(this)
.startDiscovery(
/* serviceId= */ "ID here",
new EndpointDiscoveryCallback() {
@Override
public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) { Nearby.getConnectionsClient(getApplicationContext())
.requestConnection(
/* endpointName= */ "Name here",
endpointId,
mConnectionLifecycleCallback);
endpoints.add(endpointId);
}
@Override
public void onEndpointLost(String endpointId) {
startMeshNetwork();
}
},
new DiscoveryOptions(com.google.android.gms.nearby.connection.Strategy.P2P_CLUSTER));
}
Code segment that sends payloads to all connected devices
Payload payload = Payload.fromBytes(pendingmessage.getBytes());
for(String endpointId:endpoints)
Nearby.getConnectionsClient(getApplicationContext()).sendPayload(endpointId, payload);
Connections are being formed with this method, however, the payload sending is inconsistent and is only sent to one device.