簡體   English   中英

自動連接到Android上的配對藍牙設備

[英]Auto connecting to Paired Bluetooth devices on Android

我想將我的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上:socket = mmServerSocket.accept(); 我附加了以下與連接問題相關的代碼:

  1. Arduino代碼,用於將連接模式設置為auto並啟動與Android手機的連接
  2. Android BluetoothSerialService AcceptThread代碼,用於偵聽傳入連接
  3. logcat消息,顯示代碼卡在等待傳入連接

在Google的BluetoothChat演示應用程序中,有一個選項可以讓手機被發現,以便另一部手機可以連接到它。 我正在尋找類似的藍牙串行連接。 我在Google Play上搜索了一個應用程序,該應用程序將測試從藍牙串行設備收聽傳入的連接請求,但沒有找到這樣的應用程序。 有人知道這樣的應用程序嗎?

此致,Avner

  1. Arduino代碼,用於將連接模式設置為auto並啟動與Android手機的連接

     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正在進入自動連接模式,但試圖連接到家里的另一台非Android手機。

我通過將RN-42 BT重置為出廠默認值來發現這一點。 使用BlueTerm應用程序,我成功地從Android手機連接到RN-42 BT。 當我執行查詢掃描($$$ I \\ r)時,我得到了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。 我應該使用它嗎?

任何建議將不勝感激。

謝謝,Avner

所以我想出了“技巧”。 首先,移動電話設置為僅接受連接和具有線路電源以自動連接的嵌入式設備。 更改是使用自動連接將嵌入式設備從“主機”模式設置為“客戶端”模式。

下一個障礙是Android必須在服務中使用Accept代碼,否則該連接僅在應用程序處於活動狀態時才有效。 通過將Accept代碼片段放入服務中,應用程序可以是非活動的,並且嵌入式附件將自動連接。 代碼將在Instructable中共享: 在此處輸入鏈接描述

完整的代碼和說明: http//www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/

它不僅包括代碼,還包括有關其工作原理和裝配描述的說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM