如果Android BluetoothDevice.conenctGatt不使用上下文,为什么会需要上下文

时间:2019-06-18 06:29:59

标签: android bluetooth-lowenergy

我正在开发用于ble通讯的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而不是上下文,似乎无论是否有上下文,它都可以正常工作()。

有人知道为什么在那儿吗?

1 个答案:

答案 0 :(得分:0)

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

将其视为人类的阑尾或尾骨-进化遗留下来的残留痕迹:-)