开发者

Interfacing Android Nexus One with Arduino + BlueSmirf

开发者 https://www.devze.com 2022-12-26 13:48 出处:网络
I\'m a bit new to all of this, so bear with me - I\'d really appreciate your help. I am trying to link the Android Nexus One with an arduino (Duemilanove) that is connected to a BlueSmirf.I have a pr

I'm a bit new to all of this, so bear with me - I'd really appreciate your help.

I am trying to link the Android Nexus One with an arduino (Duemilanove) that is connected to a BlueSmirf. I have a program that is simply outputting the string "Hello Bluetooth" to whatever device the BlueSmirf is connected to. Here is the Arduino program:

void setup(){ Serial.begin(115200); int i; }

void loop(){Serial.print("Hello Bluetooth!"); delay(1000); }

One my computer BT terminal I can see the message a开发者_开发技巧nd connect no problem. The trouble is with my android code. I can connect to the device with android, but when I look at the log it is not displaying "Hello Bluetooth". Here is the debug log:

04-09 16:27:49.022: ERROR/BTArduino(17288): FireFly-2583 connected

04-09 16:27:49.022: ERROR/BTArduino(17288): STARTING TO CONNECT THE SOCKET

04-09 16:27:55.705: ERROR/BTArduino(17288): Received: 16

04-09 16:27:56.702: ERROR/BTArduino(17288): Received: 1

04-09 16:27:56.712: ERROR/BTArduino(17288): Received: 15

04-09 16:27:57.702: ERROR/BTArduino(17288): Received: 1

04-09 16:27:57.702: ERROR/BTArduino(17288): Received: 15

04-09 16:27:58.704: ERROR/BTArduino(17288): Received: 1

04-09 16:27:58.704: ERROR/BTArduino(17288): Received: 15

ect...

Here is the code, I'm trying to put only the relative code but if you need more please let me know:

private class ConnectThread extends Thread {
    private final BluetoothSocket mySocket;
    private final BluetoothDevice myDevice;

    public ConnectThread(BluetoothDevice device) {
        myDevice = device;
        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK");
        }
        mySocket = tmp;
    }
    public void run() {
        Log.e(TAG, "STARTING TO CONNECT THE SOCKET");
        InputStream inStream = null;
        boolean run = false;
        //...More Connection code here...

The more relative code is here:

        byte[] buffer = new byte[1024];
        int bytes;

        // handle Connection
        try {
            inStream = mySocket.getInputStream();
            while (run) {
                try {
                    bytes = inStream.read(buffer);
                    Log.e(TAG, "Received: " + bytes);
                } catch (IOException e3) {
                    Log.e(TAG, "disconnected");
                }
            }

I am reading bytes = inStream.read(buffer). I know bytes is an integer, so I tried sending integers over bluetooth because "bytes" was an integer but it still didn't make sense.

It almost appears that is sending incorrect baud rate. Could this be true?

Any help would be appreciated. Thank you very much.


Did you see this project? http://code.google.com/p/android-arduino/

Cheers


read() returns the number of bytes it successfully read into the buffer. Therefore, in this line of code:

bytes = inStream.read(buffer);

…your message will be found in the first bytes bytes of buffer (assuming everything else is correct). You can convert those to a String like so:

String message = new String(buffer, 0, bytes);

I'm glossing over a number of things here (encoding, concatenating multiple buffers, etc) but this should get you started.

0

精彩评论

暂无评论...
验证码 换一张
取 消