<?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 bonbon on Medium]]></title>
        <description><![CDATA[Stories by bonbon on Medium]]></description>
        <link>https://medium.com/@nsittee?source=rss-f54e91e96b72------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W1DcZmf8DWjV6jIwwWdpSA.jpeg</url>
            <title>Stories by bonbon on Medium</title>
            <link>https://medium.com/@nsittee?source=rss-f54e91e96b72------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:54:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nsittee/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[Scanning For Bluetooth Low Energy Device with Android Application using Kotlin]]></title>
            <link>https://medium.com/@nsittee/scanning-for-bluetooth-low-energy-device-with-android-application-using-kotlin-614e244b21e?source=rss-f54e91e96b72------2</link>
            <guid isPermaLink="false">https://medium.com/p/614e244b21e</guid>
            <category><![CDATA[bluetooth-low-energy]]></category>
            <category><![CDATA[bluetooth]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[bonbon]]></dc:creator>
            <pubDate>Sun, 03 Nov 2019 15:31:50 GMT</pubDate>
            <atom:updated>2019-11-03T15:31:50.119Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>This is the first article or that I ever wrote on this site after a long time of lurking and reading other’s article, I finally found the courage to share the knowledge. Since I’m currently doing my project about android application and IoT device using Bluetooth as a mean of communication, I might as well document what I found along the way, so it might come as useful to others that are struggling the same problem that I found.</p><h3>Objective</h3><p>In order to communicate and exchange data with the Bluetooth device, the application will need to find that device first. After finishing this article, you will be able to scan and retrieve the information of the Bluetooth device, ready to exchange the data.</p><h4>LeScanner</h4><p>First of all, we create a Kotlin class that will be an object that will connect and handle all the works in order to get the nearby Bluetooth device.</p><p><em>BluetoothAdapter </em>and <em>BluetoothLeScanner </em>is an object that deals with bluetooth device for the Android application. <em>Handler </em>will help us when we want to delay a specific action for a period of time, like the length of scan period. And finally, <em>InitialPair </em>is the activity that handle the scanning process.</p><pre><strong>class </strong>LeScanner {<br>    <strong>var bta</strong>: BluetoothAdapter<br>    <strong>var bls</strong>: BluetoothLeScanner<br>    <strong>var handler</strong>: Handler<br>    <strong>var isScan </strong>= <strong>false<br><br>    var scanPeriod</strong>: Long<br>    <strong>var strengthThreshold </strong>= -60<br>    <strong>lateinit var ip</strong>: InitialPair<br>.<br>.<br>.<br>}</pre><h4>Constructor</h4><p><em>period</em> is the length of time for the <em>LeScanner </em>to scan for other Le Device, and the <em>strength </em>is the minimum strength or signal that this Scanner will pickup, and lower than this will be ignored. -60 is a good threshold for the start. The higher the value, the stronger the signal is.</p><pre><strong>constructor</strong>(period: Long, strength: Int, activity: InitialPair){<br>    <strong>this</strong>.<strong>scanPeriod </strong>= period<br>    <strong>this</strong>.<strong>strengthThreshold </strong>= strength<br>    <strong>this</strong>.<strong>ip </strong>= activity<br><br>    <strong>handler </strong>= Handler()<br><br>    <strong>var </strong>btm = <strong>ip</strong>.getSystemService(Context.<em>BLUETOOTH_SERVICE</em>) <br>              <strong>as </strong>BluetoothManager<br>    <strong>bta </strong>= btm.<em>adapter<br>    </em><strong>bls </strong>= <strong>bta</strong>.<em>bluetoothLeScanner<br></em>}</pre><h4>Start Scanning</h4><p>This function will initiate the scanning procedure, called by the activity that start this process.</p><p><em>bls.startScan() </em>is how you can start the scanning and to stop, you call <em>bls.stopScan(). </em>You can simply call <em>startScan()</em> right away, but in order to stop the scanning process. You need to add a delay to this. One way you can achieve this is to use Handler function, called <em>postDelayed(). </em>The function take the period of time as a milliseconds unit, so 5000 <em>scanPeriod </em>means 5 seconds</p><p>Both of these function take <em>ScanCallback </em>as an argument, which we need to make next.</p><pre><strong>fun </strong>scanLeDevice(){<br>    <strong>if</strong>(!<strong>isScan</strong>){<br>        <strong>bls</strong>.startScan(<strong>callBack</strong>)<br>        <strong>this</strong>.<strong>isScan </strong>= <strong>true<br><br>        handler</strong>.postDelayed(<em>Runnable </em>()<strong>{</strong><br>            <strong>bls</strong>.stopScan(<strong>callBack</strong>)<br>            <strong>this</strong>.<strong>isScan </strong>= <strong>false</strong><br>        <strong>}</strong>, <strong>scanPeriod</strong>)<br>    }<br>}</pre><h3><strong><em>ScanCallback</em></strong></h3><p>To create a callback for this job, we declare them in the same class. Once the neaby BLE device is found, this callback will handle what to do next. Now this where you plan how to display the device that it just found. For example, we can simply store them in an <em>ArrayList, </em>then we can just use the <em>ListView </em>and <em>Adapter </em>together to display them nicely in the activity.</p><pre><strong>private val callBack</strong>: ScanCallback = <strong>object </strong>: ScanCallback(){<br>    <strong>override fun </strong>onScanResult(callbackType:Int, result:ScanResult) {<br>        <strong>super</strong>.onScanResult(callbackType, result)<br>        <strong>var </strong>rssi = result.<em>rssi<br>        </em><strong>var </strong>bd = result.<em>device<br><br>        </em><strong>if</strong>(rssi &gt; <strong>strengthThreshold</strong>){<br>            ..... // Store the bd and rssi to display later<br>        }<br>    }<br>}</pre><h3>Conclusion</h3><p>That’s the simple way for the Android application to find the nearby Bluetooth Low Energy device. Note that in order to run this code, you cannot use an emulator, because of the Bluetooth module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=614e244b21e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>