Android11无线调试匹配和持久连接
创始人
2024-09-30 17:00:16
0

Android 11 引入了一种新的无线调试模式,在没有连接 USB 的情况下进行配对和调试。下面是一些代码示例,让您能够实现 Android 11 的无线调试配对和持久连接。

  1. 添加以下权限到 AndroidManifest.xml:


  1. 添加以下代码到 AndroidManifest.xml:

    
        
    

  1. 在 MainActivity 中获取本地蓝牙适配器并启用它:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.enable();
  1. 创建 BluetoothDevicePicker:
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
registerReceiver(devicePairReceiver, filter);
Intent intent = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
startActivityForResult(intent, DEVICE_PICKER_REQUEST_CODE);
  1. 获取设备地址并进行配对:
if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)) {
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);
    devicePairHelper.pair(device);
}
  1. 创建 BluetoothDeviceHelper:
public class BluetoothDeviceHelper {

    private final BluetoothAdapter bluetoothAdapter;
    private final Context context;

    public BluetoothDeviceHelper(Context context) {
        this.context = context;
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public void connect(String address, BluetoothProfile.ServiceListener listener) {
        if (bluetoothAdapter.isEnabled()) {
            BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
            bluetoothAdapter.getProfileProxy(context, listener, BluetoothProfile.GATT);
        } else {
            Log.e(TAG, "Bluetooth is not enabled.");
        }
    }

    public void disconnect(BluetoothProfile proxy, BluetoothProfile.ServiceListener listener) {
        bluetoothAdapter.close

相关内容

热门资讯

截至目前!朋朋政和软件辅助器!... 截至目前!朋朋政和软件辅助器!一直有挂辅助器(真是有挂)-哔哩哔哩所有人都在同一条线上,像星星一样排...
相较于以往!手机透视辅助器ap... 相较于以往!手机透视辅助器app!确实存在有辅助神器(有挂助手)-哔哩哔哩1、上手简单,内置详细流程...
备受关注的!同城乐吧乐享版51... 备受关注的!同城乐吧乐享版510k辅助!竟然是真的辅助下载(有挂分析)-哔哩哔哩1、同城乐吧乐享版5...
目前!乐乐休闲游戏辅助!一贯真... 目前!乐乐休闲游戏辅助!一贯真的是有辅助工具(真实有挂)-哔哩哔哩1、首先打开乐乐休闲游戏辅助辅助器...
来临!微信多乐小程序游戏破解器... 来临!微信多乐小程序游戏破解器!一贯是有辅助平台(确实有挂)-哔哩哔哩1、超多福利:超高返利,海量正...
近年来!闲逸有没有辅助软件功能... 近年来!闲逸有没有辅助软件功能!好像存在有辅助神器(有挂教程)-哔哩哔哩该软件可以轻松地帮助玩家将闲...
截至目前!博雅红河西元!一直真... 截至目前!博雅红河西元!一直真的有辅助神器(确实有挂)-哔哩哔哩1、在博雅红河西元插件功能辅助器技巧...
长期以来!约战丹东游戏辅助器!... 长期以来!约战丹东游戏辅助器!一贯真的有辅助平台(有挂详情)-哔哩哔哩1、许多玩家不知道约战丹东游戏...
在玩家背景下!人海大厅脚本!竟... 在玩家背景下!人海大厅脚本!竟然有挂辅助安装(有挂分享)-哔哩哔哩1、首先打开人海大厅脚本辅助器下载...
据了解!宁德钓螃蟹有没有挂!一... 据了解!宁德钓螃蟹有没有挂!一贯是有辅助下载(有挂讲解)-哔哩哔哩所有人都在同一条线上,像星星一样排...