显示和读取蓝牙Seria数据的问题

时间:2014-04-09 12:13:30

标签: android bluetooth serial-port

我看过这篇文章:

Displaying Bluetooth Serial Data

它没有任何解决方案。

我有同样的问题。我的代码没用,因为它似乎在读取,但它不会在我创建的TextView上显示它。

最后它突然碾碎了!

如何在TextView上阅读和显示我收到的数据?

这是我的代码:

Handler mHandler = new Handler(){
    public void handleMessage(Message msg) {
        Log.i(tag,"in handler");
        super.handleMessage(msg);
        switch(msg.what){
        case SUCCESS_CONNECT:
            //DO SOMETHING
            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
            Toast.makeText(getApplicationContext(), "Conectar", Toast.LENGTH_SHORT).show();
            String s = "conectado con Éxito";
            connectedThread.write(s.getBytes());
            Log.i(tag,"connected");
            byte[] iniciar = new byte[]{(byte)0x05};
            // B01/n en Hexadecimal (Introducir para que Espirómetro envíe datos
            //byte [] datos = new byte[] {(byte)0x42, (byte)0x30, (byte)0x31, (byte)0x0D};
            byte [] datos = new byte[] {(byte)0x43, (byte)0x0D};
            connectedThread.write(iniciar);
            Log.i(tag, "waiting for commands");
            Log.i(tag, datos.toString());
            //Get Data
            connectedThread.write (datos);
            Log.i(tag,"Receiving Data");
            connectedThread.run(); 
            //connectedThread.cancel();
            break;

        case MESSAGE_READ:
            byte[] readBuf = (byte[])msg.obj;
            /*
            String string = new String(readBuf);
            Log.i(tag, string);
            Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
            */
            Log.i(tag,"He estado aquí");
            String readMessage = new String(readBuf, 0, msg.arg1);
            mTextView.setText(readMessage);
            //added by AMJ in attempt to display variable in textview

            break;
        }
        super.handleMessage(msg);
    }
};

ConnectedThread类:

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

    public ConnectedThread(BluetoothSocket socket) {
        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() {
       /* byte[] buffer;  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {



                buffer = new byte[1024];
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();

            } catch (IOException e) {
                break;
            }
        }*/

        Log.i(tag, "run success");
        byte[] buffer = new byte[2];
        int bytes; // bytes returned from read() 

        // Keep listening to the InputStream until an exception occurs 
        while (true) {
            try {
                // Read from the InputStream 
                bytes = mmInStream.read(buffer);
                Log.i(tag, "buffer read OK");
                // Send the obtained bytes to the UI activity 
                String str = new String(buffer); 
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, str).sendToTarget();
            }catch (Exception e) {
                Log.i(tag, "buffer read failed");
                System.out.print("read error"); 
                break;
            }
        }           

    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

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

}

谢谢!

(这是LOG):

