Before you begin

When you first use Resonance SDK, you have to create account at console.getresonance.net and register an application to get access to API key and download Resonance SDK for Android.

Note: You should use the same API key on all platforms in order to allow cross platform user matching.

Import Resonance module

Unzip Resonance SDK to any location and import Resonance module into your AndroidStudio project using File -> New -> Import Module and choose "Resonance" folder. Add compile project(":Resonance") into dependencies section of build.gradle of your app module.

Initializing Resonance

Before using Resonance API you must initialize the SDK using the API key of your application. To do this use Resonance.init(Context context, String apiKey)
All Resonance classes defined in com.cht.resonance package

Example:

import com.cht.resonance.Resonance;
import com.cht.resonance.ResonanceListener;

//...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Resonance.init(this, "DEMO_API_KEY");
}

Access to Microphone

The application needs to gain access to a devices’ microphone from the user in order to start search devices.

Add this line into your ApplicationManifest.xml:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Also request permission in runtime:

private final int RECORD_AUDIO_REQUEST_CODE = 1;
private void prepareToSearch() {
    if(ContextCompat.checkSelfPermission(this,
        Manifest.permission.RECORD_AUDIO)
        == PackageManager.PERMISSION_GRANTED) {
        // permission has been granted earlier
        // you can start search
    } else {
        String permissions[] = {Manifest.permission.RECORD_AUDIO};
        ActivityCompat.requestPermissions(this, permissions, RECORD_AUDIO_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch(requestCode) {
        case RECORD_AUDIO_REQUEST_CODE:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission granted
                // you can start search
            } else {
                // user denied microphone access
            }
    }
}

More info about runtime permissions available here

Internet connection check

Before initiating a search it is desirable to make sure that the device has an active internet connection. Here is a way to do this:

public boolean isOnline() {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

Starting search

For devices to discover eachother, all of them must start a search. To do this you need to call Resonance.startSearch(String payload, ResonanceListener listener) method.

payload is data which will be delivered to nearby devices. Maximum length is 2048.

To receive a notification when a device is located nearby or if there was an error, you need to inherit ResonanceListener class and override its onNearbyFound(Client client) and onSeachStopped(ResonanceError error) methods.

Example:

Resonance.startSearch("Some useful data to send", new ResonanceListener() {
    @Override
    public void onNearbyFound(Client client) {
        Log.d("NearbyFound", "Nearby found with payload: " + client.getPayload());
    }

    @Override
    public void onSearchStopped(ResonanceError error) {
        if(error != null) {
            Log.e("ResonanceError", "Search stopped with error: " + error.message);
        }
    }
});

Stopping search

To stop the search, you should call Resonance.stopSearch(). Do not forget to stop the search when the application is in background.

onSearchStopped(ResonanceError error) is invoked when the search is stopped by application or when search is stopped due to an error – such as when there is no connection with the server. If the argument error is equal to null, then the search has been stopped by the application. If not then there was an error.