Sorry here's the rest of it:
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "lib/Tlc5940/Tlc5940.h"
int bluetoothTx = 6;
int bluetoothRx = 7;
extern HardwareSerial Serial;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
char buffer[3];
int left[] = {13, 14, 12};
int right[] = {29, 30, 28};
void setup()
{
Tlc.init();
Tlc.setAll(4095);
Tlc.update();
//Setup usb serial connection to computer
Serial.begin(57600);
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);
}
void set(int channel[3], char buffer[3]) {
Tlc.set(channel[0], 4095);
Tlc.set(channel[1], 4095);
Tlc.set(channel[2], 4095 - 16*buffer[2]);
Tlc.update();
}
void loop() {
while (!bluetooth.available() || (bluetooth.available() && (bluetooth.read() != 0xFF))); // wait till we hit 0xFF
while (bluetooth.available() < 3); // Wait until we have 3 bytes waiting
bluetooth.readBytes(buffer,3);
Serial.write(buffer);
set(left, buffer);
}
And the Java from Android (mostly not mine) is:
package com.example.bluetoothtest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView myLabel;
EditText myTextbox;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openButton = (Button)findViewById(R.id.open);
Button sendButton = (Button)findViewById(R.id.send);
Button closeButton = (Button)findViewById(R.id.close);
myLabel = (TextView)findViewById(R.id.label);
myTextbox = (EditText)findViewById(R.id.entry);
//Open Button
openButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
findBT();
openBT();
}
catch (IOException ex) { }
}
});
//Send Button
sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
sendData();
}
catch (IOException ex) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//Close button
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
closeBT();
}
catch (IOException ex) { }
}
});
}
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
Log.v("BT2", "Device: " + device.getName());
if(device.getName().equals("BT UART"))
{
mmDevice = device;
break;
}
}
}
myLabel.setText("Bluetooth Device Found");
}
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
myLabel.setText("Bluetooth Opened");
}
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
myLabel.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void sendData() throws IOException, Exception {
for (int i = 1; i < 255; i=+3) {
mmOutputStream.write(0xFF);
mmOutputStream.write(i);
mmOutputStream.write(i+1);
mmOutputStream.write(i+2);
Thread.sleep(50);
}
}
void closeBT() throws IOException
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
myLabel.setText("Bluetooth Closed");
}
}
Originally I thought it was because I was missing closing null bytes on the transmission (as advised on another forum thread), but that made no difference (I think my arduino code was wrong then), so I've taken them out again. The 1st half (approx half anyway) means the numbers 1-100, after that the numbers start skipping. e.g I'll get 0x64, 0x65, 0x69, 0x72 etc on the serial monitor.
The obviousy problem is an Uno only has one h/w serial, so it's tricky to debug. Maybe I should swap the bluetooth onto the h/w and the PC on the software one and see if it improves...
I've just melted my bluetooth adapter somehow as well(!), so I'll have to wait for another one to arrive now! Smoke is probably a bad sign 