各位大佬,我最近和队友一起在做安卓蓝牙与嵌入式系统蓝牙模块的通信,嵌入式端经测试能够正常接收和发送数据,安卓端也能够正常接收嵌入式端发送的数据,但在安卓端向嵌入式端发送数据时嵌入式端没有接收到数据,但安卓端也没有报错,求大佬支招!

解决方案 »

  1.   

    安卓端数据发送代码如下:
    private void sendMessageHandle(final String msg)
        {
            if (mSocket == null)
            {
                Toast.makeText(this, "未连接到设备", Toast.LENGTH_SHORT).show();
                return;
            }
            mBluetoothAdapter.cancelDiscovery();
            if(mSocket.isConnected()){
                try {
                    OutputStream os = mSocket.getOutputStream();
                    os.write(hexStringToBytes(msg));
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            mSocket.connect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                try {
                    OutputStream os = mSocket.getOutputStream();
                    os.write(hexStringToBytes(msg));
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    }