Mam podobną konfigurację (Android Galaxy S3 telefonu 4,0 i RN-42 BT podłączony do Arduino Uno) jestem w stanie powiązać Android oraz bluetooth i połączyć z Androida do RN-42 BT (Używam aplikacji BlueTerm do testowania). Nie mogę jednak połączyć się z RN-42 BT z telefonem z Androidem. Śledziłem innstrukcje i przykład kodu w: http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/
Zaprogramowałem 42 BT, aby służyć jako klient i ustawić go na tryb automatycznego łączenia (SR, 3). W moim kodzie Android, BluetoothSerialService (odpowiednik przykładowego kodu PhoneInfoServer) utknął w AcceptThread na: socket = mmServerSocket.accept(); Ja załączając następujące fragmenty kodu, które są związane z problemem połączenia:
- kod Arduino, który ustawia tryb połączenia na auto i inicjuje połączenie z telefonu Android
kodu
- Android BluetoothSerialService AcceptThread że nasłuchuje przychodzących pOŁĄCZENIE
- wiadomości logcat, które pokazują, że kod jest zatrzymany czeka na połączenia przychodzącego
W aplikacji demonstracyjnej BluetoothChat dostępna jest opcja, aby telefon był wykrywalny, aby inny telefon mógł się z nim połączyć. Szukam czegoś podobnego do połączenia szeregowego bluetooth. Szukałem aplikacji w Google Play, która przetestuje słuchanie przychodzącej prośby o połączenie z urządzenia szeregowego Bluetooth, ale nie znalazła takiej aplikacji. Czy ktoś wie o takiej aplikacji?
Pozdrawiam, Avner
kod Arduino, który ustawia tryb połączenia na auto i inicjuje połączenie z telefonu Android kodu
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");
}
Android BluetoothSerialService AcceptThread
który nasłuchuje połączeń przychodzących
// ...
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);
//...
wiadomości logcat, które pokazują, że kod jest zatrzymany czeka na przychodzące połączenia
// ...
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: [email protected]
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: [email protected]
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... !
// ...
I dalej odkrył, że RN-42 BT jest miejsce trybie auto-connect ale próbuje połączyć się z innym niż Android LG telefon w domu.
Odkryłem to poprzez ponowne ustawienie RN-42 BT na fabryczne ustawienia domyślne. Używanie aplikacji BlueTerm Pomyślnie łączę się z telefonu z Androidem do RN-42 BT. Kiedy wykonuję skanowanie z zapytaniem ($$$ I \ r), otrzymuję adres MAC i nazwę telefonu LG. Ten telefon ma Bluetooth z innym adresem mac (0026e25d8a91) - Nie wiem, co powoduje, że RN-42 BT próbuje połączyć się z tym urządzeniem.
Oznacza to, że tryb automatycznego połączenia działa, ale połączenie jest przekierowywane do niewłaściwego telefonu. mam pazzled bo jestem podając adres MAC telefonu Android za pomocą następujących poleceń (z opóźnień między nimi)
// 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
Jestem teraz myśli, że inicjacja połączenia z RN-42 BT jest Ok ale to, że BluetoothServerSocket na kodzie androida nie jest poprawnie skonfigurowany.
Próbowałem ustawić BluetoothServerSocket do nasłuchu przy użyciu listenUsingInsecureRfcommWithServiceRecord i listenUsingRfcommWithServiceRecord. Zauważam, że istnieje polecenie createInsecureRfcommSocketToServiceRecord. Czy powinienem go użyć zamiast tego?
Każda rada byłaby mile widziana.
Dzięki Avner
Nie zapomnij można zaakceptować swoją odpowiedź. –
Na przykład, czy wiesz, jak zrobić, aby połączyć się z moim najbliższym urządzeniem Bluetooth, aktywując tylko Bluetooth ręcznie? –