【Android -- 蓝牙】蓝牙配对和蓝牙连接

一、蓝牙配对

在这里插入图片描述
搜索到蓝牙设备后,将设备信息填充到listview中,点击listiew则请求配对

蓝牙配对有点击配对和自动配对,点击配对就是我们选择设备两个手机弹出配对确认框,点击确认后配对

自动配对就是搜索到蓝牙设备后自动配对不需要输入pin码,但在基本开发中都不采用这种方式,所以这里说的是第一种配对方式

点击配对,调用

BluetoothDevice.class.getMethod

进行配对,代码如下:

Method method = BluetoothDevice.class.getMethod("createBond");
Log.e(getPackageName(), "开始配对");
method.invoke(listdevice.get(position));

同样的,如果我们想要配对的设备取消配对

只需要将 creatBond 改为 removeBond

二、蓝牙连接

配对成功之后,就可以进行蓝牙连接了,蓝牙连接操作比较耗时,可以在一个线程中进行:

1. 调用自己定义的

connect(listdevice.get(position));

同样传递的参数也是设备device

首先声明蓝牙套接字:

private BluetoothSocket mBluetoothSocket;
...
mBluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(BltContant.SPP_UUID);

BltContant.SPP_UUID 是一个 UUID 常量,至于 UUID 是什么,大家可以自行百度,因为详细的文章已经很多了。

连接的时候要先判断蓝牙是否在扫描,如果在扫描就停止扫描,并且没有连接才进行连接,代码如下:

if (bluetoothadapter.isDiscovering()) {
    bluetoothadapter.cancelDiscovery();
}
if (!mBluetoothSocket.isConnected()) {
    mBluetoothSocket.connect();
}

当连接成功时,我们要让被连接的那部手机也自动跳转到聊天页面,所以我们要开启蓝牙服务端等待设备的连接,当设备连接时,自动跳转页面,蓝牙服务端代码如下:

/**
 * 开启服务端
 */
public void startBluService() {
 
    while (true) {
        try {
            if (getBluetoothServerSocket() == null){
                Log.e("在这里获取的为空","在这里获取的为空");
            }
            bluetoothSocket = getBluetoothServerSocket().accept();
            if (bluetoothSocket != null) {
                APP.bluetoothSocket = bluetoothSocket;
                EventBus.getDefault().post(new BluRxBean(SERVER_ACCEPT, bluetoothSocket.getRemoteDevice()));
                //如果你的蓝牙设备只是一对一的连接,则执行以下代码
                getBluetoothServerSocket().close();
                //如果你的蓝牙设备是一对多的,则应该调用break;跳出循环
                //break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Android 中,断开连接和取消配对的广播分别是 ACTION_ACL_DISCONNECTED 和 ACTION_BOND_STATE_CHANGED。 ACTION_ACL_DISCONNECTED 广播在 BluetoothAdapter 中定义,当蓝牙连接断开时发送。您可以使用以下代码注册和接收此广播: ``` private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) { // 处理断开连接的逻辑 } } }; IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_ACL_DISCONNECTED); registerReceiver(mReceiver, filter); ``` ACTION_BOND_STATE_CHANGED 广播在 BluetoothDevice 中定义,当蓝牙设备配对状态更改时发送。您可以使用以下代码注册和接收此广播: ``` private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 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); int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); if (bondState == BluetoothDevice.BOND_NONE && previousBondState == BluetoothDevice.BOND_BONDED) { // 处理取消配对的逻辑 } } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(mReceiver, filter); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值