Using RedBear Nano with SAndroidE

Giovanni Lenzi edited this page Nov 27, 2017 · 30 revisions

NOTE: Code in this page applies to Sandroide v0.7 and later. NOTE: You may find the full source of this application here

Introduction

SAndroidE allows your app to easily interact with RedBear Nano device.

In this tutorial we will see how to manage a button, a led and a trimmer connected to Nano pins. The logic is very simple: the analog value of the trimmer must be displayed on the application's UI, while the led must light on when the button is clicked.

sandroide-nano-circuit

For any issue related with your Android code, you may access the example app sources


Requirements

  1. A smartphone supporting Bluetooth 4.0-4.1 (aka Bluetooth Smart)
  2. A micro USB cable to deploy application to your smartphone
  3. Android Studio
  4. SAndroidE zip package: download and extract the zip file
  5. Basic programming knowledge of Android applications
  6. A Nano device complete with its MK20 USB dongle
  7. A Male-Female USB cable to flash the Nano and powering it on

Mount the circuit

In the following picture you can see how to mount the hardware to be ready for test your App:

TutorialNanoCompleto.png


Prepare your app for SAndroidE

Some steps are common to all SAndroidE based projects. Please follow our Prepare your app for SAndroidE guide before proceeding with next steps.


Flashing the Nano

To flash the Nano you should:

  1. connect the Nano to the provided MK20 USB dongle
  2. connect the USB dongle to your PC using a female-male usb cable, like the one in the picture
  3. When a new drive named MBED appears in your Explorer, just save the Sandroide compatible Nano firmware into your new drive.
  4. After flashing, the nano will automatically restart and will be ready for use from your smartphone

Flashing Nano

If you want to extend the firmware with your own logic, import these published sources into your own mbed developer portal account, by clicking the Import into compiler button.


Detecting Nano device

To use the Nano device in your application, you should first give it a name, by using the Flasher app.

Just copy all xml files contained within the SAndroidE zip package and the Flasher app into the root of your smartphone device.

Then click on the flasher app, using the Smartphone File Explorer, to install it.

After installation, you can run the Flasher app and search for bluetooth devices nearby by clicking the Scan for Discoverable devices button. A device of type MyNano should appear in the list.

device list

Then we wait for the Bluetooth characteristic named BLE Shield UART Service to show up and then click on the Store attribute button.

nano device selected

We assign our device the name nano.

device naming

After that, on the main menu we click on the Show Stored Resources, to see the exact resource identifiers given by SAndroidE to each pin.

stored resources

These identifiers will be required when writing our application.

As shown in the picture below Nano can work in two modes:

stored resources

User mode enables 6 analog inputs, while the default mode only has 3 analog inputs. Make your choice and use the mode which better suits your requirements.

On the official RedBear Nano page, you can find more on how to change from default mode to user mode.

Writing the Android Application

The Layout file

The layout file, that is the file containing the application User Interface, is very simple. It contains only some TextView to show the analog value of the Trimmer and the states of Button and Led.

You can find the source code here

The Activity file

As you can see from the example activity, it must inherit from SandroideBaseActivity in order to let SAndroidE execute some tasks like initialization and checking that Android permissions have been correctly granted from the user.

NOTE: After copying the code, you will have to change the word "giove" in the line "package com.example.giova.nano;" with your username.

Some class variables related to SAndroidE must be defined:

    SandroideDevice nano;
    SandroidePin nanoButton;
    SandroidePin nanoLed;
    SandroidePin nanoTrimmer;

Then we connect the Nano using the resource identifier given by the flasher application.

        nano = ((SandroideDevice) BLEContext.findViewById("nano_sandroide_device"));

        if (nano!=null) {

            nano.setOnDeviceConnected(new SandroideDevice.OnDeviceConnectedListener() {
                @Override
                public void onEvent(SandroideDevice device) {
                    Log.i(TAG,"OK, Nano connected");
                }
            });

To finish, we implement the logic for each pin.

WARNING: to be sure to do not miss the first transmitted value call all the setting mode with the constructor as shown below.

 nanoButton = new SandroidePin().setDevice(nano,15)
                    .setMode(SandroidePin.PIN_MODE_DIGITAL_INPUT)
                    .setDeltaThreshold(0.02)
                    .setSamplingInterval(50)
                    .setOnValueReceived(new SandroidePin.OnValueReceivedListener() {
                        @Override
                        public void onEvent(final Object newValue, Object oldValue) {
                            nanoLed.setValue(newValue);
                            Log.d(TAG, "button pressing: " + newValue);
                            runOnUiThread(new Runnable() {
                                              @Override
                                              public void run() {
                                                  tvButton.setText((int)newValue==1?"Button PRESSED":"Button not pressed");
                                                  tvButton.setTextColor((int)newValue==1?Color.BLUE:Color.BLACK);
                                              }
                                          }
                            );
                        }
                    });
            nanoLed = new SandroidePin().setDevice(nano,28)
                    .setMode(SandroidePin.PIN_MODE_DIGITAL_OUTPUT)
                    .setValue(true)
                    .setOnValueReceived(new SandroidePin.OnValueReceivedListener() {
                        @Override
                        public void onEvent(final Object newValue, Object oldValue) {
                            Log.d(TAG, "led state: " + newValue);
                            runOnUiThread(new Runnable() {
                                              @Override
                                              public void run() {
                                                  tvLed.setText((int)newValue == 1 ? "Led ON" : "Led OFF");
                                                  tvLed.setTextColor((int)newValue == 1 ? Color.BLUE : Color.BLACK);
                                              }
                                          }
                            );
                        }
                    });

            nanoTrimmer = new SandroidePin().setDevice(nano,6)
                    .setMode(SandroidePin.PIN_MODE_ANALOG_INPUT,1)
                    //.setDeltaThreshold(0.2)
                    //.setSamplingInterval(400)
                    .setOnValueReceived(new SandroidePin.OnValueReceivedListener() {
                        @Override
                        public void onEvent(final Object newValue, Object oldValue) {
                            Log.d(TAG, "Trimmer: " + newValue);
                            runOnUiThread(new Runnable() {
                                              @Override
                                              public void run() {
                                                  tvTrimmer.setText(newValue.toString());
                                              }
                                          }
                            );
                        }
                    });

NOTE: You may find full source of this activity here


Running the application

Down below the result of running the application, which behaves as expected.

sandroide-nano-circuit

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.