Bluetooth + Pim API
What bluetooh is
Bluetooth is a simple, wireless and safe technology that permits communication among devices, such as smartphones, computers, mouses, keyboards, etc.
What PIM API is
PIM API is a group of interfaces and classes that allow access to personal information databases. This optional package permits CRUD (create, retrieve, update, and delete) operations on: Contact lists, Event lists, and To-Do lists.
What our midlet do
Our midlet uses bluetooth to send contacts (name, address, and phone) among devices and to discover how many contacts in common have between two devices.
How our midlet join Bluetooth and PIM API
- Send contacts among devices
The bluetooth communication among devices is based on messages (String). The client chooses one device to send a string containing the contact’s name,address, and phone . The string format is: “name #/# address#/#phone”, we use “#/#” to separate the fields, but you can use any character set you want. When the server receives a string, we use a function to get each field and create a new contact on user’s contact list. The user can have more than one contact list, but only the first contact list will save the new contact.
The server just receives a string through bluetooth containing name, address and telephone, and save the new contact on one the user contact list.
The fields name, address and phone was chosen because they are the most important fields when we create a new contact on contact list. The others fields (birthday, e-mail, nickname, photo, etc) could also be used, although in order to make things easier to understand we use only the three main fields.
We tested our midlet in two devices and was verified that each one has two contact lists. The contact list name is different from one device to another. You can get all contact list names by using “String [] myList = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST).” On our example you are able to choose any contact list and any contact you want to send.
Code (client)
After choosing a contact the next step is to choose which device is going to receive the contact. We use the lines below to make the local device discoverable and to find other devices.
local_device = LocalDevice.getLocalDevice(); disc_agent = local_device.getDiscoveryAgent(); local_device.setDiscoverable(DiscoveryAgent.GIAC); disc_agent.startInquiry(DiscoveryAgent.GIAC, this); uuid = new UUID(0x0003);
After choosing one device we open a connection, transform our contact into a string using the function “sendContato” and finally send the contact through bluetooth.
String url = servico .getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); try{ //openning a connection conn = (StreamConnection)Connector.open(url); sendContato(conn.openOutputStream()); conn.close(); display.setCurrent(new Alert("Contact sent!"), previousForm); } catch (Exception e) { e.printStackTrace(); }
private void sendContato(OutputStream output) { String s = ""; if (contato.getPIMList().isSupportedField(Contact.FORMATTED_NAME)) { s += contato.getString(Contact.FORMATTED_NAME, 0)+" #/#"; } else { s += " #/#"; } if (contato.getPIMList().isSupportedField(Contact.FORMATTED_ADDR)) { s += contato.getString(Contact.FORMATTED_ADDR, 0)+" #/#"; } else { s += " #/#"; } if (contato.getPIMList().isSupportedField(Contact.TEL)) { s += contato.getString(Contact.TEL, 0); } else { s += " "; } try { output.write(s.getBytes()); output.flush(); output.close(); } catch (IOException e) { } }
Code (server)
The server opens a connection and wait until a client sends the contact, open a InputStream to receive data from the client, turn the inputStream into a string using the function “slurp”and save the contact using the function “salva contato”.
UUID uuid = new UUID(0x0003); LocalDevice localDevice = LocalDevice.getLocalDevice(); localDevice.setDiscoverable(DiscoveryAgent.GIAC); // open a connection con = (StreamConnectionNotifier)Connector.open("btspp://localhost:"+uuid +";name=examplo;authorize=false"); // the code below blocks the thread until establish a contact with a client StreamConnection conn = con.acceptAndOpen(); InputStream input = conn.openInputStream(); //======== String recebido = slurp(input); //======= salvaContato(recebido); public String slurp(InputStream in) throws IOException { String s = ""; int len; while ((len = in.read())>=0) { s += (char)len; } return s; }
private String salvaContato(String s) { String nome = s.substring(0, s.indexOf("#/#")); s = s.substring(s.indexOf("#/#")+3); String endereco = s.substring(0, s.indexOf("#/#")); s = s.substring(s.indexOf("#/#")+3); String tel = s; ContactList contacts = null; try { // Retrieve the contact list contacts = (ContactList)PIM.getInstance() .openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); Contact contact = contacts.createContact(); if (contacts.isSupportedField(Contact.FORMATTED_NAME)) { contact.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, nome); } if (contacts.isSupportedField(Contact.TEL)) { contact.addString(Contact.TEL, Contact.ATTR_HOME, tel); } if (contacts.isSupportedField(Contact.FORMATTED_ADDR)) { contact.addString(Contact.FORMATTED_ADDR, PIMItem.ATTR_NONE, endereco); } contact.commit(); contacts.close(); } catch (Exception e) { } return nome+" / "+endereco+" / "+tel; }
- Common contacts among devices
In the following example we will use bluetooth to discover common contacts among devices. In the beginning of our MIDlet the user chooses if s/he wants to share his/her contacs. To explain the code we are using two devices (device A and device B) and supposing that both are able to share contacts. The device A searches and chooses other devices looking for commom contacts and device B waits connections and sends contacts.
After chossing the option “Buscar contatos em comum” the device A chooses and stablishes a connection with device B. The device “B” sends a string to device “A” with all user contact’s phones. The string format is: “phone#/#phone#/#… .” When the device “A” receives the string, the function “busca” is used to search for common contacts between device A and device B (if the phone number has more than eight digits, the comparison is made by the last ten digits ).
The function “busca” gets each contact’s phone from device “A” and verifies if there is on the string received from device “B”, a substring corresponding to the contact’s phone. If exists, we found a contact in common and we store this contact in a vector.
public static Vector busca(String telefones) throws Exception { Vector answer = new Vector(); String[] lists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST); ContactList contactList; Enumeration items; String tel = ""; for (int i = 0; i<1; i++) { contactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.getInstance().READ_WRITE, lists[i]); if (contactList.isSupportedField(Contact.TEL)) { items = contactList.items(); while (items.hasMoreElements()) { Contact contact = (Contact)items.nextElement(); if (contact.countValues(Contact.TEL)>0) { tel = contact.getString(Contact.TEL, 0); if (tel.length()>8){ tel= tel.substring(tel.length()-10, tel.length()-1); } if (telefones.indexOf(tel)>-1) { answer.addElement(contact); } } } } } return answer; }
The code below is a function we used to transform all contacts into string format “phone#/#phone#/#… .”
public static String TransformAllContactsToString() throws Exception { String answer = ""; String[] lists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST); ContactList contactList; Enumeration items; for (int i = 0; i<lists.length; i++) { contactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.getInstance().READ_WRITE, lists[i]); if (contactList.isSupportedField(Contact.TEL)) { items = contactList.items(); while (items.hasMoreElements()) { Contact contact = (Contact)items.nextElement(); if (contact.countValues(Contact.TEL)>0) { answer += contact.getString(Contact.TEL, 0)+"#/#"; } } } } return answer; }
- Screenshots
Below the first screen of our MIDlet. There the user can choose if s/he wants to share his/her contacts among devices.
The next screen is the main screen. There are 5 options: list contacts, search contacts, send contact, receive contact , and buscar contatos em comum .

==== Sending contacts among devices ====
If the user chooses “send contact”, the next screen shows all available contact lists. After choosing one available list the next screen will look like this:

The user must choose one contact and select “send”.

After this, our MIDlet will look for available devices. The result appears on another screen.
After choosing the device, If everything runs ok the next screen on client is:
On server the next screen is an alert with the message “Contact received”.
==== Searching commom contacts among devices ====
(We suppose that the device B accepted the option of share contacts. As soon as the user chooses the option “yes”on the first screen, a connection wiil be open and will wait until a device make a request ).
The device A chooses the option “Buscar contatos em comum” on the main screen:

After that the device will search for available bluetooth devices. After finishing the search the next screen will look like this:
After choosing one device the next screen is an alert. If there is no contact in common, the alert message is:”Nenhum contato em comum”, but if there are more than one contact in common, the alert message is: “Temos X contatos em comum”. The letter “X” represents the number of commom contacts. The next screen will show a list with all contacts in commom.
Leave a comment