outputstream.write not executing Eclipse android bluetooth..probably all the connections are being made.. but senddata() ain’t writing the data

      //i am trying to connect my phone to arduino through bluetooth module
     //my app can't write the data
       //the scanning and connecting part is working fine
     //the relevant part of the code the rest look to work fine
     // 
     //
      //
     senddata.setOnClickListener(new View.OnClickListener() //send button
     {

        @Override
        public void onClick(View v) {

            setup();

            try {
                sendData(); //calling to write

            } catch (IOException e) {

                e.printStackTrace();

            }


        }
    });
    void setup() // to setup 
    {

    try {
        goandsend();

    } catch (Exception e1) {

        e1.printStackTrace();
    }
    try {
        openBT();

    } catch (IOException e) {

        e.printStackTrace();

    }

}
void goandsend() //looking for connected device
{
if(devicelist.size()>0)
for(BluetoothDevice b:devicelist)
    {
    if(b.getBondState()==BluetoothDevice.BOND_BONDED)
    {
        id=b.getAddress(); //get the bonded device
    devicetosend=b;
     break;
    }


    }
  void openBT() throws IOException { //making connections to send
    try {
        // Standard SerialPortService ID
        UUID uuid = UUID.fromString(id);
        mmSocket = devicetosend.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();   //connecting
        //getting the streams to in and out
       mmOutputStream = mmSocket.getOutputStream();
        mmInputStream = mmSocket.getInputStream();



                } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// This will send data to be printed by the arduino

void sendData() throws IOException { //writing
    try {
     blue.cancelDiscovery();
        // the text typed by the user
        String msg = datafield.getText().toString();


     byte[] b=msg.getBytes();
    //writing and trying to flush out the buffer
       mmOutputStream.write(b);
       mmOutputStream.flush();

        // tell the user data were sent
       Toast.makeText(this, "Sent", Toast.LENGTH_SHORT).show();

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/*the app is connecting properly..
and the connection is made.
but cant write anything..
nclude all relevant tags

After you post, leave the suggestions for a code, and see if anyone can help me. If anyone can provide the extra code or so,plz respond by editing the question .
*/


Source: eclipse

Leave a Reply