04-03 22:22:49.528: I/debugging(13221): construct
04-03 22:22:49.537: I/BluetoothSocket_MTK(13221): [JSR82] Bluetooth Socket Constructor
04-03 22:22:49.551: I/BluetoothSocket_MTK(13221): [JSR82] type=1 fd=-1 auth=true     encrypt=true port=-1
04-03 22:22:49.556: D/dalvikvm(13221): create interp thread : stack size=32KB
04-03 22:22:49.556: D/dalvikvm(13221): create new thread
04-03 22:22:49.557: D/dalvikvm(13221): new thread created
04-03 22:22:49.557: D/dalvikvm(13221): update thread list
04-03 22:22:49.557: D/dalvikvm(13221): threadid=13: interp stack at 0x5e081000
04-03 22:22:49.557: D/dalvikvm(13221): threadid=13: created from interp
04-03 22:22:49.557: D/dalvikvm(13221): start new thread
04-03 22:22:49.557: I/debugging(13221): in click listener
04-03 22:22:49.560: D/dalvikvm(13221): threadid=13: notify debugger
04-03 22:22:49.562: D/dalvikvm(13221): threadid=13 (Thread-953): calling run()
04-03 22:22:49.572: I/debugging(13221): connect-run
04-03 22:22:49.573: I/BluetoothSocket_MTK(13221): [JSR82] connect: do SDP
04-03 22:22:56.408: I/BluetoothSocket_MTK(13221): [JSR82] SdpHelper::onRfcommChannelFound: channel=1
04-03 22:22:56.408: I/BluetoothSocket_MTK(13221): [JSR82] connect: do SDP done; mPort=1
04-03 22:22:56.959: I/debugging(13221): connect-succeded
04-03 22:22:56.959: D/dalvikvm(13221): threadid=13: exiting
04-03 22:22:56.961: I/debugging(13221): in handler
04-03 22:22:56.964: D/dalvikvm(13221): threadid=13: bye!
04-03 22:22:56.969: D/skia(13221): Flag is not 10
04-03 22:22:57.046: I/debugging(13520): connected
04-03 22:23:14.049: I/debugging(13520): waiting for commands
04-03 22:23:14.050: I/debugging(13520): [B@41d1c590
04-03 22:23:31.057: I/debugging(13520): Receiving Data
04-03 22:23:31.058: I/debugging(13520): run success
04-03 22:23:31.061: I/debugging(13520): buffer read OK
04-03 22:23:31.064: I/debugging(13520): buffer read OK
04-03 22:23:31.067: I/debugging(13520): buffer read OK
04-03 22:23:31.070: I/debugging(13520): buffer read OK
04-03 22:23:31.073: I/debugging(13520): buffer read OK
04-03 22:23:31.076: I/debugging(13520): buffer read OK
04-03 22:23:31.078: I/debugging(13520): buffer read OK
04-03 22:23:31.082: I/debugging(13520): buffer read OK
04-03 22:23:31.085: I/debugging(13520): buffer read OK
04-03 22:23:31.087: I/debugging(13520): buffer read OK
04-03 22:23:31.091: I/debugging(13520): buffer read OK
04-03 22:23:31.158: I/debugging(13520): buffer read OK
04-03 22:23:31.158: I/debugging(13520): buffer read OK
04-03 22:23:37.503: I/ViewRootImpl(13520): No key event currently.
04-03 22:23:37.510: I/ViewRootImpl(13520): No motion event currently.
04-03 22:23:37.510: I/ViewRootImpl(13520): The current processed event of VRI is none
04-03 22:23:37.510: I/ViewRootImpl(13520): notify IMS Dump
04-03 22:23:37.545: D/ANRAppManager(13520): MSG HISTORY IN MAIN THREAD:
04-03 22:23:37.545: D/ANRAppManager(13520): Current kernel time : 28387613ms
04-03 22:23:37.545: D/ANRAppManager(13520): === LONGER MSG HISTORY IN MAIN THREAD ===
04-03 22:23:37.545: D/MessageQueue(13520): Dump first 20 messages in Queue: 
04-03 22:23:37.546: D/MessageQueue(13520): Dump Message in Queue (1): { what=0 when=-57s502ms callback=android.widget.Toast$TN$1@41cea490 target=Handler (android.os.Handler) {41cea5e8} }
04-03 22:23:37.546: D/MessageQueue(13520): Dump Message in Queue (2): { what=0 when=-55s499ms callback=android.widget.Toast$TN$2@41cea548 target=Handler (android.os.Handler) {41cea5e8} }
04-03 22:23:37.546: D/MessageQueue(13520): Dump Message in Queue (3): { what=1 when=-6s484ms arg1=2 arg2=-1 obj=?? target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.546: D/MessageQueue(13520): Dump Message in Queue (4): { what=1 when=-6s482ms arg1=2 arg2=-1 obj= target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.547: D/MessageQueue(13520): Dump Message in Queue (5): { what=1 when=-6s480ms arg1=2 arg2=-1 obj= target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.547: D/MessageQueue(13520): Dump Message in Queue (6): { what=1 when=-6s476ms arg1=2 arg2=-1 obj=0 target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.547: D/MessageQueue(13520): Dump Message in Queue (7): { what=1 when=-6s473ms arg1=2 arg2=-1 obj=18 target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.547: D/MessageQueue(13520): Dump Message in Queue (8): { what=1 when=-6s471ms arg1=2 arg2=-1 obj=36 target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.547: D/MessageQueue(13520): Dump Message in Queue (9): { what=1 when=-6s468ms arg1=2 arg2=-1 obj=00 target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.548: D/MessageQueue(13520): Dump Message in Queue (10): { what=1 when=-6s466ms arg1=2 arg2=-1 obj=NE target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.548: D/MessageQueue(13520): Dump Message in Queue (11): { what=1 when=-6s463ms arg1=2 arg2=-1 obj=WD target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.548: D/MessageQueue(13520): Dump Message in Queue (12): { what=1 when=-6s460ms arg1=2 arg2=-1 obj=EV target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.548: D/MessageQueue(13520): Dump Message in Queue (13): { what=1 when=-6s457ms arg1=2 arg2=-1 obj=IC target=Handler (net.londatiga.android.bluetooth.DeviceListActivity$1) {418e92f8} }
04-03 22:23:37.548: D/MessageQueue(13520): Dump Message in Queue (14): { what=1 when=-6s454ms arg1=2 arg2=-1 obj=E

target = Handler(net.londatiga.android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.549:D / MessageQueue(13520):队列中的转储消息(15):{what = 1 when = -6s449ms arg1 = 2 arg2 = -1 obj =     04-03 22:23:37.549:D / MessageQueue(13520):0 target = Handler(net.londatiga.android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.549:D / MessageQueue(13520):队列中的转储消息(16):{what = 1 when = -6s447ms arg1 = 2 arg2 = -1 obj = 14 target = Handler(net.londatiga。 android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.549:D / MessageQueue(13520):队列中的转储消息(17):{what = 1 when = -6s446ms arg1 = 2 arg2 = -1 obj = 79 target = Handler(net.londatiga。 android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.549:D / MessageQueue(13520):队列中的转储消息(18):{what = 1 when = -6s445ms arg1 = 2 arg2 = -1 obj = 00 target = Handler(net.londatiga。 android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.550:D / MessageQueue(13520):队列中的转储消息(19):{what = 1 when = -6s445ms arg1 = 2 arg2 = -1 obj =:4 target = Handler(net.londatiga) .android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.550:D / MessageQueue(13520):队列中的转储消息(20):{what = 1 when = -6s445ms arg1 = 2 arg2 = -1 obj = 23 target = Handler(net.londatiga。 android.bluetooth.DeviceListActivity $ 1){418e92f8}}     04-03 22:23:37.554:D / MessageQueue(13520):总消息数:92     04-03 22:23:37.608:I / dalvikvm(13520):threadid = 3:对信号3作出反应     04-03 22:23:37.609:D / dalvikvm(13520):转储本机堆栈:main     04-03 22:23:37.609:D / dalvikvm(13520):放松回溯     04-03 22:23:37.621:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.622:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.622:D / dalvikvm(13520):完成原生     04-03 22:23:37.625:D / dalvikvm(13520):转储本机堆栈:Binder_2     04-03 22:23:37.626:D / dalvikvm(13520):放松回溯     04-03 22:23:37.628:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.628:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.628:D / dalvikvm(13520):完成原生     04-03 22:23:37.629:D / dalvikvm(13520):转储本机堆栈:Binder_1     04-03 22:23:37.629:D / dalvikvm(13520):放松回溯     04-03 22:23:37.631:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.632:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.632:D / dalvikvm(13520):完成原生     04-03 22:23:37.633:D / dalvikvm(13520):转储本机堆栈:编译器     04-03 22:23:37.633:D / dalvikvm(13520):放松回溯     04-03 22:23:37.634:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.635:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.635:D / dalvikvm(13520):完成原生     04-03 22:23:37.635:D / dalvikvm(13520):转储本机堆栈:JDWP     04-03 22:23:37.635:D / dalvikvm(13520):放松回溯     04-03 22:23:37.636:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.637:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.637:D / dalvikvm(13520):完成原生     04-03 22:23:37.637:D / dalvikvm(13520):转储本机堆栈:GC     04-03 22:23:37.637:D / dalvikvm(13520):放松回溯     04-03 22:23:37.639:D / dalvikvm(13520):获得原生符号     04-03 22:23:37.639:D / dalvikvm(13520):格式回溯线     04-03 22:23:37.639:D / dalvikvm(13520):ghttps://stackoverflow.com/questions/21922000/displaying-bluetooth-serial-dataet native done     04-03 22:23:37.639:D / dalvikvm(13520):准备'/data/anr/traces.txt'的堆栈跟踪     04-03 22:23:37.639:I / dalvikvm(13520):将堆栈跟踪写入'/data/anr/traces.txt'

0 个答案:

没有答案