The Build:
I went to Ben’s to get a PS3 controller to assist him with a controller mod and he mentioned working on an upcoming Android episode. I told him I had a little experience with Android and bluetooth control and I offered my assistance. He came up with an idea of making a remote pot stirrer so you can stir and monitor your cooking from another room. We talked throughout the week about the basic look and he gave me some graphics to use.
As I was busy most of the week, I ended up doing the program Friday night. I had some sample code from my other apps, but strange Android errors still made it take me until 3AM before I finished a working product. I had weird layout issues, it seemed to be something to do with the main.xml file. I would move something and I’d get casting errors that crashed the program when it ran on the phone. I’d delete a few objects and re-add them, doing nothing in the java code, and the problem would go away. The biggest offender was the image button. I could not line the device id textview up with it otherwise I’d get one of those crashes. When doing the layout xml’s, I made it specifically for my phone, which included editing some of the graphics. I figured I’d port the program to Ben’s nook or phone the next day.
I showed up on Saturday for taping. Ben had the hardware for the stirrer complete. It was an arm on a stand that moved a spoon back and forth using continuous rotation servos and limit switches. As we had discussed the protocol over email, the firmware was pre-coded and working from a terminal. I also had my app working with a terminal.
Despite having both of our devices working separately, getting them to work together didn’t work straight out of the box. First, our baudrate on the Arduino was incorrect. We were using the Roving Networks RN41 module and those modules default at 115,200 which I forgot. After we figured that out, we had to get our continuous rotation servo working correctly. We simplified our code to just send exactly what the android phone sends, to the arm via pwm, so 0-180 with 90 being stop. This fixed that issue. And of course whenever you have an issue, you ended up “fixing” everything else until you find the real problem and have to go “unfix” what you had changed.
When trying to get this ported to the nook, I had problems. First of all, I needed to find usb dev drivers for the nook, not too bad. Then the version of Android I developed for 4.03 or Ice Cream Sandwich, was too new for the nook. I revered the project to 2.3, or Gingerbread, but then I had to take out the rotates as Gingerbread didn’t support the imagview.rotate command. I also had to change the layout a lot to get things to line up. I had edited Ben’s files to work on my phone and I had to edit them back to what he originally made.
Once I got it installed, I tried to get the bluetooth working. It would not, the nook, no matter what I did, would talk to the module. It couldn’t even pair. It would fail and give an error. Without even a successful pair, it was dead in the water.
Next, I tried Ben’s phone. This was harder, the drivers were not readily available to connect for development. I had to put his phone into recovery mode first, which windows then took over and installed the drivers. The phone took forever to reboot and I was worried I had broke it as I had to take out the battery to get it to leave recovery mode. It finally did boot though.
I tried to send the apk to the phone through eclipse. It didn’t work. No matter what I did, it couldn’t see his device as a target even with USB debugging enabled. I ended up having to put his phone into mass storage mode and dropping the apk on there. I got it installed on the phone and gave it a try. It would pair to the Roving Networks module, but would never connect. I’m not sure if it supports SPP or Serial Port Profile, which is a Bluetooth mode, kind of like HID or audio protocol. In the end I had to use my phone. We had to fix a few code problems on the receiver side and then we were up and running, except for the thermistor. We only worked half a day so Ben said he’d add the thermistor code that night and I would port the program back to my phone and re-add the rotate command for ICS.
I came in on Sunday for a few hours to finish up. Ben added the thermistor code and we started getting some sending errors back to the phone. We removed the old sending code, which sent the speed value received as a temp, it was a debugging tool of ours and no longer needed, and then it appeared to work on the terminal. I was still getting erratic behavior on the device when I plugged into Ben’s universal wall wart. I thought the previous day we had used a 5V supply, but apparently we must have used the 12V line to power the Arduino board. With the drop caused by the regulator, the Arduino wasn’t getting enough power to work correctly. After that fix, everything was working great. We finished taping the function and that was that. Not without problems, but definitely not as hectic as Robot Luggage.
Program Information:
I wrote this program based on some samples of previous programs I’ve written for Bluetooth Control. Much of the Bluetooth code was used and Adapted from the book “Programming Android”.
AboutInfoActivity- This file is all about showing the “About” section when you hit the options key. It just shows information I put in the about.xml file explaining the protocol
DeviceListActivity- This pertains to choosing a bluetooth device. After you hit the sync button, a screen comes up letting you connect to a bluetooth device.
BtHelperHandler and BtSPPHelper- These are the bluetooth control classes. They are pretty much exactly from “Programming Android” except I had to add some code to allow me to receive data. These codes I believe were adapted from the bluetooth chat example on Google’s code database.
BenSturActivity- This is the main activity. It is what you interface with for the program. It controls the other activities, uses the bluetooth, reads the seekbar and button inputs, and sends the bluetooth information every 250mS by use of a thread. You sync with a device and it will show the Device ID, you know its connected when the temperature changes from — to a number. Then you slide your finger back and forth on the slider in the upper right to move the arm. Releasing puts the position back to 90, or off. That’s about it.
Protocol- Its a pretty simple protocol with everything sent in Ascii. I did this because I was having trouble getting useful Byte data from the bluetooth helper classes but strings worked fine.
Sending- Start character – Three digits of position, always 3 digits, padded with 0’s when neccessary
ex- !090 (stop) !180 (full CW) !000 (full CCW)
Receving- Start character – One to Three digits of temperature, does not need to be 0 stuffed
ex- !99 !120
Known Issues- When coming back from a screen off condition (like a sleep), the program often crashes from an inflater error. Something about unknown class. I’ll see if I can fix it, but I’m not an Android nor Java king. If anyone else looks at this code and fixes it, let me know so I can update it.
Older Support- Because of the ImageView.rotate command, this is ICS and above only. Remove the ImageView rotation command and then configure the project for an older version of Android to work on older phones. Also remember to update the Minimum Version in the Android Manifest !
Eclipse Project file and APK is attached:
BenStur Files
Android Market Link:
Ben Stur