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

时间:2019-02-25 22:50:32

标签: java android bluetooth

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

            }
        }
    };

1 个答案:

答案 0 :(得分:0)

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