LeandroSQ / android-ble-made-easy

An Android Library for handling Bluetooth Low Energy on Android Easy
MIT License
88 stars 29 forks source link

[Question] How to scan for nearby devices? #29

Closed panguoxian closed 1 year ago

panguoxian commented 1 year ago

I modified the startScan function of BluetoothCentralManager.kt Because the startScan function uses filter, but the filter is empty

private fun startScan(filters: List<ScanFilter>, scanSettings: ScanSettings, scanCallback: ScanCallback) {
    if (bleNotReady()) return
    if (bluetoothScanner == null) {
        bluetoothScanner = bluetoothAdapter.bluetoothLeScanner
    }
    if (bluetoothScanner != null) {
        setScanTimer()
        currentCallback = scanCallback
        currentFilters = filters
       + if (filters.isEmpty()){
       +   bluetoothScanner?.startScan(scanCallback)
       + }else{
            bluetoothScanner?.startScan(filters, scanSettings, scanCallback)
       + }

        Logger.i(TAG, "scan started")
    } else {
        Logger.e(TAG, "starting scan failed")
    }
}
LeandroSQ commented 1 year ago

Hello :) Thank you for your issue.

I am sorry, I think I am confused, are you sure you are not referring to blessed kotlin repo? My library also is based on Kotlin, but has a different approach and philosophy.

panguoxian commented 1 year ago

Different methods and concepts.

I need to scan for unknown bluetooth devices nearby. How do this? i want to know your method.

I use scanForPeripherals(resultCallback: (BluetoothPeripheral, ScanResult) -> Unit, scanError: (ScanFailure) -> Unit ) function. but resultCallback has no callback.

Because the startScan function uses filter, but the filter is empty. So scanForPeripherals has no callback.

LeandroSQ commented 1 year ago

You can take a look on the README.md for this repo to see how you can install. You can handle Bluetooth permissions, turning on the Bluetooth hardware, yourself or you can let my library help… I suggest you use the library functions to handle these, they are also on the readme file.

As for how you can scan for all nearby devices you can check this. You don't have to pass filters if you don't want to, you can choose to scan for a given period or indeterminate. And you can choose to use a callback or use coroutines.

The sample app provided in this repo does the scanning for all nearby devices, you can check that for guidance.

panguoxian commented 1 year ago

Thank you for your reply

LeandroSQ commented 1 year ago

Hey, just checking Were you able to scan devices successfully?

LeandroSQ commented 1 year ago

I am closing this due to inactivity. Please let me know if you would like to re-open this issue.