从arduino董事会接收价值,面临问题

时间:2015-01-22 10:39:41

标签: android arduino handler android-bluetooth

我能够从arduino接收值但是接收该东西的一个小问题是来自arduino的值以122 | 126的形式并且我将它设置为处理程序中的文本视图问题是当我调试它时显示实际值(正确的格式(122 | 133)),但是当我运行它时显示其他一些格式,如 122 |,2 | 133,23 | 122

我尝试了很多来解决这个问题,请帮我解决这个问题。 这是我的源代码,

BluetoothDevice myBlueDevice;
BluetoothAdapter bluetoothAdpter;
TextView contentWrite;
OutputStream mmOutputStream;
InputStream mmInputStream;
String contentMsg;
Activity act;
boolean status;
String read;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
Handler bluetoothIn;
BluetoothSocket tmp;

final int handlerState = 0; // used to identify handler message
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder recDataString = new StringBuilder();

private ConnectedThread mConnectedThread;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_after_connection);

    contentWrite = (TextView) findViewById(R.id.content);
    contentWrite.setMovementMethod(new ScrollingMovementMethod());

    Intent getAddr = getIntent();
    String blueAddress = getAddr.getStringExtra("Blueaddr");

    bluetoothAdpter = BluetoothAdapter.getDefaultAdapter();
    myBlueDevice = bluetoothAdpter.getRemoteDevice(blueAddress);

    final UUID SERIAL_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB"); // UUID for
                                                                    // serial
                                                                    // connection

    tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        Method m = myBlueDevice.getClass().getMethod("createRfcommSocket",
                new Class[] { int.class });
        tmp = (BluetoothSocket) m.invoke(myBlueDevice, 1);
    } catch (Exception e) {

    }

    bluetoothAdpter.cancelDiscovery();

    try {
        // This is a blocking call and will only return on a
        // successful connection or an exception
        tmp.connect();

    } catch (IOException e) {
        try {
            tmp.close();
        } catch (IOException e2) {
        }
        return;
    }
    if (tmp.isConnected()) {

        mConnectedThread = new ConnectedThread(tmp);
        mConnectedThread.start();

        // I send a character when resuming.beginning transmission to check
        // device is connected
        // If it is not an exception will be thrown in the write method and
        // finish() will be called
        mConnectedThread.write("x");

        bluetoothIn = new Handler() {
            public void handleMessage(android.os.Message msg) {

                // String readMessage = (String) msg.obj; // msg.arg1 =
                // bytes

                contentWrite.setText(contentMsg);

            }
        };

    }
}

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

    // creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            // Create I/O streams for connection
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[10];
        int bytes;

        // Keep looping to listen for received messages
        while (true) {
            try {
                bytes = mmInStream.read(buffer); // read bytes from input
                                                    // buffer

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

                contentMsg = read; // this is the value I am displaying in textview.
                bluetoothIn.obtainMessage(handlerState, bytes, -1, read)
                        .sendToTarget();

                // Send the obtained bytes to the UI Activity via handler

            } catch (Exception e) {
                break;
            }

        }
    }

    // write method
    public void write(String input) {
        byte[] msgBuffer = input.getBytes(); // converts entered String into
                                                // bytes
        try {
            mmOutStream.write(msgBuffer); // write bytes over BT connection
                                            // via outstream
        } catch (IOException e) {
            // if you cannot write, close the application
            Toast.makeText(getApplicationContext(),
                    "Connection Failure" + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            // finish();

        }
    }
}

@Override
public void onStop() {
    super.onStop();

    Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_SHORT)
            .show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(getApplicationContext(), "onDestroy()",
            Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

如果只是格式化问题,您可以在handleMessage(android.os.Message msg)中根据需要过滤消息。

似乎蓝牙正在传递数组,因此将其提供给setText方法会在运行时生成逗号分隔列表