0
votes

I have followed this example: http://english.cxem.net/arduino/arduino5.php

I have a arduino uno board set up with a sparkfun bluetooth device. I can connect and send data from android to arduino and I see this data pop up in the serial monitor, but I can not send data from the arduino(serial monitor) and back to Android.

I am using a ConnectThread that is started in the onResume method in the activity. This is the code for my thread:

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d("THREAD", "constructor");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.d("THREAD", "inside run" );
            byte[] buffer = new byte[256];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                Log.d("in loop", "waiting for data");
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                    h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                    Log.d("recieve", "b " + bytes);
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(String message) {
            Log.d("TAG", "...Data to send: " + message + "...");
            byte[] msgBuffer = message.getBytes();
            try {
                mmOutStream.write(msgBuffer);
            } catch (IOException e) {
                Log.d("TAG", "...Error data send: " + e.getMessage() + "...");     
              }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }



    }

Do I need to use a service that always waits for data since the serial monitor is only sending data when I press send?

EDIT: Arduino code:

    #include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

   void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    char c = (char) Serial.read();
     // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print(c); 
  }
  // and loop forever and ever!
}
1
Hi oivindth, I also faced the similar problem as yours and I also happens to refer to the same source as you have. You mentioned you solved this using a Looper.prepare in the thread, could you please explain a bit more or it would be great if you could update your solution on the codes above. Thanks a bunch.Nicholas TJ
Hi @NicholasTJ. I simply added Looper.prepare(); in my Run() method. If this doesen't help I can update post with code.oivindth
I got it running, but not with Looper.prepare(), oh well, i think i got to look into Looper.prepare() and see what it does. But if you are ok with me being lazy, an update on your code in your post would be great.Nicholas TJ

1 Answers

1
votes

No you don't need a service, a thread should work fine. This code is practically identical to the code provided in the BluetoothChat sample provide through the Android SDK so it should work.

By using the serial monitor, you are sending data to the Arduino, so I assume you are echoing that data back using Serial.println("data string") or something similar. However, since you followed the article you cited, you have the Bluetooth chip hooked up to the RX/TX pins (0 and 1) on the Arduino, which is the same pins used by the Serial Monitor. I've found that when the Bluetooth chip is hooked up to these, the serial monitor still receives data but can no longer send it. So your problem is on the Arduino side.

Also, this StackOverflow answer mentions that you can't use both the Serial Monitor and Bluetooth on pins 0 and 1 at the same time.

If you still want to use the Serial Monitor, hook up the Bluetooth chip to different digital pins and use the SoftwareSerial Arduino library as described here.

I've made a simple modification to the Bluetooth Chat sample as described in this post and my Android has no problem receiving from the Arduino.