Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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
Android bluetooth:java.io.IOException:未连接传输端点_Java_Android_Exception_Bluetooth - Fatal编程技术网

Android bluetooth:java.io.IOException:未连接传输端点

Android bluetooth:java.io.IOException:未连接传输端点,java,android,exception,bluetooth,Java,Android,Exception,Bluetooth,我在Android中的蓝牙串行连接有问题。有两个场景: 一个活动(带片段)、一个EditTex和一个按钮(片段中)。该按钮建立与串行设备的连接,并从EditText发送数据。这是工作,但是。。。我不想每次都建立连接 一个活动(带片段)、一个EditTex和两个按钮(片段中)。一个按钮建立连接,另一个按钮发送数据,好的,当我按下连接按钮时,似乎工作正常(无例外),但当我按下发送数据按钮时,logcat显示: “java.io.IOException:未连接传输终结点” 当然,数据不会到达接收器。例

我在Android中的蓝牙串行连接有问题。有两个场景:

  • 一个活动(带片段)、一个EditTex和一个按钮(片段中)。该按钮建立与串行设备的连接,并从EditText发送数据。这是工作,但是。。。我不想每次都建立连接

  • 一个活动(带片段)、一个EditTex和两个按钮(片段中)。一个按钮建立连接,另一个按钮发送数据,好的,当我按下连接按钮时,似乎工作正常(无例外),但当我按下发送数据按钮时,logcat显示:

  • java.io.IOException
    :未连接传输终结点”

    当然,数据不会到达接收器。例外情况是抛出

    out.write(txtData.getText().toString().getBytes());
    
    我的代码是(我想要的第二个场景):


    编辑:这不是另一个蓝牙设备的问题,因为我在google play中用一个应用程序测试了它,它可以工作。

    在发送数据之前,你确定这两个设备已经连接好了吗


    另外,您还应该在一个单独的线程中而不是在主线程上进行所有蓝牙连接。

    最后,我使用
    createInsurerCommsockettoServiceRecord(SERIAL_UUID)
    解决了我的问题,而不是
    createrCommsockettoServiceRecord(SERIAL_UUID)

    这两个设备是连接在一起的。但是所有的代码都在主线程(ui线程)中,因为我只想连接然后发送数据一次,我不想监听数据。连接另一端的设备是什么?连接后立即写入是有效的还是两者都会导致错误?一个设备是my android(2.3),另一个设备是串行设备,通过串行usb转换器连接到PC。在第二个场景中(一个按钮用于建立连接,另一个按钮用于发送数据),我只在尝试发送数据时出错(它会引发异常),当我建立连接时它不会引发错误。很抱歉,我不知道如何帮助您。
    public class MainFragment extends Fragment implements View.OnClickListener {
        private EditText txtData;
        private Button btnSend;
        private Button btnConnect;
        private InputStream in = null;
        private byte[] buffer = null;
        BluetoothSocket socket = null;
        OutputStream out = null;
    
        public MainFragment(){}
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            txtData = (EditText) rootView.findViewById(R.id.txtData);
            btnSend = (Button) rootView.findViewById(R.id.btnSend);
            btnConnect = (Button) rootView.findViewById(R.id.btnConnect);
            btnConnect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    connectBluetooth();    
                }
            });
            btnSend.setOnClickListener(this);
            return rootView;
        }
    
        public void connectBluetooth(){
            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
            if (adapter == null) {
                // Device does not support Bluetooth
                getActivity().finish(); //exit
            }
    
            if (!adapter.isEnabled()) {
    //make sure the device's bluetooth is enabled
                Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBluetooth, 0);
            }
    
            final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
            String mac = "00:1B:35:0B:75:BE"; //my device's mac adress
            BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
    
    
            // Get a BluetoothSocket to connect with the given BluetoothDevice
    
            try {
                socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
            } catch (IOException e) {
                Log.d("socket","Error al crear el socket");
            }
    
            try {
    
                socket.connect();    
                out = socket.getOutputStream();
                in = socket.getInputStream();
                out.write(txtData.getText().toString().getBytes());
            } catch (IOException e) {
            }
        }
    
        @Override
        public void onClick(View view) {
            try {    
                out.write(txtData.getText().toString().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }