Home » Android » NDK / Middleware / HAL » How Android Bluetooth Profiles are enabled and Services are activated ?

How Android Bluetooth Profiles are enabled and Services are activated ?

How Android Bluetooth profiles are enabled ?

Answer:

Check packages/apps/Bluetooth/res/values/config.xml

<resources>
    <bool name="profile_supported_a2dp">true</bool>
</resources>

If you want to disable the above profile, change from “true” to “false”

The above flags are used during initialisation as below, in file, packages/apps/Bluetooth/src/com/android/bluetooth/btservice/Config.java

Resource flag to indicate whether profile is supported or not.

private static final int[]  PROFILE_SERVICES_FLAG = {
        R.bool.profile_supported_hs_hfp,
        R.bool.profile_supported_a2dp,
        R.bool.profile_supported_a2dp_sink,
        R.bool.profile_supported_hid,
        R.bool.profile_supported_hdp,
        R.bool.profile_supported_pan,
        R.bool.profile_supported_gatt,
        R.bool.profile_supported_map,
        R.bool.profile_supported_hfpclient,
        R.bool.profile_supported_avrcp_controller,
    };

Supported Profile services are defined at,

private static final Class[] PROFILE_SERVICES = {
        HeadsetService.class,
        A2dpService.class,
        A2dpSinkService.class,
        HidService.class,
        HealthService.class,
        PanService.class,
        GattService.class,
        BluetoothMapService.class,
        HeadsetClientService.class,
        AvrcpControllerService.class,
    };

Now inside init, it checks resources (from packages/apps/Bluetooth/res/values/config.xml ) flag and adds a profile if its supported as below,

boolean supported = resources.getBoolean(PROFILE_SERVICES_FLAG[i]);
            if (supported) {
                Log.d(TAG, "Adding " + PROFILE_SERVICES[i].getSimpleName());
                profiles.add(PROFILE_SERVICES[i]);
            }
Refer to Link for more details.

How Profile Services are started ? As mentioned above, the declaration of profile services, is done, now the profile service actually gets started from packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

void processStart() {
Class[] supportedProfileServices = Config.getSupportedProfiles(); [ This is called to Link ] mJniCallbacks.init(mBondStateMachine,mRemoteDevices); 

[ This calls the JNI calls for the particular profile service ]

//Start profile services
      if (!mProfilesStarted && supportedProfileServices.length >0) {
            //Startup all profile services
            setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON);
        }else {
            debugLog("processStart() - Profile Services alreay started");
            mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED));
        }

The above functions calls to starts the profile services, Call to setProfileServiceState goes to
file packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

private void setProfileServiceState(Class[] services, int state) {        
for (int i=0; i <services.length;i++) {
            String serviceName = services[i].getName();
            Integer serviceState = mProfileServicesState.get(serviceName);
            if(serviceState != null && serviceState != expectedCurrentState) {
                debugLog("setProfileServiceState() - Unable to "
                    + (state == BluetoothAdapter.STATE_OFF ? "start" : "stop" )
                    + " service " + serviceName
                    + ". Invalid state: " + serviceState);
                continue;
            }

            debugLog("setProfileServiceState() - "
                + (state == BluetoothAdapter.STATE_OFF ? "Stopping" : "Starting")
                + " service " + serviceName);

            mProfileServicesState.put(serviceName,pendingState);
            Intent intent = new Intent(this,services[i]);
            intent.putExtra(EXTRA_ACTION,ACTION_SERVICE_STATE_CHANGED);
            intent.putExtra(BluetoothAdapter.EXTRA_STATE,state);
            startService(intent);
        }
}

Leave a Comment