如何在安卓中等待DEVICE_DISCOVERY_FINISHED直到返回结果。

huangapple 未分类评论44阅读模式
英文:

How to hold return until DEVICE_DISCOVERY_FINISHED in adnroid

问题

以下是您要翻译的内容:

"Hi i am quite new to android development and using some native code in flutter. So i have called this function to scan bluetooth devices in MainActivity.kt.

private fun scanBluetooth(): Set<String> {
var foundDevices = mutableSetOf<String>()
var isFinished = false

val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

bluetoothAdapter?.startDiscovery()

var filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)

val receiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action: String = intent.action
        when(action) {
            BluetoothDevice.ACTION_FOUND -&gt; {
                val device: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                val deviceName = device.name
                val deviceHardwareAddress = device.address // MAC address
                val rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE)
                foundDevices.add(&quot;${deviceHardwareAddress} ${rssi}&quot;)
            }
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED -&gt; {
                isFinished = true;
            }
        }
    }
}
registerReceiver(receiver, filter)

return foundDevices

}

So i want to return foundDevices only when BluetoothAdapter.ACTION_DISCOVERY_FINISHED is finished. Can you suggest a way how to achieve this?

android
I have tried something like this but getting errors.

return when (isFinished) {
true -> {
unregisterReceiver(receiver)
return foundDevices
}
}"

请注意,我只翻译了您的请求部分,不包括代码部分。如果您有任何其他问题,请随时提出。

英文:

Hi i am quite new to android development and using some native code in flutter. So i have called this function to scan bluetooth devices in MainActivity.kt.

    private fun scanBluetooth(): Set&lt;String&gt; {
        var foundDevices = mutableSetOf&lt;String&gt;()
        var isFinished = false

        val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

        bluetoothAdapter?.startDiscovery()

        var filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)

        val receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val action: String = intent.action
                when(action) {
                    BluetoothDevice.ACTION_FOUND -&gt; {
                        val device: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                        val deviceName = device.name
                        val deviceHardwareAddress = device.address // MAC address
                        val rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE)
                        foundDevices.add(&quot;${deviceHardwareAddress} ${rssi}&quot;)
                    }
                    BluetoothAdapter.ACTION_DISCOVERY_FINISHED -&gt; {
                        isFinished = true;
                    }
                }
            }
        }
        registerReceiver(receiver, filter)
        
        return foundDevices
    }

So i want to return foundDevices only when BluetoothAdapter.ACTION_DISCOVERY_FINISHED is finished. Can you suggest a way how to achieve this?

android
I have tried something like this but getting errors.

    return when (isFinished) {
        true -&gt; {
            unregisterReceiver(receiver)
            return foundDevices
        }
    }

huangapple
  • 本文由 发表于 2020年4月10日 15:01:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61135457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定