蓝牙|标准蓝牙配对方式

蓝牙:BlueTooth,是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换,蓝牙又分为传统/标准蓝牙和BLE蓝牙。

在了解配对方式前,先了解设备的IOCapacity,IOCapcaity是由设备InputCapacity和OutputCapacity组合而成,表示的是设备的输入输出的能力,InputCapacity和OutputCapacity具体如下:
这里写图片描述
根据上述的InputCapacity和OutputCapacity,组合的IOCapacity如下:
这里写图片描述
发起者和响应者各自的IOCapacity,具体表现的配对模式如下:
这里写图片描述
上述的配对模式,具体解析如下:
安全简易配对SSP(Secure simple pairing),蓝牙2.0之后配对方式,简易安全配对一共有四种,其中Out of Band很少使用到,具体如下:

  • Numeric Comparison

配对双方都显示一个6位的数字,由用户来核对数字是否一致,并输入Yes/No,两端Yes表示一致即可配对,可以防止中间人攻击。
使用场景:两端设备可以弹出6位十进制数,并且有yes和no按钮。

  • Passkey Entry

配对目标输入一个在本地设备上显示的6位数字,输入正确即可配对,并可以防止中间人攻击。
使用场景:一端设备可以显示,另一端设备可以输入。

  • Just Works

不会进行鉴权,不能防止中间人攻击用于配对没有显示没有输入的设备,主动发起连接即可配对,用户看不到配对过程,不可以防止中间人攻击,例如连接蓝牙耳机。
使用场景:用于即不能显示6位随机数,也不能输入的设备。

  • Out of Band

两设备的通过别的途径交换配对信息,例如一些NFC蓝牙音箱。

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,使用 Bluetooth API 进行蓝牙配对的流程如下: 1. 打开蓝牙 首先需要检查设备是否支持蓝牙,如果支持则需要打开蓝牙。 ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 } else { if (!bluetoothAdapter.isEnabled()) { // 请求打开蓝牙 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } ``` 2. 搜索蓝牙设备 使用 `BluetoothAdapter.startDiscovery()` 方法搜索附近的蓝牙设备,并在搜索到设备时进行处理。在搜索到设备时,可以通过 `BluetoothDevice.createBond()` 发起配对请求。 ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.startDiscovery(); private final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { // 设备未配对,发起配对请求 device.createBond(); } } } }; // 注册设备搜索广播接收器 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(deviceFoundReceiver, filter); ``` 3. 输入配对码 在配对请求发起后,蓝牙设备会弹出配对码输入框,需要在输入框中输入配对码。可以使用 `BluetoothDevice.setPin(byte[] pin)` 方法设置配对码,也可以使用反射机制获取配对码输入框并模拟输入。 ``` private void inputPin(BluetoothDevice device, String pin) { try { Method m = device.getClass().getMethod("setPin", byte[].class); m.invoke(device, pin.getBytes()); } catch (Exception e) { e.printStackTrace(); } } private void simulateInputPin() { try { BluetoothDevice device = ...; // 获取需要输入配对码的设备 Method m = device.getClass().getMethod("setPairingConfirmation", boolean.class); m.invoke(device, true); m = device.getClass().getMethod("cancelPairingUserInput"); m.invoke(device); } catch (Exception e) { e.printStackTrace(); } } ``` 注意:使用反射机制可能会因为厂商定制的不同而不适用于某些设备。 4. 处理配对结果 在输入配对码后,需要等待配对结果。可以注册广播接收器监听配对结果,例如: ``` private final BroadcastReceiver bondStateChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); if (bondState == BluetoothDevice.BOND_BONDED) { // 配对成功 } else if (bondState == BluetoothDevice.BOND_NONE) { // 配对失败 } } } }; // 注册配对状态改变广播接收器 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(bondStateChangedReceiver, filter); ``` 完整代码示例: ``` private final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { // 设备未配对,发起配对请求 device.createBond(); } } } }; private final BroadcastReceiver bondStateChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); if (bondState == BluetoothDevice.BOND_BONDED) { // 配对成功 } else if (bondState == BluetoothDevice.BOND_NONE) { // 配对失败 } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 } else { if (!bluetoothAdapter.isEnabled()) { // 请求打开蓝牙 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } // 搜索蓝牙设备 bluetoothAdapter.startDiscovery(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(deviceFoundReceiver, filter); // 注册配对状态改变广播接收器 filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(bondStateChangedReceiver, filter); } } private void inputPin(BluetoothDevice device, String pin) { try { Method m = device.getClass().getMethod("setPin", byte[].class); m.invoke(device, pin.getBytes()); } catch (Exception e) { e.printStackTrace(); } } private void simulateInputPin() { try { BluetoothDevice device = ...; // 获取需要输入配对码的设备 Method m = device.getClass().getMethod("setPairingConfirmation", boolean.class); m.invoke(device, true); m = device.getClass().getMethod("cancelPairingUserInput"); m.invoke(device); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(deviceFoundReceiver); unregisterReceiver(bondStateChangedReceiver); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值