본문 바로가기
Android

블루투스 스캔할때 name, address, rssi 받아오기

by kkong93 2022. 10. 26.
반응형
private val mBluetoothStateReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            val action = intent!!.action //입력된 action
            val intents: Intent = intent
            val device = intents.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)

                Dlog.d( "action : $action")

            //입력된 action에 따라서 함수를 처리한다
            when (action) {
                BluetoothAdapter.ACTION_STATE_CHANGED -> {
                    val state =
                        intents.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
                    when (state) {
                        BluetoothAdapter.STATE_OFF -> {
                        }
                        BluetoothAdapter.STATE_TURNING_OFF -> {
                        }
                        BluetoothAdapter.STATE_ON -> {
                        }
                        BluetoothAdapter.STATE_TURNING_ON -> {
                        }
                    }
                }
                BluetoothDevice.ACTION_ACL_CONNECTED -> {
                    isConnected = true
                    //mBluetoothAdapter!!.cancelDiscovery()
                    isScanning = false
                    Dlog.d( "onReceive: action_acl_connected")
                }
                BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
                }
                BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
                    isConnected = false
                    isScanning = false
                }
                BluetoothAdapter.ACTION_DISCOVERY_STARTED -> {
                    BluetoothListener!!.startScan()
                    isScanning = true
                }
                BluetoothDevice.ACTION_FOUND -> {
//                    if (!isConnected) {
                    val device_name = device!!.name
                    val device_Address = device.address
                    val rssi =
                        intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE).toInt()
                    if (device_name != null && device_name.length > 0) {

                            Dlog.d( "====device_name : $device_name")
                            Dlog.d( "====device_Address : $device_Address")
                            Dlog.d( "====device_RSSI : $rssi")

                        if (device_name.contains("@@@@@") || device_name.contains("@@@@") /*&& rssi > -80*/) {
                            bluetooth_device.add(device)
                            BluetoothListener!!.findDevice(bluetooth_device)

                            //showBluetoothDeviceList()
                        }
                    }
                    //                    } else {
//                        stopScan()
//                    }
                }

                BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
                    //블루투스 기기 검색 종료
                    BluetoothListener!!.scanFinished()
                    isScanning = false
                }
                BluetoothDevice.ACTION_PAIRING_REQUEST -> {
                }
            }
        }
    }
반응형

댓글