Sample Android application to create virtual rooms for sharing information and content
- Head to https://manage.getcoco.buzz (Sign up if needed)
- Go to Applications tab
- Click on + Application button
- Choose the application name, group and type
- Choose the capabilities and click submit
- Click on the Created Application
- Get the client id present there
- Replace the client_id present in strings.xml with your client id
- Run the app
- Copy .jar and paste it in the app/libs
- Mark .jar as a gradle dependency
implementation fileTree(include: ['*.jar'], dir: 'libs')
- Copy .so file directories and paste them in app/src/main/jniLibs
- Add INTERNET permission in Manifest file
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
- Load Native Library
class Application {
static {
System.loadLibrary("cocojni");
}
}
- Init CocoClient
class Application {
public static void main(String[] args) {
new CocoClient.Builder()
.addCallbackListener(new DefaultNativeCallbacksInterface() {})
.withPlatform(new PlatformInterface() {})
.build();
}
}
- Connect to a Network with invite url
class Application {
public static void main(String[] args) {
// Init CocoClient
/**
* POST: https://api.getcoco.buzz/network-manager/networks/:networkId/generate-invite
* AUTH: Bearer Token {{access_token}}
* BODY: {
* "appId": "{{client_id}}",
* "appCapabilities": [ 0, 1, 2 ]
* }
*/
new Network.ConnectArgs()
.setNetworkId("<NETWORK ID>")
.setNodeId(NODE_ID)
.setInviteURL("<INVITE URL>")
.setNetworkName("<NETWORK NAME>")
.setNetworkType(NETWORK_TYPE)
.setUserRole(USER_ROLE)
.setAccessType(ACCESS_TYPE)
.connect();
}
}
- Connect to previously connected networks
class Application {
public static void main(String[] args) {
// Init CocoClient
// Get the list of previously connected networks
Network[] networks = CocoClient.getInstance().getSavedNetworks();
for (Network network : networks) {
network.connect();
}
}
}
- Sending Data to other nodes
class Application {
public static void main(String[] args) {
// Init CocoClient
// Connect to a Network
// Get the list of previously connected networks
Network[] networks = CocoClient.getInstance().getSavedNetworks();
// get the network with given network id. (Null if it's not yet connected)
Network network = CocoClient.getInstance().getNetwork("<NETWORK ID HERE>");
// send data to all nodes in the network
network.sendData("hello world", null);
// send data to nodes with node id 1, 2
network.sendData("hello world", new long[] { 1, 2 });
// send content info to all nodes in the network
network.sendContentInfo(System.currentTimeMillis(), "hello world", null);
// send data to nodes with node id 1, 2
network.sendContentInfo(System.currentTimeMillis(), "hello world", new long[] { 1, 2 });
}
}
- Listening for content info
class Application {
public static void main(String[] args) {
// Init CocoClient
// Connect to a Network
CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {
@Override
public void nodeConnectionStatusCallback(Network network, long nodeId, NodeType nodeType, boolean isOnline, Object networkContext) {
// determines if the node is online or not
// do something
}
@Override
public void receiveDataCallback(Network network, long sourceNodeId, String data) {
// do something
}
@Override
public void contentInfoCallback(Network network, long sourceNodeId, long contentTime, String data) {
// do something
}
});
}
}
- Disconnecting from network
class Application {
public static void main(String[] args) {
// Init CocoClient
// Connect to a Network
// Get the connected network
Network network = CocoClient.getInstance().getNetwork("<NETWORK ID>");
network.disconnect();
}
}
- Listening for connection status
class Application {
public static void main(String[] args) {
// Init CocoClient
// Connect to a Network
CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {
@Override
public void connectStatusCallback(Network network, Object context) {
Network.State status = network.getState();
// do something
}
});
}
}
- Listening for network metadata
class Application {
public static void main(String[] args) {
// Init CocoClient
// Connect to a Network
CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {
@Override
public void networkMetadataCallback(Network network) {
network.getMetadata();
// do something
}
});
}
}
- Rules for proguard and r8 located here
class Application {
public static void main(String[] args) {
DefaultNativeCallbacksInterface listener;
new CocoClient.Builder().build();
new Network.ConnectArgs().connect();
Network network = CocoClient.getInstance().getNetwork("<NETWORK ID>");
CocoClient.getInstance().addSubscription(listener = new DefaultNativeCallbacksInterface() {
@Override
public void receiveDataCallback(Network network, long sourceNodeId, String data) {
// listen for messages
}
});
network.sendData("hello world", null);
network.disconnect();
CocoClient.getInstance().removeSubscription(listener);
}
}
- Some of the best practices have been omitted for sake of simplicity and readability
- Above mentioned APIs are subject to change
- The manual .jar and .so placement will be replaced with gradle dependency
- NativeCallbacksInterface will be replaced with inline callback