使用createInsecureRfcommWithServiceRecord与Android配对蓝牙

时间:2013-06-12 20:26:43

标签: android bluetooth android-bluetooth

基本上,我试图将我的星系音符(4.1.2)与连接到arduino(https://www.sparkfun.com/products/582)的bluesmirf配对

我从Instructables http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/中删除了这个项目并找到了一些友好的评论,建议更换一小部分代码,以便每次我的android尝试与之建立连接时都不需要输入密码蓝牙模块(Auto connecting to Paired Bluetooth devices on Android)。

我使用与Avner相同的代码,所以我只是借用他/她的代码作为参考。

//////////////////////////////////////////////////////////////////////////
 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);
...

这是Eclipse的当前反馈,elipse声称listenUsingInsecure或createInsecureRf未定义

方法listenUsingInsecureRfcommWithServiceRecord(String,UUID)未定义类型BluetoothAdapter PhoneInfoServer.java / myPhoneInfoWithService / src / myPhoneInfo / test / zakiem line 539 Java问题

如果我使用createInsecureRfcommSocketToServiceRecord来替换listenUsingInsecureRfcommWithServiceRecord

个人而言,我认为在谷歌搜索后它的API版本问题,但我如何使这项工作?这里有一个总的菜鸟,所以对此的任何帮助都会非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您没有解决问题,请将以下代码行添加到您的类变量中 -

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);