Thursday, June 21, 2012
Bluetooth ServerThread Interface Design
I've managed to work out BlueCove sufficiently enough to use it. Its syntax is identical to that of J2ME. J2ME is a version of Java that is designed to work with small devices, hence its built-in API support of Bluetooth. BlueCove, however, lets us use these same commands with J2SE (Standard Edition) and your basic computer running Windows or OSX.
I have also learned how to use these commands to write client-side and server-side algorithms to get devices to communicate. A useful, if slightly unclear, example can be found on CodeGuru.com. This gives us an input and output stream to work with. Since BlueMesh ultimately uses just an input & an output stream, this should be fine. However, BlueMesh's ServerThread currently passes a "BluetoothSocket" object instead, and that object is deconstructed to an input and output portion in the ReadWriteThread. So, BlueMesh is going to require a back-end redesign.
The ServerThread-like Interface will need to export an input and output stream. This is because the input and output streams are the lowest common denominator in the Android API and the J2ME API. The J2ME API gets these streams from the StreamConnection object, while the Android API gets them from the BluetoothAdapter object. Each object is specific to its own API, and cannot be included in an agnostic BlueMesh. So, the ServerThread will have to send only the input-output stream and some sort of device-identifying data (Device Name? Random Serial Number?) to the RouterObject. It will then be the RouterObject's job to find out if that device is already connected and to set up the ReadWriteThread for it.
Further, the way that J2ME handles clients is unusual. Specifically, it does not generate a BluetoothSocket object, so ClientThread will have to be reworked as well. The J2ME client is implemented with a virtual class, "DiscoveryListener", with four purely-virtual functions that are called when certain things happen, such as discovering a device. I am unsure as to whether or not this class is automatically a thread or really much about clients in J2ME at all. More research will have to be done.
Hopefully, I will just be able to extract from one of these functions/objects the input and output streams, so that the overall design of BlueMesh can be preserved. Otherwise, the ServerThread may be the only object able to set up communication and pass it to the router, meaning the client-side ClientThread will have to invoke the ServerThread to create two-way communication. I want to avoid this, so hopefully it won't be necessary.
Wednesday, May 30, 2012
Interfacing with others!
Hello, All!
As detailed in the last post, BlueMesh will be trying to make the move towards greater compatibility. Specifically, we're looking towards creating a Java interface that will encapsulate all of the proprietary portions of Bluetooth. So, if you are willing to write an interface class for your platform, then BlueMesh will work on it.
That's the vision. Practically, BlueMesh will have to come with a couple of common interfaces. Luckily, I found another open-source project, BlueCove, that will allow Microsoft, OS X, and a few other platforms to do just that. Specifically, I no longer have to write code for these machines specifically; I can use this program as a bridge. More importantly, I hope that BlueCove will give me some insight as to what exactly the BlueMesh interface should expect and contain, so that any interfaces can be written easily.
This task, as large as it is, is one of my greater priorities, for two reasons.
1) It will give BlueMesh greater compatibility and intercommunication
2) It will allow for testing with the library itself to be done one any one platform, and it can be a platform of choice. The Android SDK is nice enough, but doesn't allow for easy debugging. If this interface is successfully written, then BlueMesh can be tested, and eventually operated, between any two Bluetooth-enabled devices.
That's the vision. Practically, BlueMesh will have to come with a couple of common interfaces. Luckily, I found another open-source project, BlueCove, that will allow Microsoft, OS X, and a few other platforms to do just that. Specifically, I no longer have to write code for these machines specifically; I can use this program as a bridge. More importantly, I hope that BlueCove will give me some insight as to what exactly the BlueMesh interface should expect and contain, so that any interfaces can be written easily.
This task, as large as it is, is one of my greater priorities, for two reasons.
1) It will give BlueMesh greater compatibility and intercommunication
2) It will allow for testing with the library itself to be done one any one platform, and it can be a platform of choice. The Android SDK is nice enough, but doesn't allow for easy debugging. If this interface is successfully written, then BlueMesh can be tested, and eventually operated, between any two Bluetooth-enabled devices.
Monday, May 21, 2012
Over the summer
Hey guys,
I just wanted to write a quick post about what will be going on with BlueMesh this summer. Sean will be working on the project with RCOS at RPI and he has a lot of really awesome things that he plans to do. I will be working up by Boston this summer, but I will still try to help out as much as possible.
Some of the changes we hope to implement are as follows:
1) Port BlueMesh to Java. Currently BlueMesh only works on Android devices, we plan to port it to Java so it will run on PCs as well. This will make development much easier and quicker. Once we have a working version in Java it should be quick and painless to port it back to Android.
2) Implement message chunking. Message chunking will allow us to determine the optimum maximum size of a message and allow us to chunk messages into this maximum size in order to allow for maximum throughput.
3) Implement routing algorithm. This will allow for point to point communication rather than flooding the network with every write.
There are other changes that are also in speculation right now in addition to these three. I'm sure Sean will be updating us with his progress often and I look forward to seeing BlueMesh progress.
-Jerry
I just wanted to write a quick post about what will be going on with BlueMesh this summer. Sean will be working on the project with RCOS at RPI and he has a lot of really awesome things that he plans to do. I will be working up by Boston this summer, but I will still try to help out as much as possible.
Some of the changes we hope to implement are as follows:
1) Port BlueMesh to Java. Currently BlueMesh only works on Android devices, we plan to port it to Java so it will run on PCs as well. This will make development much easier and quicker. Once we have a working version in Java it should be quick and painless to port it back to Android.
2) Implement message chunking. Message chunking will allow us to determine the optimum maximum size of a message and allow us to chunk messages into this maximum size in order to allow for maximum throughput.
3) Implement routing algorithm. This will allow for point to point communication rather than flooding the network with every write.
There are other changes that are also in speculation right now in addition to these three. I'm sure Sean will be updating us with his progress often and I look forward to seeing BlueMesh progress.
-Jerry
Monday, April 23, 2012
So close to our first release!
Currently we are one bug fix away from our first release of BlueMesh!
This week I added in an argument to the BlueMeshService constructor so that you can specify a UUID for your program. This is important because you probably don't want your BlueMesh program to interact with other BlueMesh programs.
This is how you can call the constructor now:
A UUID is specified by hex digits in the following format 8-4-4-4-12. The UUID can also be used to enable or disable backwards compatibility of your app! If you would like a new version of your app to be able to talk to an older one, simply do not change the UUID between versions, however if your network protocol changes between versions you can specify a new UUID so that a new version will not communicate with an older version.
The bug we are still trying to fix involves not properly detecting a disconnection and deleting information about that device so that a re-connection will be attempted and successfully completed. I hope we will have this fixed within the next few weeks and we will try to keep you updated on what we think might be causing it.
This week I added in an argument to the BlueMeshService constructor so that you can specify a UUID for your program. This is important because you probably don't want your BlueMesh program to interact with other BlueMesh programs.
This is how you can call the constructor now:
bms = new BlueMeshService(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
A UUID is specified by hex digits in the following format 8-4-4-4-12. The UUID can also be used to enable or disable backwards compatibility of your app! If you would like a new version of your app to be able to talk to an older one, simply do not change the UUID between versions, however if your network protocol changes between versions you can specify a new UUID so that a new version will not communicate with an older version.
The bug we are still trying to fix involves not properly detecting a disconnection and deleting information about that device so that a re-connection will be attempted and successfully completed. I hope we will have this fixed within the next few weeks and we will try to keep you updated on what we think might be causing it.
Monday, April 9, 2012
WebViewTest Bug fixes
This weekend I fixed a minor bug in WebViewTest (can be found at http://scrambledeggs.myrpi.org/APKs/WebViewTest.apk): The app no longer crashes on start if Bluetooth is not enabled. I also added more comments to the source so it will be easier to follow.
Please try out the tutorial and try to make your own app using BlueMesh!
Please try out the tutorial and try to make your own app using BlueMesh!
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.
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.
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.
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.
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.
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.
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.
Subscribe to:
Posts (Atom)
