<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Jon-Schuster/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Jon-Schuster/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Jon-Schuster/Posts</link>
    <language>en</language>
    <pubDate>Sun, 19 May 2013 20:36:41 GMT</pubDate>
    <lastBuildDate>Sun, 19 May 2013 20:36:41 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>1</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>Facebook Contacts</title>
      <description><![CDATA[<span id="c4fmetadata">
<table cellspacing="0" cellpadding="1" width="100%" border="0">
<tbody>
<tr class="entry_overview">
<td width="50">&nbsp;</td>
<td><span class="entry_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.
</span></td>
</tr>
<tr>
<td colspan="2">
<div class="entry_author">Jon Schuster</div>
<div class="entry_company"><a href="http://www.claritycon.com/Coding4Fun">Clarity Consulting</a></div>
<br>
<div class="entry_details"><b>Difficulty: </b><span class="entry_details_input">Intermediate</span></div>
<div class="entry_details"><b>Time Required:</b> <span class="entry_details_input">
6-10 hours</span></div>
<div class="entry_details"><b>Cost: </b><span class="entry_details_input">Free</span></div>
<div class="entry_details"><b>Software: </b><span class="entry_details_input"><a href="http://msdn.com/express/">Visual Basic or Visual C# Express Editions</a>,
<a href="http://www.microsoft.com/windows/products/windowsvista/default.mspx">Microsoft Windows Vista</a>,
<a href="http://www.codeplex.com/C4FDevKit">Coding4Fun Developer Toolkit</a>, <a href="http://www.codeplex.com/FacebookToolkit">
Facebook Developer Toolkit</a></span></div>
<div class="entry_details"><b>Hardware: </b><span class="entry_details_input">Bluetooth-enabled computer and phone</span></div>
<div class="entry_details"><b>Download: </b><a href="http://channel9.msdn.com/ShowPost.aspx?PostID=368920">Download</a>
</div>
</td>
</tr>
</tbody>
</table>
</span>
<p>&nbsp;</p>
<p>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. </p>
<p>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.
</p>
<p>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.
</p>
<p align="center"><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/6893024/facebookContactsScreen.jpg"><img height="244" alt="facebookContactsScreen" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/6893024/facebookContactsScreen_thumb.jpg" width="189" border="0"></a>&nbsp;
</p>
<p align="center"><em>Figure 1: The Facebook Contacts main screen</em></p>
<p>&nbsp; </p>
<h3>Retrieving Information From Facebook</h3>
<p>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
<a href="http://developers.facebook.com/get_started.php">Get Started</a> page will lead you through the process.
</p>
<p>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:&nbsp;&nbsp;
</p>
<p><strong>C#</strong></p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e2685cef-9939-4f50-b429-62844338b26e">
<pre><div><span>private</span><span> </span><span>void</span><span> LoadFriends()
{
    List</span><span>&lt;</span><span>User</span><span>&gt;</span><span> users </span><span>=</span><span> </span><span>new</span><span> List</span><span>&lt;</span><span>User</span><span>&gt;</span><span>();

    </span><span>try</span><span>
    {
        users </span><span>=</span><span> </span><span>new</span><span> List</span><span>&lt;</span><span>User</span><span>&gt;</span><span>(facebookService1.GetFriends());
    }
    </span><span>catch</span><span> (FacebookInvalidUserException)
    {
        </span><span>this</span><span>.Close();
        </span><span>return</span><span>;
    }
    </span><span>catch</span><span> (FacebookException)
    {
        </span><span>//</span><span> do nothing: this exception just means no friends were returned,
        </span><span>//</span><span> so we can leave the users list empty</span><span>
</span><span>    }

    users.Sort(Compare);
    _friends </span><span>=</span><span> users;
}</span></div></pre>
</div>
<p></p>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a8c24887-0553-41bc-bff0-05bb4fdd53c9">
<pre><div><span>Private</span><span> </span><span>Sub</span><span> LoadFriends()
    </span><span>Dim</span><span> users </span><span>As</span><span> List(</span><span>Of</span><span> User) </span><span>=</span><span> </span><span>New</span><span> List(</span><span>Of</span><span> User)()

    </span><span>Try</span><span>
        users </span><span>=</span><span> </span><span>New</span><span> List(</span><span>Of</span><span> User)(facebookService1.GetFriends())
    </span><span>Catch</span><span> e1 </span><span>As</span><span> FacebookInvalidUserException
        </span><span>Me</span><span>.Close()
        </span><span>Return</span><span>
    </span><span>Catch</span><span> e2 </span><span>As</span><span> FacebookException
        </span><span>'</span><span> do nothing: this exception just means no friends were returned,</span><span>
</span><span>        </span><span>'</span><span> so we can leave the users list empty</span><span>
</span><span>    </span><span>End</span><span> </span><span>Try</span><span>

    users.Sort(</span><span>AddressOf</span><span> </span><span>Compare</span><span>)
    _friends </span><span>=</span><span> users
</span><span>End Sub</span></div></pre>
</div>
<p>I sort the users by comparing their last names, and then their first names if the last names are equal:
</p>
<p><strong>C#</strong></p>
<p></p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ae8e1984-1fe3-4376-a736-5bca3cf8d7b2">
<pre><div><span>private</span><span> </span><span>int</span><span> Compare(User leftUser, User rightUser)
{
    </span><span>int</span><span> lastNameComparison </span><span>=</span><span> leftUser.LastName.CompareTo(rightUser.LastName);

    </span><span>if</span><span> (lastNameComparison </span><span>!=</span><span> </span><span>0</span><span>)
    {
        </span><span>return</span><span> lastNameComparison;
    }

    </span><span>return</span><span> leftUser.FirstName.CompareTo(rightUser.FirstName);
}</span></div></pre>
</div>
<p></p>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:23e2d613-ef02-4217-9756-5bd1cd1c5504">
<pre><div><span>Private</span><span> </span><span>Function</span><span> </span><span>Compare</span><span>(</span><span>ByVal</span><span> leftUser </span><span>As</span><span> User, </span><span>ByVal</span><span> rightUser </span><span>As</span><span> User) </span><span>As</span><span> </span><span>Integer</span><span>
    </span><span>Dim</span><span> lastNameComparison </span><span>As</span><span> </span><span>Integer</span><span> </span><span>=</span><span> leftUser.LastName.CompareTo(rightUser.LastName)

    </span><span>If</span><span> lastNameComparison </span><span>&lt;&gt;</span><span> </span><span>0</span><span> </span><span>Then</span><span>
        </span><span>Return</span><span> lastNameComparison
    </span><span>End</span><span> </span><span>If</span><span>

    </span><span>Return</span><span> leftUser.FirstName.CompareTo(rightUser.FirstName)
</span><span>End Function</span></div></pre>
</div>
<p></p>
<p></p>
<p>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:</p>
<p><strong>C#</strong></p>
<p></p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:d1ace490-4dcd-483e-b1e8-2ad0ab9dba9b">
<pre><div><span>private</span><span> List</span><span>&lt;</span><span>string</span><span>&gt;</span><span> GetNetworks()
{
    List</span><span>&lt;</span><span>string</span><span>&gt;</span><span> networkNames </span><span>=</span><span> </span><span>new</span><span> List</span><span>&lt;</span><span>string</span><span>&gt;</span><span>();

    </span><span>foreach</span><span> (User friend </span><span>in</span><span> _friends)
    {
        </span><span>foreach</span><span> (Network network </span><span>in</span><span> friend.Affiliations)
        {
            </span><span>if</span><span> (</span><span>!</span><span>networkNames.Contains(network.Name))
            {
                networkNames.Add(network.Name);
            }
        }
    }

    networkNames.Sort();
    networkNames.Insert(</span><span>0</span><span>, ALL_NETWORKS);

    </span><span>return</span><span> networkNames;
}</span></div></pre>
</div>
<p></p>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e69fb205-3250-42a2-832b-dc898a54fc67">
<pre><div><span>Private</span><span> </span><span>Function</span><span> GetNetworks() </span><span>As</span><span> List(</span><span>Of</span><span> </span><span>String</span><span>)
    </span><span>Dim</span><span> networkNames </span><span>As</span><span> List(</span><span>Of</span><span> </span><span>String</span><span>) </span><span>=</span><span> </span><span>New</span><span> List(</span><span>Of</span><span> </span><span>String</span><span>)()

    </span><span>For</span><span> </span><span>Each</span><span> [</span><span>friend</span><span>] </span><span>As</span><span> User </span><span>In</span><span> _friends
        </span><span>For</span><span> </span><span>Each</span><span> network </span><span>As</span><span> Network </span><span>In</span><span> [</span><span>friend</span><span>].Affiliations
            </span><span>If</span><span> (</span><span>Not</span><span> networkNames.Contains(network.Name)) </span><span>Then</span><span>
                networkNames.Add(network.Name)
            </span><span>End</span><span> </span><span>If</span><span>
        </span><span>Next</span><span> network
    </span><span>Next</span><span> [</span><span>friend</span><span>]

    networkNames.Sort()
    networkNames.Insert(</span><span>0</span><span>, ALL_NETWORKS)

    </span><span>Return</span><span> networkNames
</span><span>End Function</span></div></pre>
</div>
<p></p>
<p></p>
<p>This list was displayed as a combo box on my form. Then, whenever the selection in the combo box changed, I had to filter the displayed list:
</p>
<p><strong>C#</strong></p>
<p></p>
<p></p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:eecfb59d-4110-4cc7-b7b3-6891fe574552">
<pre><div><span>private</span><span> </span><span>void</span><span> FilterDisplayedFriends(</span><span>string</span><span> selectedNetwork)
{
    List</span><span>&lt;</span><span>User</span><span>&gt;</span><span> friendsToDisplay;

    </span><span>if</span><span> (selectedNetwork.Equals(ALL_NETWORKS))
    {
        friendsToDisplay </span><span>=</span><span> _friends;
    }
    </span><span>else</span><span>
    {
        friendsToDisplay </span><span>=</span><span> </span><span>new</span><span> List</span><span>&lt;</span><span>User</span><span>&gt;</span><span>();

        </span><span>foreach</span><span> (User friend </span><span>in</span><span> _friends)
        {
            </span><span>foreach</span><span> (Network network </span><span>in</span><span> friend.Affiliations)
            {
                </span><span>if</span><span> (network.Name.Equals(selectedNetwork))
                {
                    friendsToDisplay.Add(friend);
                    </span><span>continue</span><span>;
                }
            }
        }
    }

    Display(friendsToDisplay);
}</span></div></pre>
</div>
<p></p>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:81f5fa11-78f9-4e4a-88eb-c1b4ef0f65b8">
<pre><div><span>Private</span><span> </span><span>Sub</span><span> FilterDisplayedFriends(</span><span>ByVal</span><span> selectedNetwork </span><span>As</span><span> </span><span>String</span><span>)
    </span><span>Dim</span><span> friendsToDisplay </span><span>As</span><span> List(</span><span>Of</span><span> User)

    </span><span>If</span><span> selectedNetwork.Equals(ALL_NETWORKS) </span><span>Then</span><span>
        friendsToDisplay </span><span>=</span><span> _friends
    </span><span>Else</span><span>
        friendsToDisplay </span><span>=</span><span> </span><span>New</span><span> List(</span><span>Of</span><span> User)()

        </span><span>For</span><span> </span><span>Each</span><span> [</span><span>friend</span><span>] </span><span>As</span><span> User </span><span>In</span><span> _friends
            </span><span>For</span><span> </span><span>Each</span><span> network </span><span>As</span><span> Network </span><span>In</span><span> [</span><span>friend</span><span>].Affiliations
                </span><span>If</span><span> network.Name.Equals(selectedNetwork) </span><span>Then</span><span>
                    friendsToDisplay.Add([</span><span>friend</span><span>])
                    </span><span>Continue</span><span> </span><span>For</span><span>
                </span><span>End</span><span> </span><span>If</span><span>
            </span><span>Next</span><span> network
        </span><span>Next</span><span> [</span><span>friend</span><span>]
    </span><span>End</span><span> </span><span>If</span><span>

    Display(friendsToDisplay)
</span><span>End Sub</span></div></pre>
</div>
<p></p>
<h3>Connecting to the Phone via Bluetooth</h3>
<p></p>
<p>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). </p>
<p><strong>C#</strong>&nbsp; </p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:4e4d2253-28d2-4044-8418-3cf3d198e9e3">
<pre><div><span>private</span><span> </span><span>void</span><span> FillContact(Contact contact, User facebookUser, WebClient webClient)
{
    SimpleContactView simpleContact </span><span>=</span><span> </span><span>new</span><span> SimpleContactView(contact);
    simpleContact.FirstName </span><span>=</span><span> facebookUser.FirstName;
    simpleContact.LastName </span><span>=</span><span> facebookUser.LastName;
    Location location </span><span>=</span><span> facebookUser.CurrentLocation;

    </span><span>if</span><span> (location </span><span>!=</span><span> </span><span>null</span><span>)
    {
        simpleContact.HomeCity </span><span>=</span><span> location.City;

        </span><span>if</span><span> (location.StateAbbreviation </span><span>!=</span><span> Facebook.Entity.StateAbbreviation.Unknown)
        {
            simpleContact.HomeState </span><span>=</span><span> location.StateAbbreviation.ToString();
        }

        </span><span>if</span><span> (location.Country </span><span>!=</span><span> Facebook.Entity.Country.Unknown)
        {
            simpleContact.HomeCountry </span><span>=</span><span> ToString(location.Country);
        }

        simpleContact.HomeZip </span><span>=</span><span> location.ZipCode;
    }

    </span><span>if</span><span> (facebookUser.Sex </span><span>==</span><span> Facebook.Entity.Gender.Female)
    {
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Female;
    }
    </span><span>else</span><span> </span><span>if</span><span> (facebookUser.Sex </span><span>==</span><span> Facebook.Entity.Gender.Male)
    {
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Male;
    }
    </span><span>else</span><span>
    {
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Unspecified;
    }

    </span><span>if</span><span> (</span><span>!</span><span>facebookUser.PictureUrl.Equals(DEFAULT_FACEBOOK_PICTURE))
    {
        </span><span>string</span><span> pictureFile </span><span>=</span><span> Path.GetTempFileName();
        webClient.DownloadFile(facebookUser.PictureUrl, pictureFile);

        </span><span>using</span><span> (FileStream fs </span><span>=</span><span> </span><span>new</span><span> FileStream(pictureFile, FileMode.Open))
        {
            contact.Photos[PhotoLabels.UserTile] </span><span>=</span><span> </span><span>new</span><span> Contacts.Photo(fs, </span><span>&quot;</span><span>image</span><span>&quot;</span><span>);
        }

        File.Delete(pictureFile);
    }
}</span></div></pre>
</div>
<p></p>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:8f194394-3848-4b1b-8926-03da73bc4771">
<pre><div><span>Private</span><span> </span><span>Sub</span><span> FillContact(</span><span>ByVal</span><span> contact </span><span>As</span><span> Contact, </span><span>ByVal</span><span> facebookUser </span><span>As</span><span> User, </span><span>ByVal</span><span> webClient </span><span>As</span><span> WebClient)
    </span><span>Dim</span><span> simpleContact </span><span>As</span><span> SimpleContactView </span><span>=</span><span> </span><span>New</span><span> SimpleContactView(contact)
    simpleContact.FirstName </span><span>=</span><span> facebookUser.FirstName
    simpleContact.LastName </span><span>=</span><span> facebookUser.LastName
    </span><span>Dim</span><span> location </span><span>As</span><span> Location </span><span>=</span><span> facebookUser.CurrentLocation

    </span><span>If</span><span> </span><span>Not</span><span> location </span><span>Is</span><span> </span><span>Nothing</span><span> </span><span>Then</span><span>
        simpleContact.HomeCity </span><span>=</span><span> location.City

        </span><span>If</span><span> location.StateAbbreviation </span><span>&lt;&gt;</span><span> Facebook.Entity.StateAbbreviation.Unknown </span><span>Then</span><span>
            simpleContact.HomeState </span><span>=</span><span> location.StateAbbreviation.ToString()
        </span><span>End</span><span> </span><span>If</span><span>

        </span><span>If</span><span> location.Country </span><span>&lt;&gt;</span><span> Facebook.Entity.Country.Unknown </span><span>Then</span><span>
            simpleContact.HomeCountry </span><span>=</span><span> ToString(location.Country)
        </span><span>End</span><span> </span><span>If</span><span>

        simpleContact.HomeZip </span><span>=</span><span> location.ZipCode
    </span><span>End</span><span> </span><span>If</span><span>

    </span><span>If</span><span> facebookUser.Sex </span><span>=</span><span> Facebook.Entity.Gender.Female </span><span>Then</span><span>
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Female
    </span><span>ElseIf</span><span> facebookUser.Sex </span><span>=</span><span> Facebook.Entity.Gender.Male </span><span>Then</span><span>
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Male
    </span><span>Else</span><span>
        simpleContact.Gender </span><span>=</span><span> Contacts.Gender.Unspecified
    </span><span>End</span><span> </span><span>If</span><span>

    </span><span>If</span><span> (</span><span>Not</span><span> facebookUser.PictureUrl.Equals(DEFAULT_FACEBOOK_PICTURE)) </span><span>Then</span><span>
        </span><span>Dim</span><span> pictureFile </span><span>As</span><span> </span><span>String</span><span> </span><span>=</span><span> Path.GetTempFileName()
        webClient.DownloadFile(facebookUser.PictureUrl, pictureFile)

        </span><span>Using</span><span> fs </span><span>As</span><span> FileStream </span><span>=</span><span> </span><span>New</span><span> FileStream(pictureFile, FileMode.Open)
            contact.Photos(PhotoLabels.UserTile) </span><span>=</span><span> </span><span>New</span><span> Contacts.Photo(fs, </span><span>&quot;</span><span>image</span><span>&quot;</span><span>)
        </span><span>End</span><span> </span><span>Using</span><span>

        File.Delete(pictureFile)
    </span><span>End</span><span> </span><span>If</span><span>
</span><span>End Sub</span></div></pre>
</div>
<p></p>
<p>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.
</p>
<p>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:
</p>
<p><strong>C#</strong></p>
<p></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:dc670fe9-8263-4a4a-9ac8-c642a5ec47d7">
<pre><div><span>private</span><span> </span><span>void</span><span> SendToPhone(IEnumerable</span><span>&lt;</span><span>User</span><span>&gt;</span><span> users)
{
    Device device </span><span>=</span><span> (Device) cmbDevices.SelectedItem;
    RemoteService pushService;

    </span><span>try</span><span>
    {
       pushService </span><span>=</span><span> deviceServicesManager1.DiscoverServicesByType(device, ServiceType.OBEXObjectPush).ElementAt(</span><span>0</span><span>);
    }
    </span><span>catch</span><span>
    {
        MessageBox.Show(</span><span>&quot;</span><span>Could not connect to Bluetooth device </span><span>&quot;</span><span> </span><span>&#43;</span><span> cmbDevices.Text </span><span>&#43;</span><span> </span><span>&quot;</span><span>. It may be out of range.</span><span>&quot;</span><span>);
        </span><span>return</span><span>;
    }

    </span><span>try</span><span>
    {
        oppManager1.NetworkStream </span><span>=</span><span> pushService.Connect();
    }
    </span><span>catch</span><span> (ServiceConnectionException)
    {
        MessageBox.Show(</span><span>&quot;</span><span>Could not connect to Bluetooth device </span><span>&quot;</span><span> </span><span>&#43;</span><span> device.Name);
        </span><span>return</span><span>;
    }

    oppManager1.Connect();
    WebClient webClient </span><span>=</span><span> </span><span>new</span><span> WebClient();

    </span><span>foreach</span><span> (User user </span><span>in</span><span> users)
    {
        Contact contact </span><span>=</span><span> CreateContact(user, webClient);
        </span><span>string</span><span> tempFile </span><span>=</span><span> Path.GetTempFileName();
        </span><span>string</span><span> tempVCard </span><span>=</span><span> Path.GetDirectoryName(tempFile) </span><span>&#43;</span><span> </span><span>&quot;</span><span>\\</span><span>&quot;</span><span> </span><span>&#43;</span><span> Path.GetFileNameWithoutExtension(tempFile) </span><span>&#43;</span><span> </span><span>&quot;</span><span>.vcf</span><span>&quot;</span><span>;
        File.Delete(tempFile);
        contactsService1.SaveContactToVCard(contact, tempVCard);
        oppManager1.PushObject(tempVCard);
        File.Delete(tempVCard);
    }

    oppManager1.Disconnect();
    pushService.Disconnect();
    MessageBox.Show(</span><span>&quot;</span><span>Successfully transferred </span><span>&quot;</span><span> </span><span>&#43;</span><span> users.Count() </span><span>&#43;</span><span> </span><span>&quot;</span><span> friends to </span><span>&quot;</span><span> </span><span>&#43;</span><span> device.Name </span><span>&#43;</span><span> </span><span>&quot;</span><span>.</span><span>&quot;</span><span>);
}</span></div></pre>
</div>
<p><strong>VB.NET</strong></p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a6a3d0a3-8f49-47e5-92ba-6c262bd892d6">
<pre><div><span>Private</span><span> </span><span>Sub</span><span> SendToPhone(</span><span>ByVal</span><span> users </span><span>As</span><span> IEnumerable(</span><span>Of</span><span> User))
    </span><span>Dim</span><span> device </span><span>As</span><span> Device </span><span>=</span><span> </span><span>CType</span><span>(cmbDevices.SelectedItem, Device)
    </span><span>Dim</span><span> pushService </span><span>As</span><span> RemoteService

    </span><span>Try</span><span>
        pushService </span><span>=</span><span> deviceServicesManager1.DiscoverServicesByType(device, ServiceType.OBEXObjectPush).ElementAt(</span><span>0</span><span>)
    </span><span>Catch</span><span>
        MessageBox.Show(</span><span>&quot;</span><span>Could not connect to Bluetooth device </span><span>&quot;</span><span> </span><span>&amp;</span><span> cmbDevices.Text </span><span>&amp;</span><span> </span><span>&quot;</span><span>. It may be out of range.</span><span>&quot;</span><span>)
        </span><span>Return</span><span>
    </span><span>End</span><span> </span><span>Try</span><span>

    </span><span>Try</span><span>
        oppManager1.NetworkStream </span><span>=</span><span> pushService.Connect()
    </span><span>Catch</span><span> e1 </span><span>As</span><span> ServiceConnectionException
        MessageBox.Show(</span><span>&quot;</span><span>Could not connect to Bluetooth device </span><span>&quot;</span><span> </span><span>&amp;</span><span> device.Name)
        </span><span>Return</span><span>
    </span><span>End</span><span> </span><span>Try</span><span>

    oppManager1.Connect()
    </span><span>Dim</span><span> webClient </span><span>As</span><span> WebClient </span><span>=</span><span> </span><span>New</span><span> WebClient()

    </span><span>For</span><span> </span><span>Each</span><span> user </span><span>As</span><span> User </span><span>In</span><span> users
        </span><span>Dim</span><span> contact </span><span>As</span><span> Contact </span><span>=</span><span> CreateContact(user, webClient)
        </span><span>Dim</span><span> tempFile </span><span>As</span><span> </span><span>String</span><span> </span><span>=</span><span> Path.GetTempFileName()
        </span><span>Dim</span><span> tempVCard </span><span>As</span><span> </span><span>String</span><span> </span><span>=</span><span> Path.GetDirectoryName(tempFile) </span><span>&amp;</span><span> </span><span>&quot;</span><span>\</span><span>&quot;</span><span> </span><span>&amp;</span><span> Path.GetFileNameWithoutExtension(tempFile) </span><span>&amp;</span><span> </span><span>&quot;</span><span>.vcf</span><span>&quot;</span><span>
        File.Delete(tempFile)
        contactsService1.SaveContactToVCard(contact, tempVCard)
        oppManager1.PushObject(tempVCard)
        File.Delete(tempVCard)
    </span><span>Next</span><span> user

    oppManager1.Disconnect()
    pushService.Disconnect()
    MessageBox.Show(</span><span>&quot;</span><span>Successfully transferred </span><span>&quot;</span><span> </span><span>&amp;</span><span> users.Count() </span><span>&amp;</span><span> </span><span>&quot;</span><span> friends to </span><span>&quot;</span><span> </span><span>&amp;</span><span> device.Name </span><span>&amp;</span><span> </span><span>&quot;</span><span>.</span><span>&quot;</span><span>)
</span><span>End Sub</span></div></pre>
</div>
<p>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.
</p>
<h3>Conclusion</h3>
<p>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.</p>
<h3>About the Author</h3>
<p>Jon Schuster is a consultant at Clarity Consulting, and can be reached at his e-mail address, jschuster [at] claritycon [dot] com</p>
 <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Jon-Schuster/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:a9d93b6395b24ac0bff79e7600d0b2bc">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/Facebook-Contacts</comments>
      <itunes:summary>



&amp;nbsp;
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.




Jon Schuster
Clarity Consulting

Difficulty: Intermediate
Time Required: 
6-10 hours
Cost: Free
Software: Visual Basic or Visual C# Express Editions,
Microsoft Windows Vista,
Coding4Fun Developer Toolkit, 
Facebook Developer Toolkit
Hardware: Bluetooth-enabled computer and phone
Download: Download






&amp;nbsp; 
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</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/Facebook-Contacts</link>
      <pubDate>Sat, 29 Dec 2007 02:35:04 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/Facebook-Contacts</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/6893024_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/6893024_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Jon Schuster</dc:creator>
      <itunes:author>Jon Schuster</itunes:author>
      <slash:comments>5</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/Facebook-Contacts/RSS</wfw:commentRss>
    </item>    
</channel>
</rss>