【问题标题】:Paring bluetooth with Android using createInsecureRfcommWithServiceRecord使用 createInsecureRfcommWithServiceRecord 将蓝牙与 Android 配对
【发布时间】:2013-12-24 23:18:06
【问题描述】:

基本上,我正在尝试将我的 Galaxy Note (4.1.2) 与连接到 arduino (https://www.sparkfun.com/products/582) 的 bluesmirf 配对

我从 Instructables http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/ 撕掉了这个项目,并找到了一些友好的 cmets,它们建议替换一小部分代码,这样每次我的 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 是未定义的

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

如果我使用 createInsecureRfcommSocketToServiceRecord 替换 listenUsingInsecureRfcommWithServiceRecord 也是如此

就个人而言,我认为这是在谷歌上搜索后的 API 版本问题,但我该如何做到这一点?这里完全是菜鸟,因此非常感谢您提供任何帮助。

提前致谢。

【问题讨论】:

    标签: android bluetooth android-bluetooth


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 2014-02-18
      • 2019-02-06
      • 2013-05-31
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多