android - 为什么 Android BluetoothDevice.conenctGatt 在不使用时需要上下文

标签 android bluetooth-lowenergy

我正在开发用于无线通信的 android 应用程序。

我的问题是为什么这个函数

public BluetoothGatt connectGatt(Context context, boolean autoConnect,BluetoothGattCallback callback, int transport)

需要 Context 作为参数,我深入研究了这些函数,发现它没有在任何地方使用它,正如你在这里看到的:

public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback, int transport,
                                     boolean opportunistic, int phy, Handler handler) {
        if (callback == null)
            throw new NullPointerException("callback is null");

        // TODO(Bluetooth) check whether platform support BLE
        //     Do the check here or in GattServer?
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        IBluetoothManager managerService = adapter.getBluetoothManager();
        try {
            IBluetoothGatt iGatt = managerService.getBluetoothGatt();
            if (iGatt == null) {
                // BLE is not supported
                return null;
            }
            BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy);
            gatt.connect(autoConnect, callback, handler);
            return gatt;
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

奇怪的是这个属性也没有被标记为弃用。 我尝试传递 null 而不是上下文,似乎它在有上下文或没有上下文的情况下都能正常工作 ()。

有人知道它为什么在那里吗?

最佳答案

好的,基于对 Android 源代码存储库的短暂浏览,这似乎是旧设计的残余。 connectGatt 函数创建一个 BluetoothGatt 对象,其构造函数先前需要一个 Context 参数。 (这是 way back in API 18。)BluetoothGatt 最初需要 somethingContext 对象但是代码甚至在上课之前就被删除了became public六年前。但是,构造函数中的 Context 参数保留了下来。大约三年前它是finally removed ,但那时 connectGatt API 已经公开多年,他们无法在不破坏大量现有代码的情况下删除现在无用的参数。所以他们没有。

把它想象成人类的阑尾或尾骨——进化留下的退化残余:-)

关于android - 为什么 Android BluetoothDevice.conenctGatt 在不使用时需要上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642912/

相关文章:

android - 如何更改android :layout_alignParentRight runtime by java code

Android选择监听器

ios - 即使蓝牙关闭,iBeacon 也可以扫描 iOS 设备吗?

ios - swift :信标测距在打电话时返回空数组

android - 无法在 Unity 中获取 EGL14.eglGetCurrentContext()

java - 使用另一个 Activity 的静态方法启动 Activity 是一个好习惯吗?

状态更改后的 Android Bottom Sheet

从 "Accu-Chek Aviva Connect"BG 仪表收集数据的 iOS 应用程序。 (低功耗蓝牙)

未调用 Android BLE onCharacteristicChanged

ios - TI Sensortag,如何使用按钮?