蓝牙讲解上--搜索、配对

本文详细介绍了蓝牙连接的过程,包括获取权限、初始化、搜索已配对和未配对设备、设置数据适配器、进行设备配对以及准备开始聊天操作。通过注册广播监听蓝牙状态和发现设备,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

权限

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

初始化

// 获取蓝牙适配对象
        defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        // 打开蓝牙设备
        defaultAdapter.enable();

搜索已配对设备

        Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
        for (BluetoothDevice bluetoothDevice : bondedDevices) {
            if (!deviceList.contains(bluetoothDevice))
                //添加到集合中
                deviceList.add(bluetoothDevice);
        }

搜索未配对设备

  • 在一开始注册发现蓝牙的广播
    • 注册广播 ,有两个action,一个是发现蓝牙,一个是蓝牙状态改变的广播
 // 定义一个广播接收器
        receiver = new MyBluetoothReceiver();
        // 创建一个意图过滤器
        IntentFilter filter = new IntentFilter();
        // 注册一个搜素到蓝牙的一个意图action
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        // 添加一个action 监听配对状态改变的一个事件
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(receiver, filter);
  • 广播接收器代码
// 接收所注册过的action的消息
    class MyBluetoothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取事件类型
            String action = intent.getAction();
            // 获取蓝牙设备
            BluetoothDevice bluetoothDevice = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                // 添加到一开始定义的集合中
                if (!deviceList.contains(bluetoothDevice)) {
                    deviceList.add(bluetoothDevice);
                }
                // 刷新数据适配器
                setAdapter();
            } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
                int bondState = bluetoothDevice.getBondState();
                switch (bondState) {
                case BluetoothDevice.BOND_NONE:
                    Toast.makeText(MainActivity.this, "配对失败", 0).show();
                    break;
                case BluetoothDevice.BOND_BONDING:
                    Toast.makeText(MainActivity.this, "正在配对", 0).show();
                    break;
                case BluetoothDevice.BOND_BONDED:
                    Toast.makeText(MainActivity.this, "配对成功", 0).show();
                    // 在集合中移除
                    deviceList.remove(bluetoothDevice);
                    // 将这个条目重新添加到集合中
                    deviceList.add(0, bluetoothDevice);
                    // 设置数据适配器
                    setAdapter();
                    break;
                default:
                    break;
                }
            }
        }
    }
  • 开始搜索
    new Thread() {
            public void run() {
                // 如果当前正在搜素,先停止,开始本次搜索
                if (defaultAdapter.isDiscovering()) {
                    defaultAdapter.cancelDiscovery();
                }
                // 开始搜索,就可以搜索到未配对的设备
                defaultAdapter.startDiscovery();
            };
        }.start();

设置数据适配器

// 如果适配器为空,创建,设置适配器
        if (myBluetoothAdapter == null) {
            myBluetoothAdapter = new MyBluetoothAdapter(this, deviceList);
            listView.setAdapter(myBluetoothAdapter);
        } else {
            // 刷新适配器
            myBluetoothAdapter.notifyDataSetChanged();
        }

对未配对设备进行配对

try {
                    // 获取点击的条目
                    BluetoothDevice bluetoothDevice = deviceList.get(position);
                    // 未配对,就去配对
                    if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
                        String address = bluetoothDevice.getAddress();
                        // 获取远程设备
                        BluetoothDevice remoteDevice = defaultAdapter
                                .getRemoteDevice(address);
                        // 配对操作
                        // 先获取字节码文件对象
                        Class<BluetoothDevice> clz = BluetoothDevice.class;
                        // 获取方法,这个方法是隐藏的,调不到,随时用反射
                        Method method = clz.getMethod("createBond", null);
                        // 执行配对该方法
                        method.invoke(remoteDevice, null);
                        // 如果当前条目的状态,是已经配好对的,就开始聊天
                    } 

对配对设备准备开始聊天操作

 else if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
                        // 跳转界面,开始聊天
        Intent intent = new Intent(MainActivity.this,MyChatActivity.class);
        intent.putExtra("address", bluetoothDevice.getAddress());
        startActivity(intent);
                    }

代码见

http://download.youkuaiyun.com/detail/zhiyuan0932/9498223

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值