Friday, February 1, 2013

Music Glove Explained

I realize (thanks to a twitter follower) that I never really explained how my music glove actually works.
(Introducing Proof of Concept Music Glove)

Here goes.

The glove connects to the arduino over I/O. (basically just use some I/O pins to read a pressed state).

I wrote some code (referenced by this post: ) to transmit over the arduino's RS232 (Serial) port. The RN-42 uses RS232 to communicate to the device, so as long as the baud rate, etc are the same, then sending data from the Arduino to the RN-42 is a breeze.

Next we need a way to read the data. Download the bluetooth chat application from google (located here: ), modify it where the line says
In the BluetoothChatService.java
From
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
To
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
view raw gistfile1.txt hosted with ❤ by GitHub

Next I added some code to read received data.

The receive message section should look something like this:

case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (readMessage.contains("A"))
{
String action = prefs.getString("KeyCodeA","");
HandleActionOfButton(action);
}
if (readMessage.contains("B"))
{
String action = prefs.getString("KeyCodeB","");
HandleActionOfButton(action);
}
if (readMessage.contains("C"))
{
String action = prefs.getString("KeyCodeC","");
HandleActionOfButton(action);
}
if (readMessage.contains("D"))
{
String action = prefs.getString("KeyCodeD","");
HandleActionOfButton(action);
}
if (readMessage.contains("E"))
{
String action = prefs.getString("KeyCodeE","");
HandleActionOfButton(action);
}
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
break;
view raw gistfile1.java hosted with ❤ by GitHub

Changing Volume, songs, etc, are all located in the following function I stuffed in there.
public boolean HandleActionOfButton(String action)
{
Toast.makeText(getApplicationContext(), "Action " + action, Toast.LENGTH_SHORT).show();
if (action.contains("Volume Up"))
{
mAudioManager.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI
);
//Toast.makeText(getApplicationContext(), "Volume Up", Toast.LENGTH_SHORT).show();
return true;
}
else if (action.contains("Volume Down"))
{
mAudioManager.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI
);
//Toast.makeText(getApplicationContext(), "Volume Up", Toast.LENGTH_SHORT).show();
return true;
}
else if (action.contains("Previous"))
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "previous");
sendBroadcast(i);
return true;
}
else if (action.contains("Next"))
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "next");
sendBroadcast(i);
return true;
}
else if (action.contains("Mute"))
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "mute");
sendBroadcast(i);
return true;
}
else if (action.contains("TogglePause"))
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "togglepause");
sendBroadcast(i);
return true;
}
else if (action.contains("Stop"))
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "stop");
sendBroadcast(i);
return true;
}
/*<item>Previous</item>
<item>Next</item>
<item>Volume Up</item>
<item>Volume Down</item>
<item>Mute</item>
<item>TogglePause</item>
<item>Stop</item>*/
return false;
}
view raw gistfile1.java hosted with ❤ by GitHub

Of course you need a preferences to deal with things but you don't have to, you can simply just say if you see A, then do the action listed for the one you want in the above code.
Put it all together and you have a device which will change the volume or songs (if you set it up via preferences). Remember that some of the options are not publicly declared intents so they work right now, but may change in the future. If you have further questions go ahead and leave a comment, or find me on twitter @onaclov2000.