Facebook Contacts

Description

By using the Facebook Developer Toolkit and the Bluetooth and Contacts APIs from the Coding4Fun Developer Toolkit, I created an application which transfers information about your Facebook friends to your mobile phone.

Facebook is a great social networking site which keeps track of all kinds of information about your friends. Since things like your friends’ hometown, picture, and even the correct spelling of their name are already provided for you there, it would be great
if you could automatically send all of that information to your phone, instead of having to manually type it in yourself. By using a couple of different toolkits, I was able to create a simple application which does all of this for me without writing a lot
of code.

Disclaimer: Unfortunately, the Facebook API does not allow you to get a user’s contact information, such as phone number or e-mail address. If it did, they would certainly be included here, but as it is I’m limited to the information that Facebook provides.

To give a quick overview, the sample first uses the Facebook Developer Toolkit to connect to Facebook and download information about the user’s friends. These are displayed in a list on the main form, and multiple users can be selected with checkboxes. Then,
using the Bluetooth API from the Coding4Fun Developer Toolkit, I get a list of Bluetooth devices (including mobile phones) within range, allowing the user to select one of them. Finally, when the user clicks “OK”, I use the Contacts API, also from the Coding4Fun
toolkit, to turn each of the selected users into a transferable file, and then send it to the phone over a Bluetooth connection. The following sections describe this process in more detail.

Figure 1: The Facebook Contacts main screen

Retrieving Information From Facebook

To get started developing applications with the Facebook API, you first need to register your application and get an API key from Facebook which will allow you to connect to their site and download data. Their
Get Started page will lead you through the process.

Now that you have an API key, you can start developing. While you can connect to Facebook and get the information manually through HTTP REST calls, I chose to use the Facebook Developer Toolkit, which took care of maintaining a connection, requesting the
information, and parsing the response into objects that I can use in code. The following code loads the user’s friends into memory, using the included FacebookService component:

On Facebook, most users are a part of one or more “networks”, such as “Chicago, IL”, or “Notre Dame”. For my application, I wanted to be able to filter the displayed friends according to their associated networks. To do this, I first had to get a list of
networks from all my Facebook friends:

Connecting to the Phone via Bluetooth

Now we have a list of friends that we can search through and select from. The next step is to transform the users we are going to send to the phone into vCard files. vCards are a common, human-readable file format for exchanging contact information between
computers or other devices. Fortunately, the Contacts API released with the Coding4Fun Toolkit can create vCards for me. All I have to do is provide the mapping from the properties of a Facebook User object to those of a Contact object (using the included
SimpleContactView).

Finally, now that we can create vCards for Facebook users, we can make one for each selected user and send them off to the phone. First, however, we need to learn a little bit about how Bluetooth works. Every Bluetooth device implements a set of “profiles”,
which are interface specifications that let Bluetooth devices know how to communicate with each other. Each profile is responsible for providing a different kind of service, such as phone headset communication, video streaming, and file transfer. To communicate
with another Bluetooth device, you have to first find out if the device supports the profile you wish to use, connect to the device using that profile, and use the interface defined by the profile to accomplish your tasks.

For this application, I used the Object Push Profile (known as OBEX Object Push in the API), which is specifically designed to transfer objects such as vCards from one phone to another. The OppMananger component in the API takes care of the more difficult
parts of making the connection. Once I’m connected, I just create a vCard for each user, transfer each one, and disconnect when I’m done:

Voila! You can now see your Facebook friends, including their profile pictures, on your phone. This application can also save Facebook friends as Windows Contacts. The code is mostly identical, except that the contacts get saved directly to the local machine
using the ContactsService component, instead of using Bluetooth to connect to a separate device.

Conclusion

The three APIs I used for this project (Facebook, Bluetooth, and Contacts) made the application a breeze to set up. With the exception of one or two bits of code that could be improved, the APIs were all very simple and easy to use, and took care of all
the low-level details so that I could focus on the code relevant to this project. There’s still more work that could be done, including more ways to search through Facebook friends besides the network and improving load time. Overall, though, I’m very pleased
with how it came out, and I hope this has shown you how you can take advantage of some of these APIs to create a really useful tool.

About the Author

Jon Schuster is a consultant at Clarity Consulting, and can be reached at his e-mail address, jschuster [at] claritycon [dot] com