Android蓝牙配对弹出框过程分析
根据远程蓝牙设备(remote devices)的要求,手机端发起与远程蓝牙设备Bluetooth remote Device的配对有两种情况
第一种:配对时需要pin码(pin request event)即有配对请求pairing request :所对应的action为 : BluetoothDevice.ACTION_PAIRING_REQUEST。
根据远程设备所携带的type信息判断是否可以获取到pairingkey
type值通过BluetoothDevice.EXTRA_PAIRING_VARIANT获取到
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR);
pairingkey通过BluetoothDevice.EXTRA_PAIRING_KEY获取到
int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,
BluetoothDevice.ERROR);
如果type(int型数值)属于以下3种类型:
BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION(数值为2,远程设备为手机/scp860时为该类型),需要用户确认
BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY(数值为4),以前的一种配对方式,用在蓝牙2.1配对过程中,需要在本机(local device)输入显示在远程设备上的秘钥 passkey: enter the passkey displayed on remote device
BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN(数值为5),蓝牙2.0配对过程中,在本机输入显示在远程设备上的pin码:
enter the PIN displayed on remote device
则表示远程设备(remote device)本身携带有配对码pairingkey,可以通过BluetoothDevice.EXTRA_PAIRING_KEY获取到配对码
如果type不属于以上3种类型,则表示远程设备不会携带pairingkey配对码,必须要用户自己手动输入:The user will be prompted to enter a pin or an app will enter a pin for user
第二种:不需要pin码(create bond)没有配对请求(对应的设备有scp960,蓝牙耳机等),此时远程设备不需要鉴权,可以直接使用(just works)
所对应的action为:BluetoothDevice.ACTION_BOND_STATE_CHANGED
监听action的类为BluetoothPairingRequest.java,配对弹窗为BluetoothPairingDialog.java。字段信息存在于BluetoothDevice.java中
总结来说如果不以action为区分的话,配对分为三种情况
直接配对连接不需要鉴权(just works):这种情况相当于手机发起配对时不需要请求远程设备,即不需要远程设备的认可
passkey/pin enter需要一方输入型:这种情况配对时会请求远程设备,然后远程设备会提供一个passk/pin码但不会通过代码的形式通知给用户,用户需要去查看远程设备显示的配对码并输入该pairingkey配对码(属于蓝牙2.0和2.1时的处理)
passkey/pin confirmmation无需输入密码只需确认型:这种情况配对时也会请求远程设备,远程会提供一个配对码并且通知给用户,此时代码中可以获取到pairingkey配对码,严格来说是用户需要查看远程设备上显示的配对码与当前手机上显示的配对码是否一致,进行比较(compare)确认后配对,所以属于确认型,对应于上述type的第一种类型
————————————————
版权声明:本文为CSDN博主「fanfan-公众号-码农修仙儿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zrf1335348191/article/details/54020225