Android bluetooth auto connection

Bluetooth auto connect on Android-devices

Probably, it is not necessary to explain what Bluetooth is and how to use it. We all use it in different purposes: Bluetooth headset, file transfer, Internet distribution, etc. But many people are questioning whether it is possible to configure automatic Bluetooth connection on Android-devices. The answer is simple – you can do it. Wondering how? Read the article.

Setting the auto connect

Note that these functions are not initially provided. Smartphones, tablets and laptops do not have built-in tools that allow you to automatically connect to Bluetooth. So you will have to search for the desired program, install it on the smartphone (or tablet), configure it, and only then the connection will happen automatically.

In addition, before the program can perform its direct functions, you will have to pair the device with the desired devices manually. Later in this article we will discuss how to pair the devices, which application to install, where to find it and how to configure it.

Pairing between Bluetooth devices

You have to know a few rules and perform a clear sequence of actions. To make pairing between devices, you must enable the Bluetooth on both devices and activate the detection function.

Detection on the computer is activated as follows: in the notification area there is the Bluetooth icon. Right-click it. In the menu, it is necessary to select «View Options».

In the tab «Options» there is a line «Allow devices to find this computer». Put a tick here and save the settings.

On Android, you will need to open the Bluetooth settings, enable the adapter (move the slider to the «On») and put a checkmark in the “Visible to all Bluetooth devices”.

As a rule, the tick is removed automatically after 2-3 minutes. To avoid this, you must press the «Menu» (on the Samsung it is the touch button to the left of the mechanical «Home» key). Select the «Connection timeout» and choose the time after which the detection will be switched off.

Now, when the detection is enabled, we can perform the pairing. If you want to connect your smartphone to your computer, then click the Bluetooth icon in the system tray again on your PC. Select «Add a device».

You will see a window, in which the system will display all Bluetooth devices available to connect. Select your device from the list and click «Next».

There will be another window, in which you will see the PIN code. If it is the same as the code that appeared on your phone (tablet), then press «OK» on both devices. That is all, the pairing is performed.

Читайте также:  Tsc alpha 3rb bluetooth

If we are talking about the connection between two mobile gadgets, then go to the Bluetooth settings, turn on the adapter and press the «Search» button below.

The system will find all devices available for pairing. You will just need to select the desired device. On the second device, you will be asked to connect. If the PIN code is the same on both gadgets, confirm the request. The devices are paired.

Setting the auto connection

As mentioned above, the automatic connection of Bluetooth on Android is available only through third-party applications. The most common application is called “Bluetooth Auto Connect”. You can download the app from the Google Play for free.

It is quite simple to work with. After starting the program you will see the settings. There will be a few items that need to be set up:

  • Set Last Device. If you put a tick here, after Bluetooth enabling the smartphone will automatically connect to latest gadget (if it is in range).
  • Profiles. Here you can select the types of connections. For a quick connection to a headset, you should tick the lines «Media Audio (A2DP)» and «Call audio (HSP)». Here you will need to choose the right technologies by checking the appropriate lines.

Do not configure the remaining sections. That’s all. Now, when you turn on Bluetooth on your phone it will automatically connect to a paired devices that are in range of the connection. As you can see, everything is very simple and easy.

Bluetooth auto connect on Android devices: Video

[rBlock name=after_video return=1]

Источник

Auto connecting to Paired Bluetooth devices on Android

I want to connect my Android Phone (2.3.6 Samsung Note) to an embedded Bluetooth device (RN-42 BT UART module on Arduino). Started with BluetoothChat example and have the embedded device paired with the phone. So far all seems to work fine, the devices get connected and data is passed from Android to embedded device. What I am still missing is to make the devices connect automatically once the two are in range. Using the debugger I see the embedded device is «Host» or in it’s term «Slave» and the Android being a client, as the Android issuing a connect request.

3 Answers 3

I have a similar configuration (Android Galaxy S3 phone 4.0 and RN-42 BT connected to Arduino Uno) I am able to pair the Android and the bluetooth and to connect from the Android to the RN-42 BT (I am using the BlueTerm app to test that) However, I’m unable to connect from the RN-42 BT to the Android phone. I followed the innstructions and the code example in: http://www.instructables.com/id/Missed-calls-and-SMS-Notifier-Accessory/

Читайте также:  Panasonic sa pm250 блютуз

I programmed the 42 BT to serve as a client and set it to Auto connect mode (SR,3). In my Android code, the BluetoothSerialService (equivalent to the PhoneInfoServer example code) is stuck in the AcceptThread on: socket = mmServerSocket.accept(); I’m attaching the following pieces of codes that are related to the connection problem:

  1. Arduino code that sets the connection mode to auto and initiates a connection with the Android phone
  2. Android BluetoothSerialService AcceptThread code that listens to incoming connection
  3. logcat messages that show that the code is stuck waiting for incoming connection

In Google’s BluetoothChat demo app there is an option to make the phone discoverable so that another phone can connect to it. I am looking for something similar for bluetooth serial connection. I looked for an app on Google Play that will test listenning to an incoming connection request from a bluetooth serial device, but didn’t find such an app. Does someone know of such an app?

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

I further discovered that the RN-42 BT is getting into auto-connect mode but tries to connect to another non-android LG phone at the house.

Читайте также:  Лучшие полноразмерные наушники блютуз до 10000

I discovered this by reseting the RN-42 BT to the factory defaults. Using the BlueTerm app I successfully connect from the Android phone to the RN-42 BT. When I perform an inquiry scan ($$$I\r) I get the mac address and the name of the LG phone. This phone has a bluetooth with a different mac address (0026e25d8a91) — I don’t know what causes the RN-42 BT to try and connect with this device.

This means that the auto connect mode does work but the connection is directed to the wrong phone. I’m pazzled because I’m specifying the mac address of the Android phone using the following commands (with delays between them)

// 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 

I am now thinking that the connection initiation from the RN-42 BT is Ok but that the BluetoothServerSocket on the android code is not set up properly.
I have tried setting the BluetoothServerSocket to listen using listenUsingInsecureRfcommWithServiceRecord and listenUsingRfcommWithServiceRecord. I notice that there is a command createInsecureRfcommSocketToServiceRecord. Should I use it instead?

Any advice would be greatly appreciated.

Источник

Оцените статью
Adblock
detector