This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF BLE Android - My BleScannerCompat doesn't detect a device, but nrf connect does

Hi,
I'm having this trouble since some weeks ago and I was looking for an answer in documentation and was impossible to find it.

I have a device with Nordic ble chip and I'm trying to find it trough android scanner's library (BluetoothLeScannerCompat.getScanner()) and is impossible to see it. But when I turn on the nrf connect app or bluetooth settings from phone settings, I see this device and, later, it appears in my app's scanner. Of course this is not a good solution to this problem, because I can't say to every user to do this to discover this devices.

Scanner settings:


val settings: ScanSettings = ScanSettings.Builder()
.setLegacy(false)
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(500)
.setUseHardwareBatchingIfSupported(true)
.build()


I don't know what to do to solve this. Any idea?

Thanks.
Parents
  • Hello,

    But when I turn on the nrf connect app or bluetooth settings from phone settings, I see this device and, later, it appears in my app's scanner.

    This leads me to believe that you are not enabling the BLE adapter in your application, or something similar.
    From this description I am thinking that you are using the BLE adapter correctly, but not starting it properly. This way, if it is properly started (such as through the nRF Connect application for smartphone, or through the Bluetooth settings directly), then everything would work fine. Could this be the case?

    Could you possibly show me the rest of your scanner initialization and start code as well?
    Please use Insert -> Code when sharing code on DevZone.

    Best regards,
    Karl

  • Hi,

    thanks for the answer.

    This is the code:

    private var scanner = BluetoothLeScannerCompat.getScanner()
    
    fun scan(mac: String?, listener: ScanListener) {
        scanner.stopScan(mScannerCallback)
        val settings: ScanSettings = ScanSettings.Builder()
            .setLegacy(false)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setReportDelay(500)
            .setUseHardwareBatchingIfSupported(true)
            .build()
    
        scanner.startScan(arrayListOf(), settings, scannerCallback(mac, listener))
    }

    This is the mScannerCallback:

    private fun scannerCallback(mac: String?, listener: ScanListener?): ScanCallback {
        mScannerCallback = object : ScanCallback() {
            override fun onScanFailed(errorCode: Int) {
                super.onScanFailed(errorCode)
                Log.d(TAG, "onScanFailed $errorCode")
            }
    
            override fun onBatchScanResults(results: MutableList<ScanResult>) {
                super.onBatchScanResults(results)
                for(item in results) {
                    item.device.name?.let {
                        Log.d(TAG, "device name ${item.device.name}")
                        if(item.device.name != null && item.device.name.toUpperCase(Locale.getDefault()).contains("____")) {
                            if(isEmpty(mac) || mac == item.device.address) {
                                listener?.newDevice(item)
                            }
                        }
                    }
                }
            }
        }
        return mScannerCallback
    }

    How can I enable BLE adapter in my app? I was using nordic chip in other devices but I never had this problem.

Reply
  • Hi,

    thanks for the answer.

    This is the code:

    private var scanner = BluetoothLeScannerCompat.getScanner()
    
    fun scan(mac: String?, listener: ScanListener) {
        scanner.stopScan(mScannerCallback)
        val settings: ScanSettings = ScanSettings.Builder()
            .setLegacy(false)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setReportDelay(500)
            .setUseHardwareBatchingIfSupported(true)
            .build()
    
        scanner.startScan(arrayListOf(), settings, scannerCallback(mac, listener))
    }

    This is the mScannerCallback:

    private fun scannerCallback(mac: String?, listener: ScanListener?): ScanCallback {
        mScannerCallback = object : ScanCallback() {
            override fun onScanFailed(errorCode: Int) {
                super.onScanFailed(errorCode)
                Log.d(TAG, "onScanFailed $errorCode")
            }
    
            override fun onBatchScanResults(results: MutableList<ScanResult>) {
                super.onBatchScanResults(results)
                for(item in results) {
                    item.device.name?.let {
                        Log.d(TAG, "device name ${item.device.name}")
                        if(item.device.name != null && item.device.name.toUpperCase(Locale.getDefault()).contains("____")) {
                            if(isEmpty(mac) || mac == item.device.address) {
                                listener?.newDevice(item)
                            }
                        }
                    }
                }
            }
        }
        return mScannerCallback
    }

    How can I enable BLE adapter in my app? I was using nordic chip in other devices but I never had this problem.

Children
  • Hello again,

    Thank you for your patience with this. The summer holidays have begun here in Norway, and DevZone is therefore operating with reduced staff for the time being. Sorry for any inconvenience this might cause.

    joel.aa said:
    thanks for the answer.

    No problem at all, I am happy to help!

    joel.aa said:
    This is the code:

    Thank you for sharing the code.
    Briefly looking through it I do not see anything immediately out of the ordinary. What changes have you made to your AndroidManifest?
    Please see the answer and project uploaded by my colleague Aleksander in this ticket, he is one of the developers of the module. I have not tested this project yet myself either, but looking over the code it looks to be a good match for the functionality you describe (apart from the filtering).
    I have sent Aleksander a message, asking for an update on this ticket - if he has had time to review the project, but he is currently on vacation and will not return for a couple of weeks yet.

    joel.aa said:
    How can I enable BLE adapter in my app? I was using nordic chip in other devices but I never had this problem.

    I am not sure I understand what you mean by this - is the issue not that your smartphone application does not start scanning on its own?
    Did you use this exact code on another device, which had a Nordic chip onboard, with a different result than the behavior you are seeing now?

    Best regards,
    Karl

Related