Android 蓝牙开发清除GATT缓存

“android.bluetooth.BluetoothGatt”类中有一个“refresh”方法,该方法是清理GATT层缓存的方法,但是该方法是隐藏的,隐藏的方法我们是无法直接调用的,如果非要使用,可以采用java的反射机制进行调用,实现如下:

    /**
     * Clears the internal cache and forces a refresh of the services from the
     * remote device.
     */
    public boolean refreshDeviceCache() {
        if (mBluetoothGatt != null) {
            try {
                BluetoothGatt localBluetoothGatt = mBluetoothGatt;
                Method localMethod = localBluetoothGatt.getClass().getMethod(
                        "refresh", new Class[0]);
                if (localMethod != null) {
                    boolean bool = ((Boolean) localMethod.invoke(
                            localBluetoothGatt, new Object[0])).booleanValue();
                    return bool;
                }
            } catch (Exception localException) {
                Log.i(TAG, "An exception occured while refreshing device");
            }
        }
        return false;
    }

其中,“mBluetoothGatt”是在连接建立的时候设置的,另外对于该方法的使用,最好在断开连接成功的回调方法中使用,即在你自己实现的“BluetoothGattCallback”对象的“onConnectionStateChange”方法中判断“status”是“BluetoothProfile.STATE_DISCONNECTED”的时候调用。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容