Problems with connecting bluetooth SPP in android?


Answers

Sending a string via Bluetooth from a PC as client to a mobile as server

You will need to use the RFComm APIS to make the communication work I have managed to define a class which is a Thread and will be acting as a server and listening for client connections. I have also placed some comments for you to understand.

    private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
    }

    public void run() {         
        BluetoothSocket socket = null;

                    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

        // Listen to the server socket if we're not connected
        while (true) {

            try {
                // Create a new listening server socket
                Log.d(TAG, ".....Initializing RFCOMM SERVER....");

                // MY_UUID is the UUID you want to use for communication
                mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);                    
                //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);  you can also try using In Secure connection...

                // This is a blocking call and will only return on a
                // successful connection or an exception                    
                socket = mmServerSocket.accept();                   

            } catch (Exception e) {

            }

            try {
                Log.d(TAG, "Closing Server Socket.....";                    
                mmServerSocket.close();



                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the BluetoothSocket input and output streams

                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();


                mmInStream = new DataInputStream(tmpIn);
                mmOutStream = new DataOutputStream(tmpOut); 

                // here you can use the Input Stream to take the string from the client whoever is connecting
                //similarly use the output stream to send the data to the client
            } catch (Exception e) {
                //catch your exception here
            }

        }
    }

}

I hope this helps

For your another question:

Declaring javax.bluetooth.UUID on Client side (PC) UUID class should be from javax.bluetooth.UUID

   uuidSet2[0] = new UUID("446118f08b1e11e29e960800200c9a66", false);

Declaring java.util.UUID at Server Side (Android)

    UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
Question
public class TestConnection extends Activity {
/** Called when the activity is first created. */
public static final UUID BluetoothSerialUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter _adapter;
BluetoothSocket socket=null;

@Override 
protected void onPause() {
    try {
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView text = (TextView) findViewById(R.id.mainText);
    text.setText("This is a test");

    String deviceAddress = "05:03:e8:c0:bf:c0";
    _adapter = BluetoothAdapter.getDefaultAdapter();

    BluetoothDevice device = _adapter.getRemoteDevice(deviceAddress);

    try {
        socket = device.createRfcommSocketToServiceRecord(BluetoothSerialUuid);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        socket.connect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

I am trying to use some code from this site:

http://zornsoftware.talsit.info/blog/pairing-spp-bluetooth-devices-with-android-phones.html

to overcome the fact android skips class 0x00 Bluetooth devices. However when I do this: _adapter.getRemoteDevice(deviceAddress);

I never get a response




Links



Tags