BLE连接失败:由于不可接受的BD_ADDR,连接被拒绝。
创始人
2024-12-22 14:00:34
0

这个错误通常表示由于不可接受的BD_ADDR(蓝牙设备地址),导致BLE连接被拒绝。以下是一个示例代码,演示如何解决这个问题:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothManager;
import android.content.Context;

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothGatt bluetoothGatt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化BluetoothAdapter
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();

        // 检查设备是否支持蓝牙
        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
            // 蓝牙未启用,或设备不支持蓝牙
            return;
        }

        // 发起BLE连接
        BluetoothDevice device = bluetoothAdapter.getRemoteDevice("00:11:22:33:AA:BB"); // 替换为你的设备地址
        bluetoothGatt = device.connectGatt(this, false, gattCallback);
    }

    // BluetoothGatt的回调方法
    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (newState == BluetoothGatt.STATE_CONNECTED) {
                    // 成功连接设备
                    // 进行后续操作,例如发现服务和特征值
                }
            } else {
                // 连接失败,处理错误
                if (status == 133) {
                    // 由于不可接受的BD_ADDR,连接被拒绝
                    // 在这里进行错误处理
                }
            }
        }
    };
}

在上面的示例代码中,我们通过bluetoothAdapter.getRemoteDevice("00:11:22:33:AA:BB")获取目标设备的BluetoothDevice对象,然后使用connectGatt()方法发起BLE连接。在BluetoothGattCallbackonConnectionStateChange回调方法中,我们可以处理连接成功和连接失败的逻辑。

当连接失败时,我们检查错误码是否为133,如果是则表示由于不可接受的BD_ADDR导致连接被拒绝。在这里,你可以根据实际需求进行错误处理。

相关内容

热门资讯

热点讨论!蜜瓜大厅可以装挂吗,... 热点讨论!蜜瓜大厅可以装挂吗,约战沙城攻略,通报开挂辅助教程(有挂详细);无需打开直接搜索打开薇:1...
安装程序教程!衡阳丫丫辅助,闲... 安装程序教程!衡阳丫丫辅助,闲玩暗宝插件,了解开挂辅助教程(有挂辅助)1、下载安装好衡阳丫丫辅助,进...
玩家必看科普!贰柒拾手机辅助,... 您好:贰柒拾手机辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
总算了解!微乐小程序晃晃脚本,... 您好:掌中乐游戏辅助工具这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
一分钟了解!大当家辅助脚本设置... 一分钟了解!大当家辅助脚本设置,福建天天开心辅助器,实测开挂辅助教程(有挂透视);无需打开直接搜索薇...
专业讨论!新518互游辅助,广... 专业讨论!新518互游辅助,广西友乐app辅助工具,曝光开挂辅助教程(真的有挂);无需打开直接搜索薇...
记者揭秘!边锋干瞪眼小程序辅助... 记者揭秘!边锋干瞪眼小程序辅助,对战互娱辅助系统,原来有开挂辅助教程(有挂助手);无需打开直接搜索加...
盘点几款!微乐透视辅助功能插件... 盘点几款!微乐透视辅助功能插件下载,微信小程序微乐辅助器,必看开挂辅助教程(存在有挂);无需打开直接...
每日必看!小程序卡五星辅助,顺... 每日必看!小程序卡五星辅助,顺欣茶楼开挂,正版开挂辅助教程(证实有挂);无需打开直接搜索加薇1367...
热点推荐!江湖悠悠手游攻略,越... 热点推荐!江湖悠悠手游攻略,越乡游辅助软件,实测开挂辅助教程(有挂方略);无需打开直接搜索打开薇:1...