Thursday, March 29, 2012

How to use BlueMesh Tutorial

This post is a basic tutorial on how to use BlueMesh in your own android project.  Source code for this tutorial was taken from WebViewHost and WebViewTest.

To begin, you can consider the BlueMeshService object both a device for input and output.  This object provides functionality for both reading data from all devices on the BlueMesh network and writing data to all devices on the BlueMesh network.

As a prerequisite to using BlueMesh, a device should enable Bluetooth and pair with other devices.  It is not necessary to be paired with all devices that are going to be on the network, but there should be a path from every device to every other device via Bluetooth pairs.

The first thing that needs to be done when using BlueMesh is create a BlueMeshService object.
try{ bms = new BlueMeshService(); } catch(NullPointerException e){ Log.e(TAG, "Bluetooth not enabeled"); return; }

It is important to note that the BlueMeshService() constructor throws a NullPointerException if Bluetooth is not enabled.  If this happens it means that the constructor failed and you should not proceed, rather you should either end the program, or attempt to enable Bluetooth and try again.

If the constructor completes successfully, it is then time to call launch().  launch begins the Bluetooth communications (listening for other devices running the same app to connect to) and allows you to use the IO functions.
bms.launch();

Now that BlueMesh is running it is time to use the IO functions.  Remember that the UI thread must complete in order to render any graphical display, so it is recommended that BlueMesh IO is preformed in a separate thread.

The following code block shows how to set up a button to send data over BlueMesh.  The write(byte[]) function takes one argument, a byte array, and sends it to all devices on the network.  In this example slides is an ArrayList of Strings.
//button listener send final Button buttonSend = (Button) findViewById(R.id.btnSend); buttonSend.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if( bluetoothWorking ){ bms.write(slides.get(currentSlide).getBytes()); } } });

In order to read data, use the byte[] pull() function.  This function will return a byte array containing a received message on success and null if there is no message to receive.  This call is non-blocking and will return immediately, please note that it is not recommended that this function be called instantly over and over again in a read loop without a small sleep between attempts.

