Why Android BluetoothDevice.conenctGatt require context if it not use it

Viewed 800

I'm developing android app for ble communication.

My question is why the function

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

requires Context as parameter, I dug down the functions and found that it not using it anywhere as you can see here:

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;
    }

The weird thing is that this property not marked as deprecated too. I tried to pass null instead of the context and seems that it works fine () with context or without.

Anyone know why is it there?

1 Answers

OK, based on short browsing of the Android source code repository this seems to be a remnant of an older design. The connectGatt function creates a BluetoothGatt object whose constructor preivously needed a Context argument. (This was way back in API 18.) BluetoothGatt originally needed the Context object for something but the code had been removed before the class even became public six years ago. The Context argument in the constructor, however, remained. About three years ago it was finally removed, but by then the connectGatt API has been public for years and they couldn't remove the now useless argument without breaking a lot of existing code. So they didn't.

Think of it as the human appendix or the tailbone - a vestigial remnant left behind by evolution :-)

Related