Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过蓝牙从Raspberry pi接收字符串_Java_Android_Python_Bluetooth - Fatal编程技术网

Java 通过蓝牙从Raspberry pi接收字符串

Java 通过蓝牙从Raspberry pi接收字符串,java,android,python,bluetooth,Java,Android,Python,Bluetooth,我正在开发一个android应用程序,通过蓝牙从raspberry pi 2串行接收字符串 代码工作正常,但只有BeginToListentData不工作 package com.first.majazen.appfinal; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluet

我正在开发一个android应用程序,通过蓝牙从raspberry pi 2串行接收字符串

代码工作正常,但只有BeginToListentData不工作

package com.first.majazen.appfinal;


import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import android.os.Handler;


public class Recieve extends Activity {
    private static String mystuff;
    private static final String TAG = mystuff;
    BluetoothSocket mmSocket = null;
    BluetoothDevice mmDevice = null;
    BluetoothAdapter mBluetoothAdapter;
    DataInputStream mmInputStream;
    Thread workerThread;
    byte[] readBuffer;
    int readBufferPosition;
    volatile  boolean stopWorker;
    EditText text;


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

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        text = (EditText)findViewById(R.id.text);
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
            Log.i("myStuff", "Bluetooth Enabled");
        } else {
            Log.i("myStuff", "Bluetooth Already Enabled");
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                if (device.getName().equals("RNBT-E965")) {
                    mmDevice = device;
                    Log.i("myStuff", "Device equals " + device.getName());
                    try {
                        openBT();
                    } catch (IOException e) {
                        Log.i("myStuff", "BT not opened");
                    }
                }}
        }




    }

    public void openBT()throws IOException {


        UUID uuid = mmDevice.getUuids()[0].getUuid(); //if you don't know the UUID of the bluetooth device service, you can get it like this from android cache
        Log.d(TAG, "UUID: " + uuid.toString());
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        Log.i("myStuff", "Connected OK!");
        mmInputStream = new DataInputStream(mmSocket.getInputStream());
        beginListenForData()
    }


    public void BackActivity(View view) {
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }


   public void beginListenForData() {
      final Handler handler = new Handler();
        final byte delimiter = 10;

       stopWorker = false;
        readBufferPosition =0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()&& !stopWorker){
                    try{
                        text.setText("manal");

                        int bytesAvailable = mmInputStream.available();
                        if(bytesAvailable >0){
                            byte[] packetBytes =new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);
                            for (int i=0; i<bytesAvailable; i++){
                                byte b = packetBytes[i];
                                if (b== delimiter){
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;
                                    handler.post(new Runnable() {
                                        public void run(){
                                            text.setText(data);

                                            Log.i("myStuff", "Received");

                                        }
                                    });
                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }
                    }catch (IOException ex){
                        stopWorker = true;
                        Log.i("myStuff", "not Received");

                    }
            }
        }
        }); workerThread.start();
    }




}
这是我的日志

请帮我解决这个问题

04-24 15:52:41.591 3991-6131/com.first.majazen.appfinal E/AndroidRuntime: FATAL EXCEPTION: Thread-65679
                                                                          Process: com.first.majazen.appfinal, PID: 3991
                                                                          android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                              at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8251)
                                                                              at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1307)
                                                                              at android.view.View.requestLayout(View.java:20148)
                                                                              at android.view.View.requestLayout(View.java:20148)
                                                                              at android.view.View.requestLayout(View.java:20148)
                                                                              at android.view.View.requestLayout(View.java:20148)
                                                                              at android.view.View.requestLayout(View.java:20148)
                                                                              at android.widget.TextView.checkForResize(TextView.java:8357)
                                                                              at android.widget.TextView.spanChange(TextView.java:9404)
                                                                              at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:12067)
                                                                              at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1057)
                                                                              at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:763)
                                                                              at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:662)
                                                                              at android.text.Selection.setSelection(Selection.java:125)
                                                                              at android.text.Selection.setSelection(Selection.java:134)
                                                                              at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:325)
                                                                              at android.widget.TextView.setText(TextView.java:4976)
                                                                              at android.widget.TextView.setText(TextView.java:4834)
                                                                              at android.widget.EditText.setText(EditText.java:117)
                                                                              at android.widget.TextView.setText(TextView.java:4809)
                                                                              at com.first.majazen.appfinal.Recieve$1.run(Recieve.java:99)
                                                                              at java.lang.Thread.run(Thread.java:818)
第99行,替换

    text.setText(data);

如错误日志中所述,您正试图从GUI线程以外的其他线程修改GUI。这是禁止的。
另外,尝试将此代码段封装到另一个方法中以提高可读性。

它在第126行E/AndroidRuntime:FATAL EXCEPTION:Thread-67716 Process:com.first.majazen.appfinal,PID:8616 java.lang.ArrayIndexOutOfBoundsException:length=1024;index=1024位于com.first.majazen.appfinal.Recieve$1.run(Recieve.java:126)第126行:readBuffer[readBufferPosition++]=b;将缓冲区大小更改为:if(bytesavable>0&&readBufferPosition<1024)仍然是readBuffer的错误:它给了我这个错误:04-24 19:31:40.331 2646-3729/com.first.majazen.appfinal E/AndroidRuntime:FATAL异常:Thread-76971进程:com.first.majazen.appfinal,PID:2646 java.lang.ArrayIndexOutOfBoundsException:length=1024;index=1024位于com.first.majazen.appfinal.Recieve$1.run(Recieve.java:131)位于java.lang.Thread.run(Thread.java:818),您正试图在缓冲区之外进行写操作。它的大小由您自己限制为1024字节。此外,您正在测试缓冲区可用性,并尝试在两种不同的条件下读取它。尝试记录bytesAvailable的内容。它应该能帮助你理解这个问题。此外,请使用适当的缩进和间距。你的代码几乎不可读。这给了我另一个问题。。“threads take ms”请帮助:(
    text.setText(data);
    Recieve.this.runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            text.setText(data);
        }
    });