android - 在 Android 上自动连接到已配对的蓝牙设备

标签 android bluetooth

我想将我的 Android 手机(2.3.6 Samsung Note)连接到嵌入式蓝牙设备(Arduino 上的 RN-42 BT UART 模块)。从 BluetoothChat 示例开始,并将嵌入式设备与手机配对。到目前为止一切似乎都工作正常,设备已连接并且数据从 Android 传递到嵌入式设备。我仍然缺少的是让设备在两者处于范围内后自动连接。

使用调试器,我看到嵌入式设备是“主机”或术语“从属”,Android 是客户端,因为 Android 发出连接请求。

最佳答案

我有类似的配置(Android Galaxy S3 手机 4.0 和 RN-42 BT 连接到 Arduino Uno) 我能够将 Android 和蓝牙配对并从 Android 连接到 RN-42 BT (我正在使用 BlueTerm 应用程序进行测试) 但是,我无法从 RN-42 BT 连接到 Android 手机。 我遵循了以下说明和代码示例:http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/

我将 42 BT 编程为客户端并将其设置为自动连接模式 (SR,3)。 在我的 Android 代码中,BluetoothSerialService(相当于 PhoneInfoServer 示例代码)卡在 AcceptThread on: socket = mmServerSocket.accept(); 我附上以下与连接问题相关的代码:

  1. 将连接模式设置为自动并启动的 Arduino 代码 与 Android 手机的连接
  2. Android BluetoothSerialService AcceptThread 监听传入的代码 连接
  3. 显示代码卡在等待中的 logcat 消息 传入连接

在 Google 的 BluetoothChat 演示应用程序中,有一个选项可以使手机可被发现,以便另一部手机可以连接到它。 我正在为蓝牙串行连接寻找类似的东西。我在 Google Play 上寻找一个应用程序来测试监听来自蓝牙串行设备的传入连接请求,但没有找到这样的应用程序。 有人知道这样的应用程序吗?

问候, 艾夫纳

  1. 将连接模式设置为自动并启动与 Android 手机的连接的 Arduino 代码

    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 监听传入连接的代码

    // ...
        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);
    //...
    
  3. 显示代码在等待传入连接时卡住的 logcat 消息

        // ...
    12-09 01:04:38.765: I/BluetoothSerialServiceAcceptThread(16175): BEG AcceptThread::AcceptThread
    12-09 01:04:38.765: I/BluetoothSerialServiceAcceptThread(16175): AcceptThread constructor trying to create listening socket
    12-09 01:04:38.765: V/BluetoothSocket.cpp(16175): initSocketNative
    12-09 01:04:38.765: V/BluetoothSocket.cpp(16175): ...fd 49 created (RFCOMM, lm = 0)
    12-09 01:04:38.765: V/BluetoothSocket.cpp(16175): initSocketFromFdNative
    12-09 01:04:38.775: D/BluetoothUtils(16175): isSocketAllowedBySecurityPolicy start : device null
    12-09 01:04:38.775: V/BluetoothSocket.cpp(16175): bindListenNative
    12-09 01:04:38.775: V/BluetoothSocket.cpp(16175): ...bindListenNative(49) success
    12-09 01:04:38.785: D/BluetoothSerialServiceAcceptThread(16175): AcceptThread: Listening BT Socket Insecure created
    12-09 01:04:38.785: D/BluetoothSerialServiceAcceptThread(16175): mmServerSocket: android.bluetooth.BluetoothServerSocket@41af72c8
    12-09 01:04:38.785: D/BluetoothReadService(16175): END start
    12-09 01:04:38.795: I/BluetoothSerialServiceAcceptThread(16175): BEG BluetoothSerialService::run
    12-09 01:04:38.795: D/BluetoothSerialServiceAcceptThread(16175): AcceptThread.run: socket type:Insecure
    12-09 01:04:38.795: I/BluetoothSerialServiceAcceptThread(16175): mState: 1
    12-09 01:04:38.795: I/BluetoothSerialServiceAcceptThread(16175): socket before mmServerSocket.accept(): null
    12-09 01:04:38.795: V/BluetoothSocket.cpp(16175): acceptNative
    12-09 01:04:38.855: I/MainActivity(16175): mBtStatus: android.widget.ImageView@41adc698
    12-09 01:04:38.855: I/MainActivity(16175): In case: BluetoothSerialService.STATE_LISTEN
    12-09 01:04:38.855: D/MainActivity(16175): Beg onCreateOptionsMenu
    12-09 01:04:38.885: D/memalloc(16175): ion: Mapped buffer base:0x5d760000 size:3768320 offset:0 fd:57
    12-09 01:04:38.925: D/CLIPBOARD(16175): Hide Clipboard dialog at Starting input: finished by someone else... !
    // ...
    

我进一步发现 RN-42 BT 正在进入自动连接模式,但尝试连接到家里的另一部非安卓 LG 手机。

我通过将 RN-42 BT 重置为出厂默认设置发现了这一点。 使用 BlueTerm 应用程序,我成功地从 Android 手机连接到 RN-42 BT。 当我执行查询扫描($$$I\r)时,我得到了 LG 手机的 mac 地址和名称。 此手机的蓝牙具有不同的 mac 地址 (0026e25d8a91) - 我不知道是什么导致 RN-42 BT 尝试连接此设备。

这意味着自动连接模式确实有效,但连接被定向到错误的手机。 我很困惑,因为我使用以下命令指定 Android 手机的 mac 地址(它们之间有延迟)

// The mac address of the android phone
$$$SR,04FE3144A0A4\r

// Force a complete reboot of the device (similar to a power cycle).
$$$R,1\r

// SM,3 - mode=auto
// SO,Z - Extended Status String, Setting this string enables status messages to be sent to the local serial port.
// --- - exit command mode (three minus signs).
$$$SM,3\rSO,Z\r---\r

我现在认为从 RN-42 BT 发起的连接是可以的,但是 android 代码上的 BluetoothServerSocket 设置不正确。
我已尝试将 BluetoothServerSocket 设置为使用 listenUsingInsecureRfcommWithServiceRecord 和 listenUsingRfcommWithServiceRecord 进行监听。 我注意到有一个命令 createInsecureRfcommSocketToServiceRecord。我应该改用它吗?

如有任何建议,我们将不胜感激。

谢谢, 艾夫纳

关于android - 在 Android 上自动连接到已配对的蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732252/

相关文章:

iphone - 如何在iPhone中使用邮件访问配置文件(MAP)

android - 蓝牙 RSSI 值在所有 Android 设备中始终以 dBm 为单位?

android - 如果未连接到设备,蓝牙将关闭

android - 两个 editText : android 中的时间比较

Android 精确闹钟总是关闭 3 分钟

Android 不支持的操作异常 : Can't convert to color: type=0x2

java - 连接蓝牙设备

java - 将第三个 Android Activity 的结果返回给第一个 Activity

java - EventBus的onEvent方法如何使用?

ios - 外设写入值: forCharacteristic: type: return null error and value