Johna
Johna

Reputation: 1

Paring bluetooth with Android using createInsecureRfcommWithServiceRecord

Basically, i'm trying to pair my galaxy note (4.1.2) with a bluesmirf that is connected to arduino (https://www.sparkfun.com/products/582)

i ripped this project off Instructables http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/ and found some friendly comments that suggest replacing a small part of the code so that there would be no need to key in password every single time my android tries to establish a connection with bluetooth module (Auto connecting to Paired Bluetooth devices on Android).

I'm using the same code as Avner, so i just borrowed his/her code as a reference.

//////////////////////////////////////////////////////////////////////////
 1. Arduino code that sets the connection mode to auto and initiates a
    connection with the Android phone
//////////////////////////////////////////////////////////////////////////
void setup()
{


  Serial.begin(115200);

  Serial.println("BEG setup");

  static const char *initString0 = "$$$SR,04FE3144A0A4\r";

  // R,1 Forces a complete reboot of the device (similar to a power cycle).
  static const char initString1a[] = "$$$";
  static const char initString1b[] = "R,1\r";

  // auto
  static const char initString2a[] = "$$$";
  static const char initString2b[] = "SM,3\rSO,Z\r---\r";
  static const char *initVector[] = { initString0, initString1a, initString1b, initString2a, initString2b, NULL };

  int i;

  for (i=0; initVector[i] != NULL; i++)
  {
      Serial.print(initVector[i]);
      delay(500);
  }

  Serial.println("Setup completed");

}

//////////////////////////////////////////////////////////////////////////
 2. Android BluetoothSerialService AcceptThread code that listens to
    incoming connection
//////////////////////////////////////////////////////////////////////////

...
    private class AcceptThread extends Thread
    {
        // The local server socket
    static private final String TAG = "BluetoothSerialServiceAcceptThread";
        private final BluetoothServerSocket mmServerSocket;
        private String mSocketType;


        /** Creates an thread for accepting incoming Bluetooth connections
         * @param secure    Currently ignored, but suppose to represent the mode of socket.
         * All communication is currently done over insecure socket 
         */
        public AcceptThread(boolean secure)
        {
            Log.i(TAG, "BEG AcceptThread::AcceptThread");

            BluetoothServerSocket tmp = null;
            mSocketType = secure ? "Secure":"Insecure";

            // Create a new listening server socket
            try
            {
            Log.i(TAG, "AcceptThread constructor trying to create listening socket");

                if (!secure)
                {
                    // This is for Android 2.2
                    // tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_INSECURE, BT_SPP_UUID);

                    // This is for Android 2.3 but testing the above on 2.3 device showed it to be working.
                    tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, BT_SPP_UUID);
                }

                Log.d(TAG, "AcceptThread: Listening BT Socket " + mSocketType + " created");
            }
            catch (IOException e)
            {
                Log.e(TAG, "AcceptThread: Listening BT Socket Type: " + mSocketType + " listen() failed " + e.getMessage());
                acceptProblem();
            }
            mmServerSocket = tmp;

            Log.d(TAG, "mmServerSocket: " + mmServerSocket);

        } // public AcceptThread


        public void run()
        {

            Log.i(TAG, "BEG BluetoothSerialService::run");

            if (mmServerSocket == null)
            {
            Log.e(TAG, "AcceptThread.run: No server socket");
            return;
            }

            Log.d(TAG, "AcceptThread.run: socket type:" + mSocketType);
            setName("AcceptThread" + mSocketType);

            BluetoothSocket socket = null;

            Log.i(TAG, "mState: " + mState);

            // Listen to the server socket if we're not connected
            while (mState != STATE_CONNECTED)
            {
                Log.i(TAG, "socket before mmServerSocket.accept(): " + socket);

                try
                {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    socket = mmServerSocket.accept();
                    Log.d(TAG, "AcceptThread.run: returned from accept");
                }
                catch (IOException e)
                {
                    Log.e(TAG, "AcceptThread.run: Socket Type: " + mSocketType + "accept() failed " + e.getMessage());
                    break;
                }

                Log.i(TAG, "socket after mmServerSocket.accept(): " + socket);
...

and this is the the current feedback from Eclipse i have, elipse claims that listenUsingInsecure or createInsecureRf is undefined

The method listenUsingInsecureRfcommWithServiceRecord(String, UUID) is undefined for the type BluetoothAdapter PhoneInfoServer.java /myPhoneInfoWithService/src/myPhoneInfo/test/zakiem line 539 Java Problem

same goes if i use createInsecureRfcommSocketToServiceRecord to replace listenUsingInsecureRfcommWithServiceRecord

personally, i think its the API version issue after some googling, but how do i make this work? A total noob here, so any help on this would be much appreciated.

Thanks in advance.

Upvotes: 0

Views: 811

Answers (1)

crazyPixel
crazyPixel

Reputation: 2330

In case you didn't solve the problem yet add the following code line to your class variables -

private final static String NAME_INSECURE = "My application service name";
private final static String MYUUID = "52286F1D-89EF-498E-AFD1-B1CA6E8AAFD8";
private final UUID BT_SPP_UUID = UUID.fromString(MYUUID);

Upvotes: 1

Related Questions