At the application framework level is the app’s code, which utilizes the android.bluetooth APIs to interact with the bluetooth hardware. Internally, this code calls the Bluetooth process through the Binder IPC mechanism. Get the code : $ git clone https://android.googlesource.com/platform/frameworks/base $ git checkout -b lollipop-release origin/lollipop-release $ cd base Immediate search tells us following files are having code related to Bluetooth, this framework defines the basic package ” android.bluetooth ” which Provides classes that manage Bluetooth functionality, such as scanning for devices, connecting with devices, and managing data transfer between devices.
Command line execution
git clone https://android.googlesource.com/platform/frameworks/base $ git checkout -b lollipop-release origin/lollipop-release $ cd base Immediate search tells us following files are having code related to Bluetooth, this framework defines the basic package ” android.bluetooth ” which Provides classes that manage Bluetooth functionality, such as scanning for devices, connecting with devices, and managing data transfer between devices. The Bluetooth APIs let applications: ( Refer Link for package details ) Scan for other Bluetooth devices (including BLE devices). Query the local Bluetooth adapter for paired Bluetooth devices. Establish RFCOMM channels/sockets. Connect to specified sockets on other devices. Transfer data to and from other devices. Communicate with BLE devices, such as proximity sensors, heart rate monitors, fitness devices, and so on. Act as a GATT client or a GATT server (BLE). base$ find . -name *Bluetooth* File : ./services/core/java/com/android/server/BluetoothManagerService.java Now, lets understand how this file comes into picture, as we know “SystemServer” starts during booting of android, so during booting “Bluetooth Service Manager” gets added to the initialisation sequence, Slog.i(TAG, "Bluetooth Manager Service"); bluetooth = new BluetoothManagerService(context); ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth); refer above code from SystemServer base/services/java/com/android/server/SystemServer.java above section of code calls “BluetoothManagerService” from ./services/core/java/com/android/server/BluetoothManagerService.java BluetoothManagerService(Context context) { mHandler = new BluetoothHandler(IoThread.get().getLooper()); mContext = context; mBluetooth = null; mBinding = false; mUnbinding = false; mEnable = false; mState = BluetoothAdapter.STATE_OFF; mQuietEnableExternal = false; mEnableExternal = false; mAddress = null; mName = null; mErrorRecoveryRetryCounter = 0; mContentResolver = context.getContentResolver(); mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); filter.addAction(Intent.ACTION_USER_SWITCHED); registerForAirplaneMode(filter); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); mContext.registerReceiver(mReceiver, filter); loadStoredNameAndAddress (); /* Retrieve the Bluetooth Adapter’s name and address and save it in the local cache */ if (isBluetoothPersistedStateOn()) { /* checks if previous saved state is ON */ mEnableExternal = true; } int sysUiUid = -1; try { sysUiUid = mContext.getPackageManager().getPackageUid(“com.android.systemui”, UserHandle.USER_OWNER); /* USER_OWNER : A user id constant to indicate the “owner” user of the device, defined at core/java/android/os/UserHandle.java */ } catch (PackageManager.NameNotFoundException e) { Log.wtf(TAG, “Unable to resolve SystemUI’s UID.”, e); } mSystemUiUid = sysUiUid; } That means, above service just prepares the bluetooth functionality. Logcat messages, might look like below, D/BluetoothManagerService( 453): Loading stored name and address D/BluetoothManagerService( 453): Stored bluetooth Name=null,Address=null I/SystemServer( 453): Bluetooth Manager Service I/PackageManager( 453): Running dexopt on: com.android.bluetooth W/PackageManager( 453): Unknown permission com.google.android.gallery3d.permission.GALLERY_PROVIDER in package com.android.bluetooth W/PackageManager( 453): Unknown permission android.permission.MMS_SEND_OUTBOX_MSG in package com.android.bluetooth D/BluetoothTethering( 453): startMonitoring: target: Handler (com.android.server.ConnectivityService$NetworkStateTrackerHandler) {2c6ce818} D/BluetoothTethering( 453): startMonitoring: mCsHandler: Handler (com.android.server.ConnectivityService$NetworkStateTrackerHandler) {2c6ce818} D/BluetoothManagerService( 453): Message: 20 D/BluetoothManagerService( 453): Added callback: android.bluetooth.BluetoothAdapter$1@2c6a7810:true D/BluetoothManagerService( 453): Message: 30 W/ContextImpl( 453): Calling a method in the system process without a qualified user: android.app.ContextImpl.bindService:1543 android.bluetooth.BluetoothPan.doBind:149 android.bluetooth.BluetoothPan.<init>:141 android.bluetooth.BluetoothAdapter.getProfileProxy:1190 android.bluetooth.BluetoothTetheringDataTracker.startMonitoring:116 D/BluetoothManagerService( 453): Message: 20 D/BluetoothManagerService( 453): Added callback: android.bluetooth.IBluetoothManagerCallback$Stub$Proxy@2c3f0ae8:true D/BluetoothManagerService( 453): Message: 300 D/BluetoothManagerService( 453): MESSAGE_USER_SWITCHED The above messages gets printed from, services/core/java/com/android/server/BluetoothManagerService.java, function ” public void handleMessage(Message msg) { if (DBG) Log.d (TAG, “Message: ” + msg.what); switch (msg.what) { } } these messages are declared at the initial of file. private static final int MESSAGE_REGISTER_ADAPTER = 20; private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; private static final int MESSAGE_USER_SWITCHED = 300; ——————————————————————————————————————————————————— ./core/java/android/bluetooth/BluetoothManager.java High level manager used to obtain an instance of an BluetoothAdapter and to conduct overall Bluetooth Management. ( Refer Link to check actual functions defined by this class ) ./core/java/android/bluetooth/IBluetoothManagerCallback.aidl File : ./core/java/android/bluetooth/IBluetoothManager.aidl /** * System private API for talking with the Bluetooth service. * * {@hide} */ interface IBluetootRisk level: destructive. Review the command before running it.
Implementation details
The Bluetooth APIs let applications: ( Refer Link for package details ) Scan for other Bluetooth devices (including BLE devices). Query the local Bluetooth adapter for paired Bluetooth devices. Establish RFCOMM channels/sockets. Connect to specified sockets on other devices. Transfer data to and from other devices.
Gotchas and common issues
Permission checks - verify user access rights and sudo privileges before executing system-level operations.
Environment configuration - double-check path variables and dependency versions to prevent runtime failures.
Backup safeguards - maintain configuration backups before applying system or database modifications.
Following these steps ensures clean configuration and reliable execution for understanding android bluetooth code : part 1 – application framework.
Comments and corrections