<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ElecFreaks &#187; open source hardware</title>
	<atom:link href="http://www.elecfreaks.com/tag/open-source-hardware/feed" rel="self" type="application/rss+xml" />
	<link>http://www.elecfreaks.com</link>
	<description></description>
	<lastBuildDate>Tue, 18 Jun 2013 11:25:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Communication between Android and Arduino with Bluetooth(1)</title>
		<link>http://www.elecfreaks.com/677.html</link>
		<comments>http://www.elecfreaks.com/677.html#comments</comments>
		<pubDate>Fri, 17 Jun 2011 04:24:09 +0000</pubDate>
		<dc:creator>ElecFreaks</dc:creator>
				<category><![CDATA[All post of ElecFreaks]]></category>
		<category><![CDATA[Bluetooth Bee]]></category>
		<category><![CDATA[Featured Post]]></category>
		<category><![CDATA[Hack Hack]]></category>
		<category><![CDATA[ADK]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[open source hardware]]></category>

		<guid isPermaLink="false">http://www.elecfreaks.com/?p=677</guid>
		<description><![CDATA[We all know Android is very popular now, especially ADK (Android Open Accessory Development Kit), which allows external 0pen source hardware to connect with Android system by USB and interact with an Android-powered device in a special &#8220;accessory&#8221; mode. We keep working on it for weeks, and try to assemble...<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/677.html' addthis:title='Communication between Android and Arduino with Bluetooth(1) '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<div>We all know Android is very popular now, especially ADK (Android Open Accessory Development Kit), which allows external 0pen source hardware to connect with Android system by USB and interact with an Android-powered device in a special &#8220;accessory&#8221; mode.</div>
<div>We keep working on it for weeks, and try to assemble some bata prototype and make a library for the communication between Android and Arduino by bluetooth and <a href="http://www.elecfreaks.com/store/freaduino-adk-p-193.html" target="_black"><strong>Freaduino </strong></a>for ADK.</div>
<div>With the Google providing source files, we are able to provide this  board from now on. We produce a batch, you can purchase them <strong><a href="http://www.elecfreaks.com/store/bare-boardgoogle-android-open-accessory-toolki-adk-p-169.html" target="_black">here</a></strong>.We will provide the Bata version as soon as possible.</div>
<div>To make the communication between Android and Arduino easy,  we would like show you a new way that android interact with Arduino and other similar boards. Bluetooth for example.</div>
<div>Step one: Make a APP to Android which could communicate with other devices by bluetooth.</div>
<div>Step two: Android APP connect to Arduino by <strong><a href="http://www.elecfreaks.com/store/bluetooth-bee-masterslave-p-171.html" target="_black">Bluetooth Bee</a></strong>.</div>
<div>For step one(Part1), we have just made a little APP for Android, achieve simple bluetooth connection with Android.</div>
<div>
<p><strong> </strong></p>
<p><strong> </strong><strong> </strong></p>
</div>
<div>This APP allows Android connect to each other by bluetooth, so you need at least two Android devices for this demo. Here we used two Android phones which are SAMSUNG I7680 and Lenovo O3. Then, there should be a complete development environment like Eclipse on your PC. You can find many of them in the Internet.</div>
<div>I guess you all get ready, let&#8217;s see more details.</div>
<div>·</div>
<div><strong>Bluetooth</strong></div>
<div>The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications connect wirelessly to other Bluetooth devices, enabling point-to-point and multipoint wireless features.</div>
<div>·</div>
<div><strong>The Basics</strong></div>
<div>All of the Bluetooth APIs are available in the Android bluetooth package. Here is a summary of the classes you will need to create as below.<span> </span></div>
<ul>
<li><span style="color: #ff00ff;">BluetoothAdapter</span>: Represents the local Bluetooth adapter (Bluetooth radio)</li>
<li> <span style="color: #ff00ff;">BluetoothDevice</span>: Represents a remote Bluetooth device, query information such as its name, address, class, and bonding state.</li>
<li> <span style="color: #ff00ff;">BluetoothSocket</span>: Represents the interface for a Bluetooth socket (similar to a TCP Socket).</li>
<li> <span style="color: #ff00ff;">BluetoothServerSocket</span>: Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket).</li>
<li> <span style="color: #ff00ff;">BluetoothClass</span>:  Describes the general characteristics and capabilities of a Bluetooth device.</li>
</ul>
<div>·</div>
<div><strong>Bluetooth Permissions</strong></div>
<div>In order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions: BLUETOOTH and BLUETOOTH_ADMIN.</div>
<div>Declare the Bluetooth permission(s) in your application&#8217;s AndroidManifest.xml as below.</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>manifest ... <span style="color: #339933;">&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>uses<span style="color: #339933;">-</span>permission android<span style="color: #339933;">:</span><span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;android.permission.BLUETOOTH&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; <span style="color: #339933;">&lt;</span>uses<span style="color: #339933;">-</span>permission android<span style="color: #339933;">:</span><span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;android.permission.BLUETOOTH_ADMIN&quot;</span> <span style="color: #339933;">/&gt;</span><br />
&nbsp; ...<br />
<span style="color: #339933;">&lt;/</span>manifest<span style="color: #339933;">&gt;</span></div></div>
</pre>
</div>
<div>Oh, Is it a little boring in front of this?  OK, let us see some code, really start from here.</div>
<div>In order to connect by bluetooth, there should be four steps as below:<span> </span></div>
<ul>
<li><strong>Setting Up Bluetooth</strong></li>
<li><strong> Finding Device</strong></li>
<li><strong>Connecting Device</strong></li>
<li><strong>Managing Device(server/client)</strong></li>
</ul>
<p>·</p>
<div>
<div><strong>Setting Up Bluetooth</strong></div>
</div>
<div><img title="1" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/11.jpg" alt="" width="293" height="271" /></div>
<div>We made the APP include six java files. For the Bata, we only need to use Activity01, DiscoveryActivity, ClientSocketActivity and ServerSocketActivity.</div>
<div>Before your application can communicate with Bluetooth, you should verify Bluetooth is supported on the device and make sure that it is enabled. If Bluetooth is not supported, you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, you are able to request the user enable Bluetooth without leaving your application.</div>
<div>So, at Activity01 we get five buttons, as below:</div>
<div><img title="2" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/21.jpg" alt="" width="201" height="259" /></div>
<div>Select “Open BT” button make the device&#8217;s own Bluetooth adapter (the Bluetooth radio) working. There is one Bluetooth adapter for the entire system, and your application can interact with it when it is open.</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">private</span> BluetoothAdapter _bluetooth <span style="color: #339933;">=</span> BluetoothAdapter.<span style="color: #660066;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></div>
</pre>
</div>
<div>There are two ways to enable bluetooth:</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//startActivityForResult(enabler, REQUEST_ENABLE);</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//enable</span><br />
&nbsp; &nbsp; _bluetooth.<span style="color: #660066;">enable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</pre>
</div>
<div>The frist way, create a new Intent as &#8220;<span>Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)&#8221;<span>, a dialog box will appear and request user permission to enable Bluetooth. Select &#8220;Yes&#8221; and the system will enable Bluetooth and focus will return to your application once the process completes (or fails). &#8220;Yes&#8221; returns RESULT_OK and &#8220;No&#8221; returns RESULT_CANCELED.</span></span></div>
<div><img title="3" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/3.png" alt="" width="200" height="160" /></div>
<div>The other way is force enable Bluetooth instead of dialog, we used this way here.</div>
<div>Next, you need to ensure that Bluetooth is enabled and allowed other devices could discover it. Add the below code, and a dialog box will appear also, you should click &#8220;Yes&#8221;.</div>
<div><img title="4" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/4.png" alt="" width="200" height="192" /></div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; Intent enabler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>BluetoothAdapter.<span style="color: #660066;">ACTION_REQUEST_DISCOVERABLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; startActivityForResult<span style="color: #009900;">&#40;</span>enabler<span style="color: #339933;">,</span> REQUEST_DISCOVERABLE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</pre>
</div>
<div>OK, Setting up device is completed. The full code as below:</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ackage com.<span style="color: #660066;">yarin</span>.<span style="color: #660066;">android</span>.<span style="color: #660066;">Examples_08_09</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">import</span> android.<span style="color: #660066;">app</span>.<span style="color: #660066;">Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">import</span> android.<span style="color: #660066;">bluetooth</span>.<span style="color: #660066;">BluetoothAdapter</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">import</span> android.<span style="color: #660066;">content</span>.<span style="color: #660066;">Intent</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">import</span> android.<span style="color: #660066;">os</span>.<span style="color: #660066;">Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">import</span> android.<span style="color: #660066;">view</span>.<span style="color: #660066;">View</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">public</span> <span style="color: #003366; font-weight: bold;">class</span> Activity01 <span style="color: #003366; font-weight: bold;">extends</span> Activity<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Get Default Adapter */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> BluetoothAdapter&nbsp; _bluetooth&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> BluetoothAdapter.<span style="color: #660066;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* request BT enable */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> static final int&nbsp; REQUEST_ENABLE&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> 0x1<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">/* request BT discover */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> static final int&nbsp; REQUEST_DISCOVERABLE&nbsp; <span style="color: #339933;">=</span> 0x2<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/** Called when the activity is first created. */</span><br />
&nbsp; <span style="color: #339933;">@</span>Override<br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">super</span>.<span style="color: #660066;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #660066;">layout</span>.<span style="color: #660066;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Enable BT */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onEnableButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//startActivityForResult(enabler, REQUEST_ENABLE);</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//enable</span><br />
&nbsp; &nbsp; _bluetooth.<span style="color: #660066;">enable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; Intent enabler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>BluetoothAdapter.<span style="color: #660066;">ACTION_REQUEST_DISCOVERABLE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; startActivityForResult<span style="color: #009900;">&#40;</span>enabler<span style="color: #339933;">,</span> REQUEST_DISCOVERABLE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Close BT */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDisableButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; _bluetooth.<span style="color: #660066;">disable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Start search */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onStartDiscoveryButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Intent enabler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> DiscoveryActivity.<span style="color: #003366; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; startActivity<span style="color: #009900;">&#40;</span>enabler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Client */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onOpenClientSocketButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; Intent enabler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> ClientSocketActivity.<span style="color: #003366; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; startActivity<span style="color: #009900;">&#40;</span>enabler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">/* Server */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onOpenServerSocketButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; Intent enabler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> ServerSocketActivity.<span style="color: #003366; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; startActivity<span style="color: #009900;">&#40;</span>enabler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
</pre>
</div>
<div>·</div>
<div><strong>Finding Device</strong></div>
<div>With Discovery Activity.java file, We will find around device. The process is asynchronous and the method will immediately return with a boolean indicating whether discovery has started successfully. The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each found device to retrieve its Bluetooth name. Your application must register a BroadcastReceiver for the ACTION_FOND. Intent in order to receive information about each device discovered.</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* Register Receiver*/</span><br />
&nbsp; &nbsp; IntentFilter discoveryFilter <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> IntentFilter<span style="color: #009900;">&#40;</span>BluetoothAdapter.<span style="color: #660066;">ACTION_DISCOVERY_FINISHED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; registerReceiver<span style="color: #009900;">&#40;</span>_discoveryReceiver<span style="color: #339933;">,</span> discoveryFilter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; IntentFilter foundFilter <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> IntentFilter<span style="color: #009900;">&#40;</span>BluetoothDevice.<span style="color: #660066;">ACTION_FOUND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; registerReceiver<span style="color: #009900;">&#40;</span>_foundReceiver<span style="color: #339933;">,</span> foundFilter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp;* Receiver<br />
&nbsp; &nbsp;* When the discovery finished be called.<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> BroadcastReceiver _foundReceiver <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> BroadcastReceiver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span>Context context<span style="color: #339933;">,</span> Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* get the search results */</span><br />
&nbsp; &nbsp; &nbsp; BluetoothDevice device <span style="color: #339933;">=</span> intent.<span style="color: #660066;">getParcelableExtra</span><span style="color: #009900;">&#40;</span>BluetoothDevice.<span style="color: #660066;">EXTRA_DEVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* add to list */</span><br />
&nbsp; &nbsp; &nbsp; _devices.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>device<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* show the devices list */</span><br />
&nbsp; &nbsp; &nbsp; showDevices<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> BroadcastReceiver _discoveryReceiver <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> BroadcastReceiver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #339933;">@</span>Override<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span>Context context<span style="color: #339933;">,</span> Intent intent<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* unRegister Receiver */</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">d</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&gt;&gt;unregisterReceiver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; unregisterReceiver<span style="color: #009900;">&#40;</span>_foundReceiver<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; unregisterReceiver<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; _discoveryFinished <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
</pre>
<p>After discovery will return remote device&#8217;s MAC address, device name and class. Using this information, the device performing discovery can then choose to initiate a connection with the discovered device. The following figure is click the &#8220;Search Device&#8221; button.</p>
</div>
<div><img title="5" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/51-261x300.jpg" alt="" width="209" height="240" /></div>
<div>·</div>
<div><strong>Connecting Device and Managing Device</strong></div>
<div>In order to create a connection between your application and two devices, you must implement the both server-side and client-side mechanisms, because one device must open a server socket and the other one must initiate the connection (use the server device&#8217;s MAC address to initiate a connection). So you must used a same UUID, I used one find from internet and you can get the UUID here: <a href="http://www.uuidgenerator.com/" target="_black">the UUID Generator</a>.</div>
<div>One implementation technique is to automatically prepare each device as a server, so that each one has a server socket open and listening for connections. Then either device can initiate a connection with the other one as the client. Alternatively, one device can explicitly &#8220;host&#8221; the connection and open a server socket on demand and the other device can simply initiate the connection.</div>
<div>As Server</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">_serverSocket <span style="color: #339933;">=</span> _bluetooth.<span style="color: #660066;">listenUsingRfcommWithServiceRecord</span><span style="color: #009900;">&#40;</span>PROTOCOL_SCHEME_RFCOMM<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UUID.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a60f35f0-b93a-11de-8a39-08002009c666&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</pre>
</div>
<div>As Client</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">socket <span style="color: #339933;">=</span> device.<span style="color: #660066;">createRfcommSocketToServiceRecord</span><span style="color: #009900;">&#40;</span>UUID.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a60f35f0-b93a-11de-8a39-08002009c666&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
</pre>
</div>
<div>It must select a server and a client, we has made two buttons which are &#8220;AS Client&#8221; and &#8220;AS Server&#8221;. Then one phone will be server and the other will be client.</div>
<div><strong>Server</strong></div>
<div>Frist, this connection used Socket way, so there are some usually functions such as connect, close, listen and so on. and there are others stream control such as getInputStream, getOutputStream &#8230;</div>
<div>select &#8220;AS Server&#8221; button, then you will see the following show.</div>
<div><img title="6" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/61-261x300.jpg" alt="" width="201" height="231" /></div>
<div>The sample code:</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* Client devices*/</span><br />
&nbsp; &nbsp; &nbsp; final List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> lines <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; _handler.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Runnable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Rfcomm server started...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> adapter <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerSocketActivity.<span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.<span style="color: #660066;">R</span>.<span style="color: #660066;">layout</span>.<span style="color: #660066;">simple_list_item_1</span><span style="color: #339933;">,</span> lines<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* accept client request */</span><br />
&nbsp; &nbsp; &nbsp; BluetoothSocket socket <span style="color: #339933;">=</span> _serverSocket.<span style="color: #660066;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">d</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&gt;&gt;Accept Client Request&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* Processing the request content*/</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>socket <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; InputStream inputStream <span style="color: #339933;">=</span> socket.<span style="color: #660066;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; int read <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; final byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> byte<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2048</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>read <span style="color: #339933;">=</span> inputStream.<span style="color: #660066;">read</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int count <span style="color: #339933;">=</span> read<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _handler.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Runnable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder b <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>int i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> count<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String s <span style="color: #339933;">=</span> Integer.<span style="color: #660066;">toHexString</span><span style="color: #009900;">&#40;</span>bytes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span> 0xFF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String s <span style="color: #339933;">=</span> b.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> adapter <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ArrayAdapter<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerSocketActivity.<span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.<span style="color: #660066;">R</span>.<span style="color: #660066;">layout</span>.<span style="color: #660066;">simple_list_item_1</span><span style="color: #339933;">,</span> lines<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setListAdapter<span style="color: #009900;">&#40;</span>adapter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
</pre>
<p><strong>Client</strong></p>
<p>Click the &#8220;AS Client&#8221; button, it will discover remote devices and show the devices list. It will call DiscoveryActivity class and show the devices list at last, then click one device to connect to server, send socket.connect() to server and connect to RFCOMM Channel. We send a buffer to server as below.</p>
</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> connect<span style="color: #009900;">&#40;</span>BluetoothDevice device<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//BluetoothSocket socket = null;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Create a Socket connection: need the server's UUID number of registered</span><br />
&nbsp; &nbsp; &nbsp; socket <span style="color: #339933;">=</span> device.<span style="color: #660066;">createRfcommSocketToServiceRecord</span><span style="color: #009900;">&#40;</span>UUID.<span style="color: #660066;">fromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a60f35f0-b93a-11de-8a39-08002009c666&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; socket.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">d</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&gt;&gt;Client connectted&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; InputStream inputStream <span style="color: #339933;">=</span> socket.<span style="color: #660066;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; OutputStream outputStream <span style="color: #339933;">=</span> socket.<span style="color: #660066;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; outputStream.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#40;</span>byte<span style="color: #009900;">&#41;</span> 0xa0<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>IOException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>socket <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">d</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&gt;&gt;Client Close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>IOException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
</pre>
</div>
<div>Once the Server received the data, it will show on the screen.</div>
<div><img title="7" src="http://www.elecfreaks.com/wp-content/uploads/2011/06/71-254x300.jpg" alt="" width="203" height="240" /></div>
<div>At last, of course is release resources, click &#8220;Stop Server&#8221; and return to main menu, click &#8220;Close BT&#8221; button.</div>
<div>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* Stop server */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> shutdownServer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">new</span> Thread<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _serverWorker.<span style="color: #660066;">interrupt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_serverSocket <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/* close sever */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _serverSocket.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>IOException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;EF-BTBee&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _serverSocket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
</pre>
<pre>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* Close BT */</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDisableButtonClicked<span style="color: #009900;">&#40;</span>View view<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; _bluetooth.<span style="color: #660066;">disable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></div>
</pre>
</div>
<div>OK, This is where the fun begins, you can connect the device and send data to each other. There are many things you can do, right?</div>
<div>Let us see this APP operational processes totally again:</div>
<div>Open BT (enable bluetooth and discoverable) -&gt; AS Server(listen)/AS Clent(search devices and show devices list) -&gt; Select Server from the devices list(client sends data and server receives) -&gt; Show the data on the Server screen -&gt; Stop Server -&gt; Stop BT.</div>
<div>The demo is a Bata version, just a sample code for your reference. We will gradually enrich with it and you are appreciated to redistribute and/or modify it, and please share with us.</div>
<div>·</div>
<div><strong>Download full code here:</strong></div>
<div><a href="http://elecfreaks.com/store/download/datasheet/Bluetooth/BTBee_Examples_By_EF.zip" target="_black">BTBee_Examples_By_EF.zip</a></div>
<div>·</div>
<div><strong>Reference</strong>:</div>
<div><a href="http://www.uuidgenerator.com/" target="_black">UUID Generator</a></div>
<div><a href="http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.html" target="_black">Android Bluetooth Chat</a></div>
<div><a href="http://innovator.samsungmobile.com/cms/cnts/knowledge.detail.view.do?platformId=3&amp;cntsId=4262&amp;listReturnUrl=http%3A%2F%2Finnovator.samsungmobile.com%3A80%2Fcms%2Fcnts%2Fknowledge.list.do%3FplatformId%3D3%26cateId%3D3%26cntsId%3D%26imgType%3D%26searchText%3Dbluetooth%26sortType%3D0%26codeType%3DAll%26indexDirection%3D1%26indexType%3D1%26listLines%3D10%26tabNum%3D1&amp;linkType=0" target="_black">Android Bluetooth API</a></div>
<div><a href="http://developer.android.com/sdk/android-3.1-highlights.html" target="_black">Android 3.1 Platform</a></div>
<div><a href="http://developers.sun.com/mobility/midp/articles/bluetooth1/" target="_black">Wireless Application Programming with J2ME and Bluetooth</a></div>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/677.html' addthis:title='Communication between Android and Arduino with Bluetooth(1) '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.elecfreaks.com/677.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>The Android Open Accessory Toolkit, Do you like it?</title>
		<link>http://www.elecfreaks.com/686.html</link>
		<comments>http://www.elecfreaks.com/686.html#comments</comments>
		<pubDate>Thu, 16 Jun 2011 09:45:58 +0000</pubDate>
		<dc:creator>ElecFreaks</dc:creator>
				<category><![CDATA[All post of ElecFreaks]]></category>
		<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[open source hardware]]></category>

		<guid isPermaLink="false">http://www.elecfreaks.com/?p=686</guid>
		<description><![CDATA[As you kown, several weeks ago, Google officially announced a new project called the Android Open Accessory toolkit. It allows people to connect an open source hardware interfaces like Arduino with Android system. What a exciting news! To be sure, many companies have already been working on Android and their...<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/686.html' addthis:title='The Android Open Accessory Toolkit, Do you like it? '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>As you kown, several weeks ago, Google officially announced a new project called the Android Open Accessory toolkit. It allows people to connect an <a href="http://www.elecfreaks.com/" target="_blank">open source hardware</a> interfaces like Arduino with Android system. What a exciting news!</p>
<p>To be sure, many companies have already been working on Android and their boards, so do we!</p>
<p>Please pay close attention to <a href="http://www.elecfreaks.com/" target="_blank">ElecFreaks</a> and see what we would come up with.</p>
<p>And we appreciate you leave us your ideas or any suggestions. Thank you!</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/686.html' addthis:title='The Android Open Accessory Toolkit, Do you like it? '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.elecfreaks.com/686.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Welcome to ElecFreaks!</title>
		<link>http://www.elecfreaks.com/57.html</link>
		<comments>http://www.elecfreaks.com/57.html#comments</comments>
		<pubDate>Tue, 29 Mar 2011 23:00:44 +0000</pubDate>
		<dc:creator>ElecFreaks</dc:creator>
				<category><![CDATA[All post of ElecFreaks]]></category>
		<category><![CDATA[development platform]]></category>
		<category><![CDATA[electronic design]]></category>
		<category><![CDATA[electronics components]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[open source hardware]]></category>
		<category><![CDATA[PCB service]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.elecfreaks.com/?p=57</guid>
		<description><![CDATA[Welcome to ElecFreaks! ElecFreaks is an open hardware facilitation company based in Shenzhen, China. Benefiting from the largest electronic market, local manufacture power and convenient global logistic system, a vast number of funny and exciting products are created every day. We integrate resources to serve new era of innovation. To...<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/57.html' addthis:title='Welcome to ElecFreaks! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Welcome to ElecFreaks!</p>
<p>ElecFreaks is an open hardware facilitation company based in Shenzhen, China.  Benefiting from the largest electronic market, local manufacture power  and convenient global logistic system, a vast number of funny and  exciting products are created every day. We integrate resources to serve  new era of innovation.</p>
<p id="line56">To make <a href="../" target="_blank"><span style="color: #3366ff;">electronic design</span></a> and innovation easier, we offer various <a href="../" target="_blank"><span style="color: #3366ff;">development platform</span></a>, modules, tools, <a href="../" target="_blank"><span style="color: #3366ff;">electronics components</span></a>, PCB service and other open source hardware.</p>
<p><span style="font-size: small;">If you have any opinions and suggestions, please feel free to contact us. Thank you very much!</span></p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.elecfreaks.com/57.html' addthis:title='Welcome to ElecFreaks! '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.elecfreaks.com/57.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
