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.
  • 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.

  • 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

  • I have similar problem.  I am able to detect it in using nrf connect app, but not in nrf Blinky app.  Though in my case, the nrf Blinky app is still not able to detect it even after nrf connect app has detected it.  In my case, my nrf device was far away and the rssi was very low.  There is a function isNoise() in ScannerViewModel.java that would filter out low rssi device.

    Perhaps not the kind of problem you have...Just pointing out what I found.

  • Thank you for sharing what you have found, @kvc!

    It could indeed be that the device was filtered out due to low RSSI, but I still suspect that Joel's issue might have been with the enabling/usage of the BLE adapter - since they were able to find the device with both the native BLE scan through 'settings' and the nRF Connect application (after which it was also discovered in their own application).

    If this is still an issue for you I recommend that you open a separate ticket for this issue, to receive help debugging it! :) 

    Best regards,
    Karl

Related