Android


Miletus Android Library


Architecture overview

Miletus Android Library consists of an AAR project (Android Archive Library) with wrappers and callbacks/listeners that return asynchronously results from libMiletus, flashed on a device.

Concepts

Wrappers & Callbacks/Listeners

Wrappers

Wrappers are abstractions to hold objects from libMiletus.
DeviceWrapper
DeviceWrapper provides two informations, the first is the TinyDevice object, which has all the necessary information to communicate via Wifi/Lan with the device that contains libMiletus, the second is the Android BluetoothDevice, object used for GATT/BLE communication.
ComponentWrapper
ComponentWrapper provides information about commands and states of a libMiletus device.

Callbacks/Listeners

- Commons
Info Response Interface
Return an object that represents the phisical device running the libMiletus.
Traits Response Interface
Return a set of ComponentWrapper.
Components Response Interface
Return values with the current status of the device, that can be:
  • string: Alphanumeric status value.
  • integer: Integer number status value.
  • number: Float point status value.
  • boolean: Boolean status value.
Execute commands
Return success or fail after the execution of a command.
- BLE/GATT specific
Ble Resolved
Returns an Android BluetoothDevice and its RSSI every discovery.

Usage

Wifi/Lan & BLE/GATT

Wifi/Lan

Start
  1. NsdHelper.getInstance().discoverServices();
Connect
  1. NsdHelper.getInstance().setOnInfoResponse(onInfoResponse);
Stop
  1. NsdHelper.getInstance().stopDiscovery();

BLE/GATT

Start
  1. Intent intent = new Intent(this, BleScanService.class);
  2. startService(intent);
Connect
  1. private final ServiceConnection mConnection = new ServiceConnection() {
  2. @Override
  3. public void onServiceConnected(ComponentName className,
  4. IBinder service) {
  5. BleScanService.LocalBinder binder = (BleScanService.LocalBinder) service;
  6. binder.setOnBleInfoResponse(onBleInfoResponse);
  7. binder.setOnBleResolved(nearDeviceHolder);
  8. unbindService(mConnection);
  9. }
  10. @Override
  11. public void onServiceDisconnected(ComponentName componentName) {
  12. }
  13. };
  1. bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Stop
  1. stopService(new Intent(this, BleScanService.class));


Miletus Android App

Demonstrates basic usage of the Miletus Android Library.

Build

This sample uses the Gradle build system. To build this project, use the `gradlew build` command or import the project into Android Studio.

Run

To run this sample you will need at least one Android device and one LibMiletus compatible device, for example an esp8266 development board.

Usage

Discover a LibMiletus device

  1. private final SendInfoGattCommand.OnBleInfoResponse onBleInfoResponse = new SendInfoGattCommand.OnBleInfoResponse() {
  2. @Override
  3. public void onBleInfoResponse(final DeviceWrapper device) {
  4. if (mDeviceListAdapter.containsBle(device) == null) {
  5. addDevice(device);
  6. Log.i(TAG, "Device BLE added: " + device.getDevice().getName());
  7. } else {
  8. Log.e(TAG, "Device BLE not added: " + device.getDevice().getName());
  9. }
  10. }
  11. };

Get Traits/Components from a LibMiletus device

  1. new SendComponentsGattCommand(this.getContext(),
  2. this,
  3. mDevice,
  4. components).execute();
  1. @Override
  2. public void onComponentsResponse(final Set<ComponentWrapper> components,
  3. final Set<StateWrapper> states,
  4. final TinyDevice device,
  5. final boolean isSuccess) {
  6. if (!isSuccess) {
  7. Log.e(TAG, "Failure querying for state.");
  8. if (ComponentsFragment.this.getView() != null) {
  9. Snackbar.make(ComponentsFragment.this.getView(),
  10. R.string.error_querying_state,
  11. Snackbar.LENGTH_LONG)
  12. .show();
  13. }
  14. return;
  15. } else {
  16. Log.i(TAG, "Success getting states: " + components.size());
  17. }
  18. addComponents(components);
  19. }

Execute command

  1. new SendExecuteCommand(mDevice.getDevice(),
  2. executeCommandResponse,
  3. command).execute();
  1. private final SendExecuteCommand.OnExecuteCommandResponse executeCommandResponse =
  2. new SendExecuteCommand.OnExecuteCommandResponse() {
  3. @Override
  4. public void onExecuteCommandResponse(boolean isSuccess) {
  5. if (!isSuccess) {
  6. Log.e(TAG, "Failure setting state.");
  7. if (CommandsFragment.this.getView() != null) {
  8. Snackbar.make(CommandsFragment.this.getView(),
  9. R.string.error_setting_state,
  10. Snackbar.LENGTH_LONG)
  11. .show();
  12. }
  13. } else {
  14. Log.i(TAG, "Success setting state!");
  15. }
  16. }
  17. };

Screens

Main screen

In main screen the user can see a list of discovered devices:

Main screen Main screen


Device screen

The Device screen shows the states, and it is possible to show and hide more status about the device:

Device screen Device screen


Notification

Based on a nearby BLE device, a notification is showed:

Notification


Commands screen

Device interaction in the commands screen:

Commands screen