TheNuggster
TheNuggster

Reputation: 25

Android Bluetooth SPP connection to seems to go dead after a couple of seconds

I have a fairly simple program based largely on a simple bluetooth test client application posted here:

http://www.anddev.org/code-snippets-for-android-f33/serial-over-bluetooth-simple-test-client-t11106.html

My application has 4 buttons, each button sending a different byte of data over the bluetooth connection.

It seems to work just fine for a few seconds. The connection is made, the RFCOMM socket connects and for the first few seconds data is sent over the over the connection (and recieved at the other side) After a few seconds of perfection, however, the data stops getting through. Then no matter which of the 4 buttons I press nothing happens.

Then when I press the "Exit" button (which attempts to close the bluetooth socket using the .close() function) suddenly all the data that had not been getting through suddenly goes through all at once (as if it was being stored up in a buffer) immediatly before the connection is closed. and the recieving device goes back into discovery mode.

I do not understand why the connection drops, and starts storing the data up, any Ideas?

Thanks, James

Target: Galaxy Tab @ Android 2.3.3
Recieving Device: TI EZ430-RF2560 eval kit

    package com.launcher.LaunchControl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;


import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class ThinBTClient extends Activity implements OnClickListener {

        private static final String TAG = "THINBTCLIENT";
        public static final String ADDRESS = "ADDRESS";
        private static final boolean D = true;
        private BluetoothAdapter mBluetoothAdapter = null;
        private BluetoothSocket btSocket = null;
        private OutputStream outStream = null;
        byte [] msgBuffer = {0x01, 0x02, 0x03, 0x04};

        private static final UUID MY_UUID =         //Bluetooth UUID to resolve to SSP Port 1 ^^
                        UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

        private static String address;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.mainlaunch);

                findViewById(R.id.ExitButton).setOnClickListener(this);
                findViewById(R.id.LaunchButton1).setOnClickListener(this);
                findViewById(R.id.LaunchButton2).setOnClickListener(this);
                findViewById(R.id.LaunchButton3).setOnClickListener(this);
                findViewById(R.id.LaunchButton4).setOnClickListener(this);

                if (D)
                        Log.e(TAG, "+++ ON CREATE +++");

                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
                        Toast.makeText(this,
                                "Bluetooth is not available.",
                                Toast.LENGTH_LONG).show();
                        finish();
                        return;
                }

                if (!mBluetoothAdapter.isEnabled()) {
                        Toast.makeText(this,
                                "Please enable your BT and re-run this program.",
                                Toast.LENGTH_LONG).show();
                        finish();
                        return;
                }

                if (D)
                        Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");

                Bundle extras = getIntent().getExtras();
                if(extras !=null) {
                    if (extras.getString(ADDRESS) != null){;
                    address = extras.getString(ADDRESS);
                    }
                }
        }

        @Override
        public void onStart() {
                super.onStart();
                if (D)
                        Log.e(TAG, "++ ON START ++");
        }

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

                if (D) {
                        Log.e(TAG, "+ ON RESUME +");                                             
                        Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
                }

                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

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

                try {
                        btSocket.connect();
                        Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
                } catch (IOException e) {
                        try {
                                btSocket.close();
                        } catch (IOException e2) {
                                Log.e(TAG,
                                        "ON RESUME: Unable to close socket during connection failure", e2);
                        }
                }                               
        }

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

                if (D)
                        Log.e(TAG, "- ON PAUSE -");

                if (outStream != null) {
                        try {
                                outStream.flush();
                        } catch (IOException e) {
                                Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
                        }
                }

                try     {
                        btSocket.close();
                } catch (IOException e2) {
                        Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
                }
        }

        @Override
        public void onStop() {
                super.onStop();
                if (D)
                        Log.e(TAG, "-- ON STOP --");
        }

        @Override
        public void onDestroy() {
                super.onDestroy();
                if (D)
                        Log.e(TAG, "--- ON DESTROY ---");
                try {
                    btSocket.close();
                } catch (IOException e1) {
                    Log.e(TAG, "ON RESUME: Unable to close socket during connection failure");
                }
        }

        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
            case R.id.ExitButton:

                try {
                    btSocket.close();
                } catch (IOException e1) {
                    Log.e(TAG, "ON RESUME: Unable to close socket during connection failure");
                }
                this.finish();
                break;


            case R.id.LaunchButton1:             

                try {
                    outStream = btSocket.getOutputStream();
                    } 
                catch (IOException e) {
                    Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
                    }

                try {
                        outStream.write(msgBuffer[0]);
                } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Exception during write.", e);
                }   
                outStream = null;           
                break;


            case R.id.LaunchButton2:
                try {
                    outStream = btSocket.getOutputStream();
                    } 
                catch (IOException e) {
                    Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
                    }

                try {
                        outStream.write(msgBuffer[1]);
                } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Exception during write.", e);
                }   
                outStream = null;   
                break;          


            case R.id.LaunchButton3:
                try {
                    outStream = btSocket.getOutputStream();
                    } 
                catch (IOException e) {
                    Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
                    }

                try {
                        outStream.write(msgBuffer[2]);
                } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Exception during write.", e);
                }   
                outStream = null;   
                break;



            case R.id.LaunchButton4:
                try {
                    outStream = btSocket.getOutputStream();
                    } 
                catch (IOException e) {
                    Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
                    }

                try {
                        outStream.write(msgBuffer[3]);
                } catch (IOException e) {
                        Log.e(TAG, "ON RESUME: Exception during write.", e);
                }   
                outStream = null;   
                break;
            }

        }
}

Upvotes: 1

Views: 3819

Answers (1)

Femi
Femi

Reputation: 64690

The Tab and (in my experience with 6 different devices) all Samsung devices have TERRIBLE Bluetooth SPP implementations. I'd suggest choosing a different device.

Also, I would check that you are calling flush on the OutputStream, particularly if you are not sending line separators.

Upvotes: 2

Related Questions