setText()没有改变我的textView()

时间:2013-06-28 14:16:15

标签: java android bluetooth textview

我尝试显示我从蓝牙设备收到的文字。 我收到文本,我将其更改为字符串,我可以在Logcat中显示它,但它不会出现在我的textView中... 我尝试了不同的方法,比如在接收到消息之后放置setText(),但没有任何作用......任何想法?

我的功能:

package com.example.blueconnect;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
    BluetoothAdapter blueAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        blueAdapter = BluetoothAdapter.getDefaultAdapter();
        if (blueAdapter == null) {
            Log.e("Error","The device has no Bluetooth.");  
        }
        else{
            if (!blueAdapter.isEnabled()) blueAdapter.enable();
        }

        //Do the Bluetooth visible
        /*Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
        */
        //Start the server
        AcceptThread blueNect = new AcceptThread();
        blueNect.start();
    }

     private class AcceptThread extends Thread {
            // The local server socket
            private BluetoothServerSocket mmServerSocket;

            public AcceptThread() {
            }

            public void run() {         
                BluetoothSocket socket = null;
                BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
                String lineRead = "";

                // Listen to the server socket if we're not connected
                while (true) {

                    try {
                        //récupération de l'élément
                        TextView tvPD = (TextView) findViewById(R.id.tvPD);
                        Log.i("MESSAGE",lineRead);
                        tvPD.setText(lineRead);

                        // Create a new listening server socket
                        Log.d("TAG", ".....Initializing RFCOMM SERVER....");

                        // MY_UUID is the UUID you want to use for communication
                        mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("ServeurBluetooth", UUID.fromString("30fa7085-2927-4f90-8da7-b9a86c398373"));                    
                        //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);  you can also try using In Secure connection...

                        // This is a blocking call and will only return on a
                        // successful connection or an exception                    
                        socket = mmServerSocket.accept();                   


                    } catch (Exception e) { }

                    try {
                        Log.d("TAG", "Closing Server Socket.....");                    
                        mmServerSocket.close();

                        InputStream tmpIn = null;
                        OutputStream tmpOut = null;

                        // Get the BluetoothSocket input and output streams

                        tmpIn = socket.getInputStream();
                        tmpOut = socket.getOutputStream();

                        DataInputStream mmInStream = new DataInputStream(tmpIn);
                        DataOutputStream mmOutStream = new DataOutputStream(tmpOut); 


                        //Read message
                        BufferedReader bReader2=new BufferedReader(new InputStreamReader(mmInStream));
                        lineRead=bReader2.readLine();
                        Log.i("MESSAGE",lineRead);

                      //send response to spp client
                        PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream));
                        pWriter.write("Response String from SPP Server\r\n");
                        pWriter.flush();
                        Log.d("TAG", "Respond sent to the CLient");

                        pWriter.close();

                        // here you can use the Input Stream to take the string from the client whoever is connecting
                        //similarly use the output stream to send the data to the client
                    } catch (Exception e) {
                        //catch your exception here
                    }

                }
            }

        }
}

我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

    <include
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/header"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

        <TextView
            android:id="@+id/tvPD"
            style="@style/TitleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Devices" />

    </RelativeLayout>
</RelativeLayout>

希望你能帮助我...

2 个答案:

答案 0 :(得分:2)

tvPD.setText(lineRead);

lineRead已初始化为""且从未修改过。这就是为什么你的TextView始终没有显示的原因。

修改:您位于与Thread不同的UI Thread内,并且对所有对UI元素的修改都应在UI Thread上完成。您必须使用HandlerrunOnUiThread

答案 1 :(得分:0)

在你的textview中你的写行lineRead

中有一个错误

更改此代码:

   Log.i("MESSAGE", lineread);       
   tvPD.setText(lineRead);

由:

   Log.i("MESSAGE", lineread);       
   tvPD.setText(lineread);

我希望能帮到你