The following code block shows how to create a read thread the polls the BlueMeshService object for incoming messages.  In this example, the read thread passes messages to the UI thread via Handler mHandler.
private final Handler mHandler = new Handler(){ @Override public void handleMessage( Message msg ){ byte[] byteMessage = (byte[]) msg.obj; String stringMessage = byteMessage.toString(); //Here you can do UI with the message } }; private class ReadThread extends Thread { public void run(){ Looper.myLooper(); Looper.prepare(); while (true){ if( this.isInterrupted()){ Log.d(TAG, "readThread interrupted"); return; } byte bytes[] = null; bytes = bms.pull(); if( bytes == null){ //Sleep if nothing is received to avoid //pounding bms try { sleep(100); } catch (InterruptedException e) { Log.e(TAG, "sleep() failed", e); } } else{ mHandler.obtainMessage (0, bytes.length, -1, bytes) .sendToTarget(); //Here you could process the message //and send a response } } } }

Now that your program can read and write over BlueMesh the only thing that remains is shutting down communications.  This requires a simple call to disconnect() which can be run as the program shuts down.
@Override public void onDestroy(){ super.onDestroy(); readThread.interrupt(); bms.disconnect(); }

Hopefully you enjoyed this simple tutorial on BlueMesh and will be able to develop your own apps.  If you have any questions please email me at schnej7@rpi.edu.

Tuesday, March 27, 2012

Slideshow Presentations over BlueMesh

Our new test app works with BlueMesh now!  There are two new apps (that work together) developed for BlueMesh that are now in the repository in the WebView branch in the directory "WebViewTest" (the audience app) and "WebViewHost" (the presenter app).

WebViewHost:
The presenter app is fairly simple.  It simply takes strings consisting of html pages and sends them over BlueMesh when the "send" button is pushed.

WebViewTest:
The client app is slightly more complicated than the presenter app because the view needs to be updated during run-time as new slides come in.  This means that BlueMesh needs to be run in it's own thread and pass messages to a handler in the UI thread.  It is important to note that a looper must be prepared if BlueMesh is run in a separate thread than the UI thread.



Stay tuned for a more in depth blog post about how this app works and how to use BlueMesh.

Sunday, March 25, 2012

Test Metric Redefined

As programming of our test programs goes on, some of our goals have changed.

The "Fastest Bounce" program works through the CLD, intermittently. We seemed to be getting a bounce time of roughly 1 to 2 milliseconds, though this varied greatly. Also, the program only gets through a few bounces before crashing. We believe that this is due to the CLD front-end, so we are currently trying to implement this test with a stripped-down front-end that will avoid tampering with test results.
Also, as a side note, our last entry didn't include that accuracy will also be tested, even though we haven't seen anything to even remotely suggest that accuracy has been an issue.

The "Largest Message" program will be a moot point initially, since BlueMesh has a maximum message size of 1024 bytes. This will be added to the API. The "Fastest Bounce" test will be repeated for max size, however, to try and obtain a max throughput. As a later goal, we may want to try editing this maximum, but that would require serious code rewrites and is being tabled.

Friday, March 23, 2012

Testing Metrics and BlueMesh

We have been working on developing a number of metric test programs to try and find the technical limitations and performance ability of BlueMesh. Hopefully, we'll be able to get some useful and important data concerning performance and limitations for developers.

Right now, the tests we're trying to create and the questions we have are:
- Fastest Bounce : How quickly can BlueMesh send a small message from device A to device B and back? How much does this time change with N intermediate devices?

- Largest Message: What's the largest message that can reliably be sent through BlueMesh? Does this result in a significant slowdown? What's the greatest data throughput we can achieve with BlueMesh, and what affects it?

I'm considering adding a "Proliferation" test, to see how quickly a message can be sent to a large number of devices, but due to a lack of testing devices, this will likely have to wait.

Fastest Bounce has already been written and is on the git's development branch. It has not yet been shown to work, nor has it been run for numbers. Since I need two phones at least to run a test, achieving this has been difficult, but should be accomplished soon.

Wednesday, March 14, 2012

A New Test App!

Hey Guys!

I've been thinking of new ways to use BlueMesh and I have come up with some great ideas.  I just posted a REALLY EARLY version of what will be a presentation app that uses BlueMesh to distribute slides between android devices.  The idea is that you will have a presentation and the app will be able to distribute additional notes to the audience.

"How will this work?" you might ask...

The slides will be created using HTML in order to make their distribution across BlueMesh fast and light-weight.  Once the slides are distributed, they will be rendered using Android's WebView object.  The viewer will then have the option to move freely in the slides he/she has received, or jump to the newest slide each time it is received.

If you have any other ideas about what this app should do, ideas for other apps using BlueMesh, or ideas for the BlueMesh API, please post comments on this blog, post on the github, or email me at schnej7@rpi.edu

Thursday, March 1, 2012

New Snapshot For Your Testing Pleasure

We have a new and improved working snapshot of CLD chat using BlueMesh. The improvements from the last snapshot include:

  1. Quit button from menu used to stop BlueMesh.
  2. BlueMesh stops correctly.
  3. No more annoying <NOTHING> messages to indicate that the service is running.
  4. Display also displays the messages that are sent from it, not just messages recieved.
  5. CLD now appends the name of the device to the message before sending it off.

Known bugs include:
  1. When device A and B are connected and device A disconnects, sometimes device B crashes when it tries to quit.
  2. When devices disconnect, they do not reconnect.
  3. Messages do not always get received because some unique IDs are not unique.
  4. When the device is tilted you forfeit the ability to send messages from the UI.

PLEASE, download and install the .apk and try it out!  You can get it from www.bluemeshproject.com or by scanning this QR-Code.
NOTE: you will need to pair the devices you want to use on the network BEFORE you start the app