title: Unable to Connect to BLE Device using Custom App without Workaround using nRF Connect
body: <p><strong>Summary and Background of Problem</strong><br></p>
  
  <p>As part of a college project, we are needing to connect to a Bluetooth LE module (BL654) with a custom app to update characteristics.<br></p>
  
  <p>The issue is that I am unable to connect to our module without first doing a workaround. I am able to search for devices without issue and our module shows in the search. I would like the app to not have to rely on another app to work properly.</p>
  
  <p>On a clean boot and clean OS install of the phone (Pixel 3XL / Pixel 3 / LG G5), I am unable to connect to our module. I am however able to connect to other BLE devices such as a Raspberry Pi and HTV Vive Base Stations.</p>
  
  <p>The error I receive in the Android Studio Logcat is:<br></p>
  
  <pre><code>D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=C2:A1:E0:B3:69:54
  </code></pre>
  
  <p>After then connecting to a <strong>different</strong> device using nRF Connect I am then able to connect to our module in nRF Connect, and subsequently our module within my app. Nordic makes both the firmware running on the board and nRF Connect. This leads me to believe something special is happening between the two?</p>
  
  <p>I am using the standard Android libraries to make the connection and have also tried using RxAndroidBle.<br></p>
  
  <p>I have researched for close to a month about this error and possible solutions and was unable to resolve it. Any guidance would be appreciated.<br></p>
  
  <p>Here is a <a href="https://drive.google.com/file/d/1zGIgnC592OXOGj8_nVTP4N5AAijkEP-r/view?usp=sharing" rel="nofollow noreferrer">Logcat</a> of a failed connection attempt. Here is a <a href="https://drive.google.com/file/d/1mpb_x4BcSTQ8TFPvUKaPG4-ONc1qhrB8/view?usp=sharing" rel="nofollow noreferrer">Logcat</a> of a successful connection attempt after using the workaround.<br></p>
  
  <p>Below I will show snippets of the relevant code. If more code is required I am happy to share.<br></p>
  
  <p><strong>Code Snippets</strong><br></p>
  
  <p>I have code split between a Kotlin Class file and MainActivity.kt, to my knowledge I am passing all the correct parameters between the two files. I am a beginner to Kotlin, so try to take it easy on me ;)<br></p>
  
  <p>Finding Bluetooth Devices (Inside mBluetoothLEAdapter.kt):</p>
  
  <pre><code>fun findBluetoothDevices(mBluetoothAdapter: BluetoothAdapter?){
          // Get and instance of the Bluetooth Low Energy Scanner
          scanner = mBluetoothAdapter?.bluetoothLeScanner
  
          // Create search settings object
          val mSettings = ScanSettings.Builder().
              setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).
              setReportDelay(reportDelay).
              build()
  
          // Create a scanning filter
          val mFilter = ScanFilter.Builder().setDeviceName("AEsir ADC Test").build()
          val scannerFilter = arrayListOf&lt;ScanFilter&gt;()
          scannerFilter.add(mFilter)
  
          // Stop previous scan if there was one
          stopScanningBluetoothDevices()
  
          //Start new scanner
          tools.showToast("Scanning...")
          scanner?.startScan(null, mSettings, mCallback)
      }
  </code></pre>
  
  <p><br></p>
  
  <p>Scan Callback (Inside mBluetoothLEAdapter.kt):</p>
  
  <pre><code>inner class MCallBack: ScanCallback() {
          override fun onScanResult(callbackType: Int, result: ScanResult?) {
              super.onScanResult(callbackType, result)
              stopScanningBluetoothDevices()
              tools.showToast("Single Result Found!")
          }
  
          override fun onScanFailed(errorCode: Int) {
              super.onScanFailed(errorCode)
              stopScanningBluetoothDevices()
              tools.showToast("Error on Scan!")
              tools.showToast(errorCode.toString())
          }
  
          override fun onBatchScanResults(results: MutableList&lt;ScanResult&gt;?) {
              super.onBatchScanResults(results)
              stopScanningBluetoothDevices()
              tools.showToast("Batch Results Found!")
              scanResults = results
              val mAdapter = DeviceListAdapter(activity, scanResults)
              val deviceList = activity.findViewById&lt;ListView&gt;(R.id.device_list)
              deviceList.adapter = mAdapter
          }
      }
  </code></pre>
  
  <p><br></p>
  
  <p>Connecting to a Device (Inside MainActivity.kt):</p>
  
  <pre><code>// Runs when an item in the Device List is pressed.
      // This initiates a GATT connection to the selected device.
      override fun onListPressed(): AdapterView.OnItemClickListener? {
          return AdapterView.OnItemClickListener { parent, _, position, _ -&gt;
  
              if (bluetoothGatt != null) {
                  bluetoothGatt?.disconnect()
                  bluetoothGatt?.close()
              }
  
              val clickedItem = parent.getItemAtPosition(position) as ScanResult
              //val device = clickedItem.device
              val address = clickedItem.device.address
              tools.showToast("Connecting to: $address")
  
              // CONNECTION NOT WORKING. HAVE TO USE nRF Connect to make it work
              bluetoothGatt = clickedItem.device.connectGatt(applicationContext, false, mGattCallback, BluetoothDevice.TRANSPORT_LE)
          }
      }
  </code></pre>
  
  <p><br></p>
  
  <p>GattCallback (Inside MainActivity.kt):</p>
  
  <pre><code>inner class GattCallback : BluetoothGattCallback() {
          override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
              super.onConnectionStateChange(gatt, status, newState)
              //If we connected to the GATT server find services on device
              if (status == BluetoothGatt.GATT_SUCCESS) {
                  gatt.discoverServices()
              }
              else if (status == BluetoothGatt.STATE_CONNECTING) {
                  tools.showToast("Connecting I am")
              }
              else if (status == BluetoothGatt.STATE_DISCONNECTED) {
                  bluetoothGatt?.close()
                  bluetoothGatt = null
                  tools.showToast("I got here and closed the connection")
              }
          }
  
          override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
              super.onServicesDiscovered(gatt, status)
              bluetoothServices = gatt?.services
          }
  
          override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
              //"Notification"
          }
  
          override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) {
              super.onCharacteristicWrite(gatt, characteristic, status)
  
              //Confirm that the characteristic was actually changed
              tools.showToast("Characteristic was written!")
          }
  
          override fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
  
          }
  
          override fun onDescriptorRead(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
  
          }
      }
  </code></pre>
  
  <p><br></p>
  
  <blockquote>
    <p>Thank you for any help you can offer :)</p>
  </blockquote>