บลูทูธรับข้อมูลจาก HC05 ไม่ทำงาน ฉันได้รับข้อมูลขยะนี้����

ใครช่วยอธิบายให้ฉันฟังว่ามีอะไรผิดปกติกับรหัสต่อไปนี้ ฉันลองทั้งหมดแล้ว: เพิ่มขีดจำกัด \n\r เพื่อรอจุดสิ้นสุดของบรรทัดและวิธีอื่นๆ อีกหลายวิธี ฉันได้รับข้อมูลขยะอยู่เสมอ ดังนั้นฉันจึงกลับไปที่รหัสเริ่มต้นของฉัน

บางคนบอกฉันว่าอาจเป็น InputStream ซึ่งไม่ได้รับข้อมูล แต่นี่ไม่ได้ให้วิธีแก้ปัญหาแก่ฉัน

 private class ConnectedThread extends Thread {
    private final InputStream mmInStream;

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

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

        mmInStream = tmpIn;
    }

    public void run() {
        byte[] buffer = new byte[1024]; // I tried all 128, 8, 256
        int bytes;
        while (true) {
            try {
                if (mmInStream.available()>0){
                    bytes = mmInStream.read(buffer);//read bytes from input buffer
                    bluetoothIn.obtainMessage(handlerState, bytes, -1, buffer).sendToTarget();
                }
            }

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


private void setw() throws IOException {

    blue_tv = findViewById(R.id.blue_tv);
    blue_tv2 = findViewById(R.id.blue_tv2);
    bluetoothConnectDevice();
    mConnectedThread = new ConnectedThread(bluetoothSocket);
    mConnectedThread.start();
}

private void bluetoothConnectDevice() throws IOException {

    try {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        blue_address = bluetoothAdapter.getAddress();
        pairedDevice = bluetoothAdapter.getBondedDevices();
        if (pairedDevice.size() > 0) {
            for (BluetoothDevice bt : pairedDevice) {
                blue_address = bt.getAddress();
                blue_name = bt.getName();
                Toast.makeText(getApplicationContext(), "Cane connected", Toast.LENGTH_SHORT).show();
                blue_status = "La canne est connectée !";

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //get mobile bluetooth device
    BluetoothDevice bd = bluetoothAdapter.getRemoteDevice(blue_address);//connect to the device
    bluetoothSocket = bd.createInsecureRfcommSocketToServiceRecord(myUUID); //create a RFCOM (SPP) connexion
    bluetoothSocket.connect();
    try {
        blue_tv.setText("Bluetooth Name : " + blue_name + "\nBluetooth Adress : " + blue_address);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_traject);
    mySR = SpeechRecognizer.createSpeechRecognizer(this);
    autoCompleteTextView = findViewById(R.id.actv);

    try {
         setw();
    }catch (Exception e){
         e.printStackTrace();
    }

    blue_status = "La canne n'est pas connectée.";

    bluetoothIn = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == handlerState) {
                byte[] readBuff = (byte[]) msg.obj;
                String readMessage = new String(readBuff,0,msg.arg1);
                blue_tv2.setText("Data Received = " + readMessage+"\n"+"Data Length = "+readMessage.length());

            }
        }
    };

person Savastaro Vegas    schedule 25.02.2019    source แหล่งที่มา


คำตอบ (1)


ไม่มีอะไรผิดปกติที่นี่ ฉันคิดว่าคุณได้รับข้อมูลที่ถูกต้องจาก HC05 อย่างไรก็ตาม ไม่สามารถพิมพ์อักขระบางตัวได้เนื่องจาก Bluetooth ส่งข้อมูลให้คุณเป็นไบต์ และทุกไบต์ไม่สามารถแปลงเป็นอักขระที่ถูกต้องได้ในขณะที่คุณพยายามตั้งค่าเป็นข้อความภายใน TextView

ฉันขอแนะนำให้คุณดำเนินการ InputStream ก่อน ก่อนที่จะวางลงใน TextView ตัวอย่างเช่น อ่านไบต์ที่ได้รับก่อนในอาร์เรย์ไบต์ แล้วสร้าง String ด้วยตัวเองดังต่อไปนี้

public String toString() {
    String print = "";
    for (int i = 0; i < buffer.length; i++) print += " " + buffer[i];
    return "Buffer (size=" + buffer.length + " content:" + print;
}
person Reaz Murshed    schedule 25.02.2019