Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List BLE devices Android - Kotlin

I'm trying to list all BLE devices on an Android device, using Kotlin (the Java-version don't work either) but I don't get any devices or any call back at all, except for a "scan was already started" I have the correct uses-permission in the manifest.

Here is the current minimum of code, I'm trying with. But even the sample code from Google is listing any devices. I'm running on a Pixel with Android version 8.1.0. I have it working on iOS, with the basic BLE device list (Swift)!

 private val bleScanner = object :ScanCallback() {
    override fun onScanResult(callbackType: Int, result: ScanResult?) {
        super.onScanResult(callbackType, result)
        Log.d("DeviceListActivity","onScanResult: ${result?.device?.address} - ${result?.device?.name}")
    }

    override fun onBatchScanResults(results: MutableList<ScanResult>?) {
        super.onBatchScanResults(results)
        Log.d("DeviceListActivity","onBatchScanResults:${results.toString()}")
    }

    override fun onScanFailed(errorCode: Int) {
        super.onScanFailed(errorCode)
        Log.d("DeviceListActivity", "onScanFailed: $errorCode")
    }

}

private val bluetoothLeScanner: BluetoothLeScanner
    get() {
        val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        val bluetoothAdapter = bluetoothManager.adapter
        return bluetoothAdapter.bluetoothLeScanner
    }

class ListDevicesAdapter(context: Context?, resource: Int) : ArrayAdapter<String>(context, resource)

override fun onCreate(savedInstanceState: Bundle?) {
    Log.d("DeviceListActivity", "onCreate()")
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_device_list)


}

override fun onStart() {
    Log.d("DeviceListActivity","onStart()")
    super.onStart()

    bluetoothLeScanner.startScan(bleScanner)

}

override fun onStop() {
    bluetoothLeScanner.stopScan(bleScanner)
    super.onStop()
}
like image 898
user3699321 Avatar asked Jun 18 '18 13:06

user3699321


1 Answers

You need to declare permission:

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

and you for devices with android 6.0 + request manually this permission:

  override fun onStart() {
        Log.d("ScanDeviceActivity", "onStart()")
        super.onStart()
        when (PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            PackageManager.PERMISSION_GRANTED -> bluetoothLeScanner.startScan(bleScanner)
            else -> requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), 1)
        }
    }


   override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when (requestCode) {
            1 -> when (grantResults) {
                intArrayOf(PackageManager.PERMISSION_GRANTED) -> {
                    Log.d("ScanDevices", "onRequestPermissionsResult(PERMISSION_GRANTED)")
                    bluetoothLeScanner.startScan(bleScanner)
                }
                else -> {
                    Log.d("ScanDevices", "onRequestPermissionsResult(not PERMISSION_GRANTED)")
                }
            }
            else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }
like image 137
Jorgesys Avatar answered Nov 11 '22 19:11

Jorgesys