RdlP
RdlP

Reputation: 1396

Android bluetooth: java.io.IOException: Transport endpoint is not connected

I have a problem with bluetooth serial connection in Android. There is two scenaries:

  1. One Activity (with fragment), one EditTex and one Button (in fragment). The button establish the connection with serial device and send data from EditText. It's work, but... I don't want establish the connection every time.

  2. One Activity (with fragment), one EditTex and two Button (in fragment). One button establish the connection and the other send data, ok, when I push the connection button it's seem work fine (no exception) but when I push the send data button the logcat show me:

"java.io.IOException: Transport endpoint is not connected"

and of course, the data don't arrive to the receiver. The exception is throw in

out.write(txtData.getText().toString().getBytes());

My code is (The second scenary that is I want):

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();
        }

    }

Edit: It is not problem of the other Bluetooth device because I tested it with a app in the google play and it works.

Upvotes: 2

Views: 2874

Answers (1)

RdlP
RdlP

Reputation: 1396

Finally I solved my problem using createInsecureRfcommSocketToServiceRecord(SERIAL_UUID) instead createRfcommSocketToServiceRecord(SERIAL_UUID)

Upvotes: 1

Related Questions