Android蓝牙开发 Gatt over EDR

在网上搜索了很多关于gatt over edr的资料,结果全部都是关于gatt over ble的信息。

事实上,通过gatt连接edr关键就在于获取BluetoothGatt。

BluetoothDevice device;
BluetoothGatt gatt = device.connectGatt(this, false, callback, BluetoothDevice.TRANSPORT_LE);

一般来说通过gatt连接BLE我们都会使用上述接口,通过下述回调获取到gatt

BluetoothGattCallback callback = new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
            }
        }

我们查看BluetoothDevice,我们可以看到有以下几个参数

    public static final int TRANSPORT_AUTO = 0;

    /**
     * Prefer BR/EDR transport for GATT connections to remote dual-mode devices
     */
    public static final int TRANSPORT_BREDR = 1;

    /**
     * Prefer LE transport for GATT connections to remote dual-mode devices
     */
    public static final int TRANSPORT_LE = 2;

因此,我们借鉴BLE的连接方式,在接口中传入BluetoothDevice.TRANSPORT_BREDR

写到这,大家可能会觉得,那这不就成功解决了吗。我也是这么认为的。然而,事情远没有这么简单。当我理所当然的吧参数写入后,对newState状态进行判断。我们发现,newState != BluetoothProfile.STATE_CONNECTED, 同样的,尝试传入BluetoothDevice.TRANSPORT_AUTO一样不行。于是这就陷入了僵局,我曾认为可能连接EDR不能像BLE一样连接。

后来,经过各种资料查找,我发现,通过使用老借口,反而可以实现gatt over edr!

不啰嗦了,直接上代码。

BluetoothDevice device;
BluetoothGatt gatt = device.connectGatt(this, false, callback);

当我们不传入连接方式时,反而成功连接上了。

按理来说,当我们不传入连接方式时,应是走BluetoothDevice.TRANSPORT_AUTO,但是当我们按照这个逻辑传入时,却无法正常连接。

以上就是我开发gatt over edr的一些小小心得,仅供大家参考

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值