<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michael Xiong on Medium]]></title>
        <description><![CDATA[Stories by Michael Xiong on Medium]]></description>
        <link>https://medium.com/@michaelxiong2001?source=rss-c0ec4a6ffff1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Xa2ZEQ3rQ1BSF7GF.</url>
            <title>Stories by Michael Xiong on Medium</title>
            <link>https://medium.com/@michaelxiong2001?source=rss-c0ec4a6ffff1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:13:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michaelxiong2001/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Bluetooth Part I — Enabling Bluetooth]]></title>
            <link>https://medium.com/@michaelxiong2001/bluetooth-part-i-enabling-bluetooth-372dc2d42cfd?source=rss-c0ec4a6ffff1------2</link>
            <guid isPermaLink="false">https://medium.com/p/372dc2d42cfd</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Michael Xiong]]></dc:creator>
            <pubDate>Fri, 12 Jan 2018 20:47:19 GMT</pubDate>
            <atom:updated>2018-01-12T20:47:19.051Z</atom:updated>
            <content:encoded><![CDATA[<p>Connecting (or attempting to connect) two devices through Bluetooth is a complicated and difficult process. This frustrating objective can’t be made easy, but I am hoping that this tutorial may provide insight on how to deal with the problems that arise on the way.</p><p>The first step to creating Bluetooth connection was by far the least frustrating — enabling a device to be found by other Bluetooth devices. This characteristic is known as “discoverability.” Enabling discoverability involves approving certain permissions within the Android device. In order to do this, use a BluetoothAdapter. The BluetoothAdapter must be imported:</p><pre><strong>import </strong>android.bluetooth.BluetoothAdapter;</pre><p>Next, create a BluetoothAdapter as an instance variable, initializing it as the default adapter of your device:</p><pre><strong>private </strong>BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.<em>getDefaultAdapter</em>();</pre><p>Once you wish for your device to become discoverable, whether it is once the app begins running or after the press of a button or some other action, create and call a method that uses your BluetoothAdapter to enable Bluetooth on your device. Here is an example method:</p><pre><strong>protected boolean </strong>setUpBT(){<br><em>        </em><strong>if </strong>(<strong>mBluetoothAdapter </strong>== <strong>null</strong>) {<br>            <em>// Device does not support Bluetooth<br>            </em>Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;This device does not support Bluetooth&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>            <strong>return false</strong>;<br>        }<br>        <strong>else if </strong>(!<strong>mBluetoothAdapter</strong>.isEnabled()) {<br>            Intent enableBtIntent = <strong>new </strong>Intent(BluetoothAdapter.<strong><em>ACTION_REQUEST_ENABLE</em></strong>);<br>            startActivityForResult(enableBtIntent, <strong><em>REQUEST_ENABLE_BT</em></strong>);<br>            Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;Please enable Bluetooth NOW!!!!!&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>            <strong>return false</strong>;<br>        }<br>        <strong>else</strong>{<br>            Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;Yay&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>            <strong>return true</strong>;<br>        }<br>}</pre><p>All of the if statements in the code above are necessary in order to avoid errors that may crash the app. The method is a boolean because following steps should only be taken if this method returns true. The first if statement checks whether or not your device is capable of using Bluetooth. The third statement is only reached if Bluetooth has already been enabled. It notifies the user of this fact and returns true, indicating the app is able to proceed. The second, and most complicated, statement uses an Intent to request Bluetooth to be enabled if it is not already enabled. It then returns false because, until Bluetooth is enabled, the code should not be received.</p><p>Once the user enables Bluetooth, the Activity “enableBtIntent” will be completed and onActivityResult will be called with request code <strong><em>REQUEST_ENABLE_BT, </em></strong>which is a constant that you must initialize with your instance variables.. By overriding the onActivityResult method, you can create a desired effect:</p><pre>@Override<br><strong>protected void </strong>onActivityResult(<strong>int </strong>requestCode, <strong>int </strong>resultCode, Intent data) {<br>    <strong>super</strong>.onActivityResult(requestCode, resultCode, data);<br>    <strong>if</strong>(requestCode == <strong><em>REQUEST_ENABLE_BT</em></strong>){<br>        Log.<em>d</em>(<strong>&quot;Working:&quot;</strong>, <strong>&quot;Bluetooth enabled&quot;</strong>);<br>        Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;Bluetooth enabled&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>    }<br>}</pre><p>Once Bluetooth has been enabled, you may request your device to be made discoverable. Much like requesting the enabling of Bluetooth, this requires an Intent to launch an Activity:</p><pre><strong>if</strong>(!(<strong>mBluetoothAdapter</strong>.getScanMode() == BluetoothAdapter.<strong><em>SCAN_MODE_CONNECTABLE_DISCOVERABLE </em></strong>) &amp;&amp; setUpBT()) {<br>    Intent discoverableIntent =<br>            <strong>new </strong>Intent(BluetoothAdapter.<strong><em>ACTION_REQUEST_DISCOVERABLE</em></strong>);<br>    discoverableIntent.putExtra(BluetoothAdapter.<strong><em>EXTRA_DISCOVERABLE_DURATION</em></strong>, <strong><em>REQUEST_ENABLE_DIS</em></strong>);<br>    startActivityForResult(discoverableIntent, <strong><em>REQUEST_ENABLE_DIS</em></strong>);<br>}</pre><p>The constant <strong><em>REQUEST_ENABLE_DIS</em></strong> must be initialized by you, and its numerical value shall be the duration of time, in seconds, for which your device will be discoverable. Once discoverability has been enabled, onActivityResult will be called. Overriding this method will allow you to notify the user that they were successful:</p><pre>@Override<br>    <strong>protected void </strong>onActivityResult(<strong>int </strong>requestCode, <strong>int </strong>resultCode, Intent data) {<br>        <strong>super</strong>.onActivityResult(requestCode, resultCode, data);<br>        <strong>if</strong>(resultCode == <strong><em>REQUEST_ENABLE_DIS</em></strong>){<br>            Log.<em>d</em>(<strong>&quot;Working&quot;</strong>, <strong>&quot;Made discoverable&quot;</strong>);<br>            Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;Made discoverable&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>            <strong>discoverable </strong>= <strong>true;</strong><em><br>        </em>}<br>        <strong>else if</strong>(resultCode == <strong><em>RESULT_CANCELED</em></strong>){<br>            Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;Discoverability rejected&quot;</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br>        }<br>    }</pre><p>The example above notifies the user with a Toast, leaves a message in the Logcat, and changes the value of an instance variable to indicate that discoverability has been enabled. In addition, if the user does not allow discoverability, this will also lead to a result and the user will be notified.</p><p>Now, you have successfully enabled the user to turn on discoverability of their Bluetooth device. However, the device is only discoverable for some period of time (which is determined by the value of <strong><em>REQUEST_ENABLE_DIS</em></strong>). If you would like to notify the user once this time is up, set up a BroadcastReceiver to listen for the Intent <strong><em>ACTION_SCAN_MODE_CHANGED</em></strong>. This Intent is broadcast every time the discoverability of the device is changed, so you should use an if statement to check whether or not the broadcast indicates that discoverability is being closed or opened. This can be done by extracting extras from the <strong><em>ACTION_SCAN_MODE_CHANGED </em></strong>Intent which tells what the previous and current scan modes are. However, I found it more convenient to use an instance variable (named simply “discoverable”) which was set to true when discoverability was enabled:</p><pre><strong>filter </strong>= <strong>new </strong>IntentFilter(BluetoothAdapter.<strong><em>ACTION_SCAN_MODE_CHANGED</em></strong>);<br><strong>broadcastReceiver </strong>= <strong>new </strong>BroadcastReceiver() {<br>            @Override<br>            <strong>public void </strong>onReceive(Context context, Intent intent) {<br>                Log.<em>d</em>(<strong>&quot;Working&quot;</strong>, <strong>&quot;Broadcast received&quot;</strong>);<br>                <strong>if</strong>(intent.getAction().equals(BluetoothAdapter.<strong><em>ACTION_SCAN_MODE_CHANGED</em></strong>)){<br>                    <strong>if</strong>(<strong>discoverable</strong>) {<br>                        Log.<em>d</em>(<strong>&quot;Working&quot;</strong>, <strong>&quot;Connection session ended&quot;</strong>);<br>                        <strong>codeDisplay</strong>.setText(<strong>&quot;Your session to connect has timed out&quot;</strong>);<br>                        <strong>discoverable </strong>= <strong>false</strong>;<br>                    }<br>                    <strong>else</strong>{<br>                        Log.<em>d</em>(<strong>&quot;Working&quot;</strong>, <strong>&quot;Connection session started&quot;</strong>);<br>                    }<br>                }<br><em>//                else if(intent.getAction().equals(&quot;ENABLE_DIS&quot;)){<br>//                    Toast.makeText(context, &quot;broadcast received&quot;, Toast.LENGTH_SHORT).show();<br>//                }<br>            </em>}<br>};<br>registerReceiver(<strong>broadcastReceiver</strong>, <strong>filter</strong>);</pre><p>broadcastReceiver and filter are both instance variables. The BroadcastReceiver functions in an interesting way; it requires an IntentFilter that sets to the specific message the receiver is meant to pick up. Without this filter, the BroadcastReceiver will not work. <br>In order for your code not to crash, make sure to unregister the receiver upon closing the app:</p><pre>@Override<br><strong>protected void </strong>onDestroy() {<br>    <strong>if </strong>(broadcastReceiver != <strong>null</strong>) {<br>        unregisterReceiver(broadcastReceiver);<br>        broadcastReceiver = <strong>null</strong>;<br>    }<br>    <strong>super</strong>.onDestroy();<br>}</pre><p>Congratulations! You have completely the least frustrating step in setting up Bluetooth for your device. Hopefully, with the help of this tutorial, this step did not take too long. Good luck on your project! The new tutorial will be posted shortly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=372dc2d42cfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>