4
votes

I'm trying to send data from an android device to a remote bluetooth device supporting Serial Port Profile(SPP). I notice whenever I open and close a socket after each press of a button, it is too slow. What socket commands should be executed in the Run() and Onclick() functions? The following is a class which does Bluetooth IO:

public class Selecteddevice extends Activity implements OnClickListener {

private static final String TAG = "THINBTCLIENT";
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice device;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;

private static final UUID MY_UUID = 
        UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static String address;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.selecteddevice);
    findViewById(R.id.toggleButton1).setOnClickListener(this);
    findViewById(R.id.toggleButton2).setOnClickListener(this);
    findViewById(R.id.toggleButton3).setOnClickListener(this);

}

@Override
public void onStart() {
    super.onStart();

    String address = getIntent().getStringExtra("address");
    TextView tv1 = (TextView) findViewById(R.id.textView_address);
    tv1.setText("                       DEVICE ADDRESS:      " +  address);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    device = mBluetoothAdapter.getRemoteDevice(address);
    run();
}

public void run(){
    mBluetoothAdapter.cancelDiscovery();
    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

    } catch (IOException e) 
    {   
        Log.e(TAG, "ON START: Socket creation failed.", e);
    }

    try {
        btSocket.connect();

    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: Socket connection failed.", e);
    }   
}

public void sendTestString(String s){
    try {
        outStream = btSocket.getOutputStream();

    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: OutputStream creation failed.", e);
    }

    try {
        outStream.write(s.getBytes());
        Log.d(TAG, "sendTestByte: OutputStream write succeeded.");

    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: OutputStream writefailed.", e);
    }       
}


public void onClick(View v){
    switch(v.getId())
    {
    case R.id.toggleButton1:
        this.sendTestString("1");
        break;
    case R.id.toggleButton2:
        this.sendTestString("2");
        break;
    case R.id.toggleButton3:
        this.sendTestString("3");
        break;
    }
}           

@Override
public void onPause() {
    super.onPause();
    if (outStream != null) {
        try {
            outStream.flush();
        } catch (IOException e5) 
        { 
            Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e5);
        }
    }
    try {
        btSocket.close();
    } catch (IOException e6) 
    {
        Log.e(TAG, "ON PAUSE: Unable to close socket.", e6);
    }
}

@Override
public void onStop() {
    super.onStop();

}

@Override
public void onDestroy() {
    super.onDestroy();

}

}


My program crashes after pairing with the error message:

    07-27 13:00:57.483: E/THINBTCLIENT(7855): sendTestByte: OutputStream writefailed.
    07-27 13:00:57.483: E/THINBTCLIENT(7855): java.io.IOException: socket closed
    07-27 13:00:57.483: E/THINBTCLIENT(7855):   at  
    android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:331)
    ...

What am I doing wrong?

Thanks.

1
What would be a minimal test case for the sort of Bluetooth I/O you need. Is it possible for all of the Bluetooth operations to happen inside of one test function? If you could provide that it might be easier to debug.speciousfool
Do you have the right permissions set in the manifest?ligi
I am having the same problem.. Did u get any solution???Zacharias Manuel

1 Answers

3
votes

If you are sure the connection is established without any error and you can get the socket, try assigning your OutputStream member in run() method as follows:

public void run()
{
    mBluetoothAdapter.cancelDiscovery();
    try 
    {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);           
    } catch (IOException e) 
    {   
        Log.e(TAG, "ON START: Socket creation failed.", e);
    }

    try 
    {
        btSocket.connect();     
    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: Socket connection failed.", e);
    }   

    try 
    {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: OutputStream creation failed.", e);
    }
}

public void sendTestString(String s)
{    
    try 
    {
        outStream.write(s.getBytes());
        outSttream.flush(); // <-- Try flush to force sending data in buffer
        Log.d(TAG, "sendTestByte: OutputStream write succeeded.");
    } catch (IOException e) 
    {       
        Log.e(TAG, "sendTestByte: OutputStream writefailed.", e);
    }       
}

You are not actually closing socket but this should work. Make sure connection with master device is not lost before write() call