1

我尝试显示从蓝牙设备收到的文本。我收到文本,我将其更改为字符串,我可以在 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>

希望你能帮助我...

4

2 回答 2

2
tvPD.setText(lineRead);

lineRead被初始化""并且从不修改。这就是为什么你的TextView节目总是什么都没有。

编辑:你在一个Thread不同的内部UI Thread ,对 UI 元素的所有修改都应该在UI Thread. 你必须使用HandlerrunOnUiThread

于 2013-06-28T14:18:20.243 回答
0

你的文本视图中的 lineread 有错误你的 write lineRead

更改此代码:

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

经过:

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

我希望这对你有帮助

于 2013-06-28T14:20:12.263 回答