繁体   English   中英

蓝牙无法从HC05接收数据。 我收到了这个垃圾数据

[英]Bluetooth receive data from HC05 desn't work. I receive this garbage data ����

有人可以向我解释以下代码有什么问题吗? 我尝试了所有方法:添加限制\\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());

            }
        }
    };

这里没有错。 我认为您正在从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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM