title: How to scan for bluetooth devices with kotlin
body: <p>I am writing an android app in kotlin and I want to scan for nearby bluetooth devices and <strong>NOT</strong> the ones previously paired and I am <strong>NOT</strong> using BLE.</p>
  <p>So far I have tried to use the bluetooth adapter's function startDiscovery() that will supposingly start descovering devices but nothing happens. According to the <a href="https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()" rel="nofollow noreferrer">developer.android.com</a> it should scan for 12 seconds. In my code it stops immediately after startin. The code below is what I have so far. I will put the recycleView adapter as well so that everyone can easily test the app.</p>
  <h3>RecycleView Adapter</h3>
  <pre class="lang-java prettyprint-override"><code>package com.example.andruino_bt
  
  import android.bluetooth.BluetoothDevice
  import android.support.v7.widget.RecyclerView
  import android.util.Log
  import android.view.View
  import android.view.ViewGroup
  import kotlinx.android.synthetic.main.activity_main_recycle_view.view.*
  
  class RecyclerAdapter(private val devices: ArrayList&lt;BluetoothDevice&gt;) : RecyclerView.Adapter&lt;RecyclerAdapter.DeviceHolder&gt;(){
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceHolder {
          val inflatedView = parent.inflate(R.layout.activity_main_recycle_view, false)
          return DeviceHolder(inflatedView)
      }
  
      override fun getItemCount(): Int {
          Log.i(&quot;&quot;, devices.size.toString())
          return devices.size
      }
  
      override fun onBindViewHolder(holder: DeviceHolder, position: Int) {
          val itemDevice = devices[position]
          holder.bindDevice(itemDevice)
      }
  
  
      class DeviceHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
  
          private var view: View = v
          private var device : BluetoothDevice? = null
  
  
          init {
              v.setOnClickListener(this)
          }
  
  
          override fun onClick(v: View) {
              Log.i(&quot;RecyclerView&quot;, &quot;CLICK!&quot;)
          }
  
          fun bindDevice(device: BluetoothDevice) {
              this.device = device
              val name = &quot;Name: ${device.name}&quot;
              view.device_name.text = name
              val address = &quot;Address: ${device.address}&quot;
              view.device_add.text = address
              Log.i(&quot;RecyclerView&quot;, device.name.toString())
          }
  
          companion object {
              private val PHOTO_KEY = &quot;PHOTO&quot;
          }
      }
  }
  </code></pre>
  <hr />
  <h3>Inflate function (in separate file)</h3>
  <pre class="lang-java prettyprint-override"><code>package com.example.andruino_bt
  
  import android.support.annotation.LayoutRes
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  
  fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
      return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
  }
  </code></pre>
  <hr />
  <h3>Main Activity</h3>
  <pre class="lang-java prettyprint-override"><code>package com.example.andruino_bt
  
  import android.app.Activity
  import android.bluetooth.BluetoothAdapter
  import android.bluetooth.BluetoothDevice
  import android.content.BroadcastReceiver
  import android.content.Context
  import android.content.Intent
  import android.content.IntentFilter
  import android.support.v7.app.AppCompatActivity
  import android.os.Bundle
  import android.support.v7.widget.LinearLayoutManager
  import android.util.Log
  import android.widget.Toast
  import kotlinx.android.synthetic.main.activity_main.*
  import android.support.v4.widget.SwipeRefreshLayout
  
  
  
  class MainActivity : AppCompatActivity(){
      private lateinit var linearLayoutManager: LinearLayoutManager
      private lateinit var m_pairedDevices: Set&lt;BluetoothDevice&gt;
      private lateinit var adapter: RecyclerAdapter
  
      private var m_bluetoothAdapter: BluetoothAdapter? = null
      private val REQUEST_ENABLE_BLUETOOTH = 1
      private val devices_list : ArrayList&lt;BluetoothDevice&gt; = ArrayList()
      private val swipeContainer: SwipeRefreshLayout? = null
  
  
      companion object {
          val EXTRA_ADDRESS: String = &quot;Device_address&quot;
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          supportActionBar?.hide()
  
  
  
          m_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
          if(m_bluetoothAdapter == null) {
              Toast.makeText(this, &quot;this device doesn't support bluetooth&quot;, Toast.LENGTH_SHORT).show()
              return
          }
          if(!m_bluetoothAdapter!!.isEnabled) {
              val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
              startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
  
              val discoverableIntent: Intent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE).apply {
                  putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300)
              }
              startActivity(discoverableIntent)
  
          }else{
              discoverDevices()
          }
  
          findViewById&lt;SwipeRefreshLayout&gt;(R.id.swipeContainer).setOnRefreshListener{
  
              devices_list.clear()
              discoverDevices()
          }
  
      }
  
      private val mReceiver = object : BroadcastReceiver() {
          override fun onReceive(context: Context, intent: Intent) {
              val action = intent.action
              if (BluetoothDevice.ACTION_FOUND == action) {
                  // A Bluetooth device was found
                  // Getting device information from the intent
                  val device = intent.getParcelableExtra&lt;BluetoothDevice&gt;(BluetoothDevice.EXTRA_DEVICE)
                  devices_list.add(device)
              }
          }
      }
  
      private fun discoverDevices(){
          if (m_bluetoothAdapter!!.isDiscovering) {
              // Bluetooth is already in mode discovery mode, we cancel to restart it again
              m_bluetoothAdapter!!.cancelDiscovery()
          }
          val bool = m_bluetoothAdapter?.startDiscovery()
          Log.i(&quot;&quot;, bool.toString())
          val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
          registerReceiver(mReceiver, filter)
  
  
          linearLayoutManager = LinearLayoutManager(this)
          recyclerView.layoutManager = linearLayoutManager
  
          adapter = RecyclerAdapter(devices_list)
          recyclerView.adapter = adapter
          findViewById&lt;SwipeRefreshLayout&gt;(R.id.swipeContainer).isRefreshing = false
          unregisterReceiver(mReceiver)
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
          if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
              if (resultCode == Activity.RESULT_OK) {
                  if (m_bluetoothAdapter!!.isEnabled) {
                      Toast.makeText(this, &quot;Bluetooth enabled&quot;, Toast.LENGTH_SHORT).show()
                      discoverDevices()
                  } else {
                      Toast.makeText(this, &quot;Bluetooth disabled&quot;, Toast.LENGTH_SHORT).show()
                  }
              } else if (resultCode == Activity.RESULT_CANCELED) {
                  Toast.makeText(this, &quot;Bluetooth enabling has been canceled&quot;, Toast.LENGTH_SHORT).show()
              }
          }
      }
  }
  </code></pre>
  <hr />
  <p>As a layout I use a RecycleView in a swipeContainer for refreshing. Also in the manifest I have the below permissions:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot;/&gt;
  </code></pre>
  <p>The biggest part of the code was found <a href="https://www.raywenderlich.com/1560485-android-recyclerview-tutorial-with-kotlin#toc-anchor-009" rel="nofollow noreferrer">here (recycleView)</a> and <a href="https://github.com/appsinthesky/Kotlin-Bluetooth/blob/master/app/src/main/java/com/appsinthesky/bluetoothtutorial/SelectDeviceActivity.kt" rel="nofollow noreferrer">here (bluetooth)</a>. Note that the initial bluetooth code in the link is set to find previusly paired devices. That part worked greate form me but it wasn't what I wanted.</p>
  <p>Sorry for the long post and if you need any more info tell me in a comment so I add that too. Thanks in advance.</p>