SAndroidEOfficial / framework

SAndroidE - Sensors for Android Embedded. A free framework allowing external devices to be easily managed in Android
http://es3.unibs.it/SAndroidE
Other
7 stars 1 forks source link

Bidirectional parameters exchange between smartphone and devices #46

Open giowild opened 7 years ago

giowild commented 7 years ago

It would be great, for Nano, Arduino and Raspberry, but in general for any device with a firmware on-board, to have a way to exchange parameters in a bidirectional way with the smartphone. These parameters /messages are indipendent from the use of a specific pin, thus they should be sent to a device withouth the need to instantiate a BLEGeneralIO object, which is instead bound to a specific pin.

This feature should be flexible enough to send and receive any kind of message, regardless of the app who has to be built. To this purpose, the use of a JSON string to send or receive, may give the needed flexibility.

I imagine a new item, in addition to the usual BLEButton, BLEGeneralIO and BLEAlarm, to represent the device itself (not a single pin) and to be used to receive/send messages inside of a json string.

This code will let applications to send custom configuration parameters, which can then be appropriately understood by a specific firmware (a firmware for heartrate monitoring may have configuration parameters different from a firmware for home-automation):

BLEDevice mynanodevice =  (BLEDevice ) BLEContext.findViewById("nano");

// how to parse message arrived
mynanodevice.setDeviceListener(new DeviceListener(String message) {
                @Override
                public void onNewMessage(String jsonString) {
                                        JSONObject myobj = new JSONObject(jsonString);
                                        int hr = myobj.getInt("heartrate");
                                        int timestamp = myobj.getInt("when");
                                        Log.d("At time %d ... heart rate was %d",timestamp, hr);
                }
            });

// how to send a custom message to firmware for heart rate monitoring
JSONObject newMsg = new JSONObject();
newMsg.put("action","set-sampling-period");
newMsg.put("value",1000);
mynanodevice.sendMessage(newMsg.toString());

These functions, dedicated to enable two-way communication between a smartphone and a device, may then use a dedicated bluetooth characteristic instead of using the actual TX and RX characteristics implemented in nano and arduino.

What do you think? Ideas & how-to are welcome!!! :)

giowild commented 7 years ago

Another example may be the set of sampling interval or histeresis for pins, even if this is maybe more pin-related and thus may be moved as BLEGeneralIO callback function

giowild commented 7 years ago

@drkangel we need your Sandroide skills here :)

drkangel commented 7 years ago

On the emulated UART is created through BLE you can communicate anything... for example GPIO info and commands like for Arduino RedBear device. The problem is the understanding between the two device: once you retrieve data from emulated UART: what do they mean? how can I use them? In case of RedBear GPIO the description of the codification explain the data and how to use them. If you want to create another 'Object' like the GPIO you should define dictionary and rules shared between the two device and describe them on the xml file (take RedBear GPIO as example)

giowild commented 7 years ago

@drkangel Just wondering: what should i put in xml files(devices, bledeviceparsers and bledataclustermodels) to let a device send and receive, over TX and RX characteristic, a simple byte array of variable length?

All examples I can find, like the one below, contain fixed length formats with primitive types uint8, char8 and so on. Also the runAction method in BLEAction seems to accept only List of floats...

What if I need to receive and send a simple byte array with variable length? Can you make an example on how to modify xml files?

                      <model>
                          <id>set_model_0</id>
                          <static>
                          <id>comm</id>
                              <value>T</value>
                              <format>char8</format>
                              <position>0-0</position>
                          </static>
                          <static>
                          <id>pin</id>
                              <value>0</value>
                              <format>uint8</format>
                              <position>1-1</position>
                          </static>
                          <data>
                              <id>value</id>
                              <format>uint8</format>
                              <position>2-2</position>
                          </data>
                      </model>