Android12手机卡在看到相同的BLE外设服务和特征UUID上。
创始人
2024-09-30 21:30:18
0

该问题是由于Android 12中的BluetoothGatt缓存机制导致的。缓存导致它会将同一外设的多个服务和特征识别为同一服务和特征,从而导致连接中的冲突。解决这个问题,是通过在连接处清除缓存来实现的。

以下是一个示例代码,可以解决这个问题:

public class MyBluetoothGattCallback extends BluetoothGattCallback {
    private BluetoothGatt mGatt;
    
    public MyBluetoothGattCallback(BluetoothGatt gatt) {
        mGatt = gatt;
    }
    
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if(newState == BluetoothProfile.STATE_CONNECTED) {
            gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
            gatt.discoverServices();
            clearGattCache(mGatt); //清除缓存
        } else if(newState == BluetoothProfile.STATE_DISCONNECTED){
            mGatt.close();
        }
    }
    
    private void clearGattCache(BluetoothGatt gatt) {
        try {
            Method localMethod = gatt.getClass().getMethod("refresh");
            if (localMethod != null) {
                localMethod.invoke(gatt);
            }
        } catch (Exception localException) {
            Log.e("ClearGattCache", "An exception occurred while refreshing device");
        }
    }
}

在MyBluetoothGattCallback中,当状态改变为STATE_CONNECTED时,我们清除蓝牙Gatt缓存。 这种方法可以有效地防止同一外设的多个服务和特征被识别为同一服务和特征。

相关内容

热门资讯

透视插件"哈糖大菠萝... 1、透视插件"哈糖大菠萝万能挂"详细辅助玩家教你(有辅助器攻略)(UU poker、哈糖大菠萝万能挂...
透视有挂"aapok... 透视有挂"aapoker发牌逻辑"详细辅助黑科技教程(有用辅助);《WPK辅助透视》‌:支持手机实时...
透视脚本"wepok... 透视脚本"wepoker游戏的安装教程"详细辅助实用技巧(作弊辅助技巧);建议优先通过wepoker...
透视私人局"wepo... 自定义wepoker透视脚本免费系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
透视肯定"we-po... 透视肯定"we-poker辅助"详细辅助2025教程(有辅助攻略);玩家必备必赢加哟《1367043...
透视好友"wepok... 透视好友"wepoker私人局透视插件"详细辅助AA德州教程(透视脚本免费下载pc软件);1.wep...
透视辅助"hhpok... 《透视辅助"hhpoker买挂"详细辅助2025新版(插件下载方法)》 hhpoker买挂软件透明挂...
透视神器"wepok... 透视神器"wepoker模拟器哪个"详细辅助2025新版总结(辅助器软件)是一款可以让一直输的玩家,...
透视好友房"佛手大菠... 透视好友房"佛手大菠萝13道挂哪里"详细辅助2025新版教程(透视挂底牌辅助);大神普及一款德州ai...
透视私人局"拱趴大菠... 透视私人局"拱趴大菠萝自动计算机器人"详细辅助教你教程(轻量版辅助教程);亲真的是有正版授权,小编(...