title: AndroidStudio Kotlin connect to bluetooth
body: <p>I've added <code>&lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;</code> to my manifest but the error</p>
  
  <blockquote>
    <p>Missing permission required by BluetoothAdapter.isEnabled: android.permission.BLUETOOTH.
    is still there.</p>
  </blockquote>
  
  <p>Also, in <code>ContextCompat.checkSelfPermission(...)</code> what is the first parameter <code>CONTEXT</code>? The documentation <a href="https://developer.android.com/training/permissions/requesting" rel="nofollow noreferrer">https://developer.android.com/training/permissions/requesting</a> does not say.</p>
  
  <p>And am I correct that I need to disconnect and reconnect bluetooth whenever the app is not being used?</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
      var bt: BluetoothAdapter? = null
      var bts: BluetoothSocket? = null
      val REQUEST_BLUETOOTH_PERMISSION: Int = 1
      val REQUEST_BLUETOOTH_ENABLE: Int = 2
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
          {
              Toast.makeText(
                  getApplicationContext(),
                  "Device does not support Bluetooth therefore this application cannot run.",
                  Toast.LENGTH_SHORT
              ).show();
              return;
          }
  
          bt = BluetoothAdapter.getDefaultAdapter()
          if (bt == null) {
              // This device does not have Bluetooth.
              Toast.makeText(
                  getApplicationContext(),
                  "Device does not have a Bluetooth adapter therefore this application cannot run.",
                  Toast.LENGTH_SHORT
              ).show();
              return;
          }
  
          bluetoothConnect();
      }
  
      fun bluetoothConnect() {
          if (ContextCompat.checkSelfPermission(
                  CONTEXT, // What is this? It's not explained at https://developer.android.com/training/permissions/requesting
                  Manifest.permission.BLUETOOTH
              ) == PackageManager.PERMISSION_GRANTED
          ) {
  
              if (bt.isEnabled == false) { // Error: Missing permission required by BluetoothAdapter.isEnabled: android.permission.BLUETOOTH.
                  val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                  startActivityForResult(enableBtIntent, REQUEST_BLUETOOTH_ENABLE)
              } else {
                  val pairedDevices: Set&lt;BluetoothDevice&gt;? = bt.bondedDevices
                  pairedDevices?.forEach { device -&gt;
                      val deviceName = device.name
                      val deviceHardwareAddress = device.address // MAC address
                  }
              }
          }
          else {
              // Request permission. That will call back to onActivityResult which in the case of success will call this method again.
              // Ask for permission.
              ActivityCompat.requestPermissions(
                  this,
                  arrayOf(Manifest.permission.BLUETOOTH),
                  REQUEST_BLUETOOTH_PERMISSION
              )
          }
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
  
          if (requestCode == REQUEST_BLUETOOTH_PERMISSION) {
              if (resultCode == RESULT_OK) {
                  bluetoothConnect();
              } else {
                  Toast.makeText(
                      getApplicationContext(),
                      "This application cannot run because it does not have Bluetooth permission.",
                      Toast.LENGTH_SHORT
                  ).show();
                  // Do we need to quit? How?
              }
          }
          else if( requestCode == REQUEST_BLUETOOTH_ENABLE)
          {
              if(resultCode == RESULT_OK)
              {
                  // try again
                  bluetoothConnect();
              }
              else {
                  Toast.makeText(
                      getApplicationContext(),
                      "This application cannot run because Bluetooth is not enabled and could not be enabled.",
                      Toast.LENGTH_SHORT
                  ).show();
                  // Do we need to quit? How?
              }
          }
      }
  
  
      override fun onPause() {
          super.onPause()
          // Release Bluetooth
      }
  
      override fun onResume() {
          super.onResume()
          // Connect Bluetooth
      }
  
      override fun onStop() {
          super.onStop()
          // Release Bluetooth
      }
  
      override fun onStart() {
          super.onStart()
          // Connect Bluetooth
      }
  
  }
  </code></pre>
  
  <p>Edit: </p>
  
  <ul>
  <li>added additional BT check to code, </li>
  <li>adding manifest</li>
  </ul>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rwb.btconnectortest"&gt;
  
      &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
      &lt;!--&lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;--&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/btconnectortestTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>