The display shows up the battery status, Missed calls count and Unread SMS on 8 digits 7 Segment display module. The communication with the accessory is using Bluetooth and one Arduino board that interfaces with the display and the Bluetooth unit.
You need to run the program once (per boot of the phone), and even if the program is in the background, when the accessory and the phone are in range of Bluetooth communication, the display will show the status of your phone. Pretty much like Car hands free set, that automatically connects with your phone.
The code runs on Android phone and source code of the Arduino accessory and the Android phone code are shared here in step 4.
Remove these ads by
Signing UpStep 1: What you need to assemble it?
2. Bluetooth module like this one, cost is ~$ 40 at Sparkfun. To use cheaper one ($ 10 on eBay) you will need minor code modifications which I tend to do for a new assembly of this.
3. 7 Segment display , cost is ~ $10
4. Wires for connecting and potentially a power supply for driving the accessory and enclosure. I did not complete this part yet.
Do yourself a favor and look for the items on eBay. It will save you money.
Total cost, if assembled by items you shop on eBay can be about $ 30. If you go for traditional on-line stores, cost will be higher.





































Visit Our Store »
Go Pro Today »




the original code from zmashia works when replacing with createInsecureRfcommSocketToServiceRecord ...
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 :(
any solution to this? do you know where it hangs? or waits around?
// Indicator if the BluetoothSerialService is used a a server or as a client to establish the connection
public static final boolean useBluetoothSerialServiceAsServerForConnection = false;
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
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:
private ConnectThread mConnectThread;
...
/** * 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() */
public synchronized void start() { Log.i(TAG, "BEG start"); Log.i(TAG, "mState: " + mState); // If we do not do that, accessory will disconnect foo5and connect again on application resume if (mState == STATE_CONNECTED) { return; } Log.i(TAG, "start() cleaning threads"); // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } Log.i(TAG, "start() creating AcceptThread"); 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, "END start"); }
/** * 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 */
public synchronized void connected( BluetoothSocket socket, BluetoothDevice device, String socketType ) { if (D) Log.d(TAG, "BEG connected"); Log.d(TAG, "PhoneInfo.connected(): socket type:" + socketType + "Canceling other threads"); // 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, "PhoneInfo.connected(): Creating connected thread"); mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Log.d(TAG, "PhoneInfo.connected(): Sending device name to calling activity"); sendDeviceNameToActivity(device.getName()); setState(STATE_CONNECTED); } // public synchronized void connected
// Stop all threads public synchronized
void stop() { Log.d(TAG, "PhoneInfo.stop(): stop all threads");// 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()
/** * This thread runs while attempting to make an outgoing connection * with a device. It runs straight through; the connection either * succeeds or fails. */
private class ConnectThread extends Thread
{
private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; private String mSocketType;
public ConnectThread(BluetoothDevice device, boolean secure) { mmDevice = device; BluetoothSocket tmp = null; mSocketType = secure ? "Secure":"Insecure"; // 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, "create() failed", e); } mmSocket = tmp; } // public ConnectThread
public void run() { Log.i(TAG, "BEGIN ConnectThread::mConnectThread"); setName("ConnectThread"); // 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, "unable to close() socket during connection failure", 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()
public void cancel() { try { mmSocket.close(); } catch (IOException e) { Log.e(TAG, "close() of connect socket failed", e); } }
} // private class ConnectThread
...
Sorry if the code appears messy. I didn't find a way to preview it properly.
I hope this helps,
Avner
I added an option for the Android phone to act as the client and initiate insecure RfcommSocket to the RN-42 BT.
Replacing createRfcommSocketToServiceRecord with createInsecureRfcommSocketToServiceRecord results in a succefull connection.
The RN-42 BT light turns green and the device communicates, without having to punch in the password (1234).
(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).
P.S.
Happy Hanukkah
http://stackoverflow.com/questions/10732252/auto-connecting-to-paired-bluetooth-devices-on-android
http://stackoverflow.com/search?q=Auto+connecting+Arduino+to+Android+using+Paired+Bluetooth+Serial
Anyways, this seems to be a problem for Android ICW (I'm using 4.0.4).
It is reported in several places such as:
http://code.google.com/p/android/issues/detail?id=34161
http://stackoverflow.com/questions/11082819/bluetooth-connection-on-android-ics-not-possible
That was my first time writing something myself with BT and Android. I explored it to get hand-on on both.
I have a bluetooth serial device (RN42) that is connected to Arduino Uno.
I am able to connect to it from Android (Galxsy S3), but the connection has to be initiated from the Android phone every time.
Instead I am trying to set the Android phone as a host and set the bluetooth serial device to auto-connect mode.
Then the bluetooth serial device can serve as the client and initiate a connection automatically when it starts.
I'm am following your instructions and example code (thanks for sharing it)
However, I am not able to establish the connection from the Arduino to the Android phone.
The Bluetooth service is stuck in the AcceptThread on: socket = mmServerSocket.accept();
I posted my observations also in http://stackoverflow.com/search?q=Auto+connecting+Arduino+to+Android+using+Paired+Bluetooth+Serial
Do you have any ideas how I can debug this problem, to see why the connection is not established?
This is step 3 in it.
Basically the RN42 is by default not doing this reconnect. You need to tell it to become one.
I always found a working example to be one of the best ways to learn a particular area of technology. Certainly as a developer.
Always happy to help.