Instructables

Bluetooth mobile phone accessory for Missed calls and SMS

Picture of Bluetooth mobile phone accessory for Missed calls and SMS
Phone and Accessory Screen.JPG
Android App screen.jpg
7 Segment display before connecting with Phone.jpg
7 Segment showing 1 missed call, 0 SMS and 89% Battery.jpg
7 Segment showing dots after phone out of range.jpg
2013-07-05 19.20.43.jpg
When at home, I do not carry my phone with me everywhere... so sometimes phone rings or an SMS comes in and I do not hear that. With the volume of music played by the teenagers at home, that is not a surprise :-) so I decided to build a small accessory that will show up the number of missed calls and unread SMS. In order to ensure it is very visible I use a 7 Segment LED display so it can be viewed from distance.

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.

Updated code can be found in here:
https://github.com/zmashiah/PhoneInfo
Made several small enhancements and fixed an issue some where complaining about, where connection was not always made from phone to the device. The problem as I learned was in new Firmware of RN-42 that required a bit of configuration to work with Android. Those changes include using commands SI,0200 and SJ,0200 for the BT module.



 
Remove these adsRemove these ads by Signing Up

Hey Zakie,

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 !

zmashiah (author)  rajanjamuindia5 months ago

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.

Make sure the Android BT MAC is registered with the Arduino BT module too. That saves on the search time as I noticed.

JOHNA_D1 year ago
hey, first of all, thanks for the wonderful project

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.

is there anyone who could enlighten me in how to overcome this problem? total noob here, so any help would be much appreciated.

thanks
zmashiah (author)  JOHNA_D1 year ago
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
SI,0200
SJ,0200
It took me few days to figure that out. Now it works perfect!
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.
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?
avnerm1 year ago
Hi Zakie, The related code in the MainActivity is:

// 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
avnerm1 year ago
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.
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).
zmashiah (author)  avnerm1 year ago
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.

P.S.
Happy Hanukkah
avnerm1 year ago
I did - you can see the commands in the Arduino code snippets in the following links:
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
zmashiah (author)  avnerm1 year ago
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.

That was my first time writing something myself with BT and Android. I explored it to get hand-on on both.
avnerm1 year ago
Hi zmashiah,

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?
zmashiah (author)  avnerm1 year ago
Did you follow the section: Pairing the devices in my explanation?
This is step 3 in it.

Basically the RN42 is by default not doing this reconnect. You need to tell it to become one.
mazikowski2 years ago
Great project! You actually answered a few Arduino/Android/BT questions I was having with a project of my own.
zmashiah (author)  mazikowski2 years ago
Thank you.
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.