Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 查看Kotlin中的蓝牙设备列表?_Android_Kotlin_Bluetooth - Fatal编程技术网

Android 查看Kotlin中的蓝牙设备列表?

Android 查看Kotlin中的蓝牙设备列表?,android,kotlin,bluetooth,Android,Kotlin,Bluetooth,我在科特林有一个小型蓝牙项目。其代码如下: var mArrayAdapter: ArrayAdapter<String>? = null val bluetoothAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter() var myList: MutableList<String> = mutableListOf<String>() var devices = ArrayList

我在科特林有一个小型蓝牙项目。其代码如下:

   var mArrayAdapter: ArrayAdapter<String>? = null
val bluetoothAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
var myList: MutableList<String> = mutableListOf<String>()
var devices = ArrayList<BluetoothDevice>()
var devicesMap = HashMap<String, BluetoothDevice>()

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private val REQUEST_ENABLE_BT = 1000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<View>(R.id.btn_scan).setOnClickListener(this)
        checkBluetoothStatus()
        val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
        registerReceiver(receiver, filter)
        mArrayAdapter = ArrayAdapter(this, R.layout.dialog_select_device)



    }


    private val receiver = object : BroadcastReceiver() {

        override fun onReceive(context: Context, intent: Intent) {
            val action: String? = intent.action
            when(action) {
                BluetoothDevice.ACTION_FOUND -> {

                    val device: BluetoothDevice =
                        intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                    val deviceName = device.name
                     myList.add(deviceName)
                }
            }
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_scan ->
                startScan()
        }
    }


    fun startScan() {
        if (BluetoothAdapter.getDefaultAdapter().startDiscovery()) {

            val myToast = Toast.makeText(this, myList.toString(), Toast.LENGTH_SHORT)

            myToast.show()
        }
    }

}
var mArrayAdapter:ArrayAdapter?=无效的
val bluetoothAdapter:bluetoothAdapter=bluetoothAdapter.getDefaultAdapter()
var myList:MutableList=mutableListOf()
var devices=ArrayList()
var devicesMap=HashMap()
类MainActivity:AppCompatActivity(),View.OnClickListener{
专用val请求\启用\ BT=1000
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.btn\u扫描).setOnClickListener(此)
checkBluetoothStatus()
val filter=IntentFilter(找到BluetoothDevice.ACTION\u)
寄存器接收器(接收器、过滤器)
mArrayAdapter=ArrayAdapter(此,R.layout.dialog\u选择设备)
}
private val receiver=对象:BroadcastReceiver(){
覆盖接收(上下文:上下文,意图:意图){
val操作:字符串?=intent.action
何时(行动){
BluetoothDevice.ACTION\u已找到->{
val设备:蓝牙设备=
intent.getParcelableExtra(BluetoothDevice.EXTRA_设备)
val deviceName=device.name
myList.add(deviceName)
}
}
}
}
覆盖有趣的onClick(v:视图?){
何时(v?.id){
R.id.btn\U扫描->
startScan()
}
}
乐趣之星{
if(BluetoothAdapter.getDefaultAdapter().startDiscovery()){
val myToast=Toast.makeText(this,myList.toString(),Toast.LENGTH\u SHORT)
myToast.show()
}
}
}
当用户单击按钮时,我希望能够看到其他蓝牙设备的列表(我认为onReceive方法可以让我这样做)。因此,我将设备名称添加到myList变量中,然后在toast中显示。但在他们点击的那一刻,什么也没有出现。谢谢你的建议