Step 2: How to assemble?

Below are the steps for connecting the hardware pieces
1. Connect the Bluetooth module to Arduino serial port:
Module Pin       Arduino Pin
Vcc                  3,3V
GND                GND
Tx-o                 Rx (if Mega, connect to 18, if other Arduino, connect to Digital 0)
Rx-I                  Tx (if Mega, connect to 17, if other Arduino, connect to Digital 1)

2. Connect the display module to Arduino (you can reassign pins in the code if you like)
Module Pin      Arduin Pin
GND                GND
+5V                 5V
DIN                  Digital 4
CS                   Digital 5
CLK                 Digital 6

<p>Hey Zakie,</p><p>I am making a data logging device where accessory comes ON every 1 hour, transfers logged data to android mobile and goes back to OFF. I am using Arduino Uno connected to Bluetooth Mate Gold which is acting as a client and Samsung Galaxy Note 3 running 4.4.2 as host (AcceptThread). Accessory is able to connect to mobile for the first time but does not connect when it comes ON for the second time. Android code is not a service but an activity. I am currently keeping the application always ON. Can you guess what is going wrong here? It will be of great help. Thanks !</p>
<p>First ensure the BT module on the Arduino is properly configured. Depending on the module itself you will need to send it some AT commands to program it to auto initiate the conversation with the Android unit. I found this part pretty tricky to be honest, until I figured it out. On the RN-42 I think it was SM,3.</p><p>Make sure the Android BT MAC is registered with the Arduino BT module too. That saves on the search time as I noticed.</p>
hey, first of all, thanks for the wonderful project <br> <br>currently im facing a problem. when i tried to replace the listenUsingInsecure with createInsecure, there is an error that says createInsecure is undefined. In my opinion, that has got something to do with the API version issue since createInsecure was only introduced later on. <br> <br>is there anyone who could enlighten me in how to overcome this problem? total noob here, so any help would be much appreciated. <br> <br>thanks
I just built a new one of this, for my wife. It turns out Rover Networks changed the Firmware of the BT module, and for it to work better with Android you need to do <br>SI,0200 <br>SJ,0200 <br>It took me few days to figure that out. Now it works perfect! <br>The changes I made includes 3 units of 8x8 LED Matrix (7219 based) and few minor changes to Android code. All code available in github
hey there also working with Galaxy S3 phone which has ICS 4.0.3. <br>the original code from zmashia works when replacing with createInsecureRfcommSocketToServiceRecord ... <br> <br>however this connection only works ONCE?! The BT module connects automatically just fine....then lets say i walk out of range and walk back in the connection will not be restablished :( <br> <br>any solution to this? do you know where it hangs? or waits around?
Hi Zakie, The related code in the MainActivity is:<br> <br> <em>// Indicator if the BluetoothSerialService is used a a server or as a client to establish the connection<br> public static final boolean useBluetoothSerialServiceAsServerForConnection = false;</em><br> <br> If this indicator is set to true, the BluetoothSerialService (parallel to PhoneInfoServer in the example code) gets into a listening state and wait for incoming connections from the RN-42 BT This did not work for my Galaxy S3 phone which has ICS 4.0.3<br> <br> If this indicator is set to false, the BluetoothSerialService gets into a connect state and tries to connect with the RN-42 BT The code is based on the app BlueTerm code which can be downloaded from http://pymasde.es/blueterm/ The related code in the BluetoothSerialService is:<br> <br> private ConnectThread mConnectThread;<br> <br> <em>...<br> <br> /** * Start the service. Depending on useBluetoothSerialServiceAsServerForConnection, either: * a) Start AcceptThread to begin listening for incoming connections (server mode), or * b) Start ConnectThread to initiate a connection (client mode) * Called by the Activity onResume() */<br> <br> public synchronized void start() { Log.i(TAG, &quot;BEG start&quot;); Log.i(TAG, &quot;mState: &quot; + mState); // If we do not do that, accessory will disconnect foo5and connect again on application resume if (mState == STATE_CONNECTED) { return; } Log.i(TAG, &quot;start() cleaning threads&quot;); // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } Log.i(TAG, &quot;start() creating AcceptThread&quot;); if( MainActivity.useBluetoothSerialServiceAsServerForConnection == true ) { ////////////////////////////////////////////////////////////////////////// // Use the following connection configuration: // Android: server // RN-42 BT: client ////////////////////////////////////////////////////////////////////////// // Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } setState(STATE_LISTEN); // Initiate the thread attempting to accept a connection if (mInsecureAcceptThread == null) { mInsecureAcceptThread = new AcceptThread(false); mInsecureAcceptThread.start(); } } // if( MainActivity.useBluetoothSerialServiceAsServerForConnection == true ) else { ////////////////////////////////////////////////////////////////////////// // Use the following connection configuration: // Android: client // RN-42 BT: server ////////////////////////////////////////////////////////////////////////// // Cancel any thread attempting to accept a connection if (mInsecureAcceptThread != null) { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } // Initiate the thread attempting to make a connection if (mConnectThread == null) { mConnectThread = new ConnectThread(MainActivity.mRemoteDevice, false); mConnectThread.start(); setState(STATE_CONNECTING); } } // else if (D) Log.d(TAG, &quot;END start&quot;); }<br> <br> /** * Start the ConnectedThread to begin managing a Bluetooth connection * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected * @param socketType The socket type: secured / insecured */<br> public synchronized void connected( BluetoothSocket socket, BluetoothDevice device, String socketType ) { if (D) Log.d(TAG, &quot;BEG connected&quot;); Log.d(TAG, &quot;PhoneInfo.connected(): socket type:&quot; + socketType + &quot;Canceling other threads&quot;); // Cancel any thread currently attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one device if (mInsecureAcceptThread != null) { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } // Start the thread to manage the connection and perform transmissions Log.d(TAG, &quot;PhoneInfo.connected(): Creating connected thread&quot;); mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Log.d(TAG, &quot;PhoneInfo.connected(): Sending device name to calling activity&quot;); sendDeviceNameToActivity(device.getName()); setState(STATE_CONNECTED); } // public synchronized void connected<br> <br> // Stop all threads public synchronized<br> void stop() { Log.d(TAG, &quot;PhoneInfo.stop(): stop all threads&quot;);// Cancel any thread attempting to make a connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel any thread accepting a connection if (mInsecureAcceptThread != null) { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } setState(STATE_NONE); } // public synchronized void stop()<br> <br> /** * This thread runs while attempting to make an outgoing connection * with a device. It runs straight through; the connection either * succeeds or fails. */<br> private class ConnectThread extends Thread<br> {<br> private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; private String mSocketType;<br> <br> public ConnectThread(BluetoothDevice device, boolean secure) { mmDevice = device; BluetoothSocket tmp = null; mSocketType = secure ? &quot;Secure&quot;:&quot;Insecure&quot;; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { // tmp = device.createRfcommSocketToServiceRecord(BT_SPP_UUID); tmp = device.createInsecureRfcommSocketToServiceRecord(BT_SPP_UUID); } catch (IOException e) { Log.e(TAG, &quot;create() failed&quot;, e); } mmSocket = tmp; } // public ConnectThread<br> <br> public void run() { Log.i(TAG, &quot;BEGIN ConnectThread::mConnectThread&quot;); setName(&quot;ConnectThread&quot;); // Always cancel discovery because it will slow down a connection mAdapter.cancelDiscovery(); // Make a connection to the BluetoothSocket try { // This is a blocking call and will only return on a // successful connection or an exception mmSocket.connect(); } catch (IOException e) { connectionFailed(); // Close the socket try { mmSocket.close(); } catch (IOException e2) { Log.e(TAG, &quot;unable to close() socket during connection failure&quot;, e2); } // Start the service over to restart listening mode //BluetoothSerial.this.start(); return; } // catch (IOException e) // Reset the ConnectThread because we're done synchronized (BluetoothSerialService.this) { mConnectThread = null; } // Start the connected thread // connected(mmSocket, mmDevice); // /// connected(socket, socket.getRemoteDevice(), mSocketType); connected(mmSocket, mmDevice, mSocketType); } // public void run()<br> <br> public void cancel() { try { mmSocket.close(); } catch (IOException e) { Log.e(TAG, &quot;close() of connect socket failed&quot;, e); } }<br> <br> } // private class ConnectThread<br> <br> ...</em><br> <br> Sorry if the code appears messy. I didn't find a way to preview it properly.<br> I hope this helps,<br> Avner<br>
I found a workaround for the problem of the Android phone (Galaxy S3 4.0.3) not being able to act as a server for automatic connection. <br>I added an option for the Android phone to act as the client and initiate insecure RfcommSocket to the RN-42 BT. <br>Replacing createRfcommSocketToServiceRecord with createInsecureRfcommSocketToServiceRecord results in a succefull connection. <br>The RN-42 BT light turns green and the device communicates, without having to punch in the password (1234). <br> <br>(Zakie, I'm now living across the world but I was exited to find out that this useful reference comes from where I used to live. Thanks again for sharing your code and instructions. It was most useful to me).
Hi Avner, and thanks for finding that issue with ICS and the code. If you don't mind sharing it, that will be useful once I upgrade my phone to Note II or something. <br> <br>P.S. <br> Happy Hanukkah
I did - you can see the commands in the Arduino code snippets in the following links: <br>http://stackoverflow.com/questions/10732252/auto-connecting-to-paired-bluetooth-devices-on-android <br>http://stackoverflow.com/search?q=Auto+connecting+Arduino+to+Android+using+Paired+Bluetooth+Serial <br> <br>Anyways, this seems to be a problem for Android ICW (I'm using 4.0.4). <br>It is reported in several places such as: <br>http://code.google.com/p/android/issues/detail?id=34161 <br>http://stackoverflow.com/questions/11082819/bluetooth-connection-on-android-ics-not-possible <br>
Thanks Avner. I do not have any ICW based code device. My Samsung Note (first one, not the new II) is 2.3.6. See more comments I made in the StackOverflow link.<br><br>That was my first time writing something myself with BT and Android. I explored it to get hand-on on both.
Hi zmashiah, <br> <br>I have a bluetooth serial device (RN42) that is connected to Arduino Uno. <br>I am able to connect to it from Android (Galxsy S3), but the connection has to be initiated from the Android phone every time. <br>Instead I am trying to set the Android phone as a host and set the bluetooth serial device to auto-connect mode. <br>Then the bluetooth serial device can serve as the client and initiate a connection automatically when it starts. <br>I'm am following your instructions and example code (thanks for sharing it) <br> <br>However, I am not able to establish the connection from the Arduino to the Android phone. <br>The Bluetooth service is stuck in the AcceptThread on: socket = mmServerSocket.accept(); <br>I posted my observations also in http://stackoverflow.com/search?q=Auto+connecting+Arduino+to+Android+using+Paired+Bluetooth+Serial <br> <br>Do you have any ideas how I can debug this problem, to see why the connection is not established?
Did you follow the section: Pairing the devices in my explanation?<br> This is step 3 in it.<br> <br> Basically the RN42 is by default not doing this reconnect. You need to tell it to become one.
Great project! You actually answered a few Arduino/Android/BT questions I was having with a project of my own.
Thank you.<br>I always found a working example to be one of the best ways to learn a particular area of technology. Certainly as a developer.<br><br>Always happy to help.

About This Instructable


38 favorites


Bio: An experienced manager in Hi-Tech that never gets bored with technology or life
More by zmashiah: Turning Stereo Amplifer On and Off automatically Colorful Countdown Clock for tight timeline management Bluetooth mobile phone accessory for Missed calls and SMS
Add instructable to: