Search code examples
androidbluetoothandroid-sourcel2capbluetoothadapter

Why is android.bluetooth.BluetoothAdapter#listenUsingL2capOn(int) not accessible?


This might be a newbie question since my experience with Android is limited but for some reason I'm unable to access the method android.bluetooth.BluetoothAdapter#listenUsingL2capOn(int). It seems to be public and hence not really sure why the symbol is not being recognized. And on a side note how does one open up L2CAP bluetooth socket server on android? Only one question I found somewhat related to this - How can I instantiate a L2Cap socket in Android?. Any help would be appreciated.


Solution

  • In Android we have two different levels of public methods:

    • Java-level public: This is controlled by the public keyword.
    • Android-level public: This is controlled by the @hide annotation. A method is Android-level public if it does not have the @hide annotation in its JavaDoc.

    If a method is Java-level public, but not Android-level public, this means it can only be used within the Android framework code, but not by app code. This is needed to keep the Android API as small as possible: When Android developers add a helper method that is useful in many places within the Android framework code, they would want this method to be Java-level public, so that it can be used by the other classes in the Android framework code. However, the Android developers might not want this helper method to be used by app code, because then if they later rework the code and the helper method is no longer needed, removing the method would break the apps using it. Not making the method Android-level public gives the Android developers more freedom to change/remove the method later on.

    Considering for example the method you gave: listenUsingL2capOn has the @hide annotation, as can be seen here. Therefore, it is not considered Android-level public. You can also see this from the API documentation of BluetoothAdapter, which does not list the methods that are not Android-level public (link).

    Regarding your side question: I'm not familiar with how to create a L2CAP bluetooth socket server in Android, but the answer that you linked looks promising. E.g. using BluetoothDevice#createL2capChannel and BluetoothAdapter#listenUsingL2capChannel. Note the name of the latter is different form listenUsingL2capOn.