This simple tool lets a developer observe documented Bluetooth-related Broadcast Intents that are generated when Bluetooth is exercised, using adb logcat with the device tethered to a development machine.
Before writing any code, the developer can learn how Bluetooth works by using the phone's settings app to turn Bluetooth on and off, pair and unpair devices, and change which devices are active, while observing which intents are generated by each action.
The tool can also be used to verify that certain Bluetooth intents are being generated in response to an app's use of the Bluetooth subsystem.
Intent actions and extras are displayed by name rather than by value, so that they appear as they would in the Android documentation, and so it is easier to follow what is happening.
IMPORTANT: Before you rely on an ACTION or EXTRA reported by this application, make sure it is officially documented! This app may display some ACTIONS or EXTRAS that don't appear in the official documentation. These would include undocumented features or those specific to a device manufacturer, that will not be available on some (or most) Android devices.
This project was created to gain a better understanding of Bluetooth operation on Android devices.
After the application is launched, intents are viewed in the Android Studio logcat window, or by using the adb tool:
$ adb logcat -s BluetoothIntentLogger
Intents are viewed in real time while the device is being manipulated. The application's activity needs to be running but need not be visible.
Hint: Hit carriage return in the logcat window to put some space between entries.
Another hint: Use
$ adb logcat | grep /Bluetooth
to show messages from this app and many other Bluetooth subsystem messages.
All Bluetooth Broadcast Intents documented in the Android Bluetooth public API as of API 21 are logged.
Some non-Bluetooth Intents related to music playback are logged as well, but are device- and/or application-dependent, so they won't always be observed. Other non-Bluetooth Intents that might be generated elsewhere in response to Bluetooth activity are not shown.
To install, import the project into Android Studio, connect a device, and run it.
The Bluetooth broadcast intents that we want to receive are specified in AndroidManifest.xml, and the broadcast receiver defined by LoggingBroadcastReceiver.java is specified there as well, as the receiver for these intents.
When one of the specified broadcast intents is available, the system calls the onReceive() method in LoggingBroadcastReceiver.java, passing it the intent.
onReceive() checks to see if this is a Bluetooth intent, and if so, passes it to the receive() method in BluetoothHandler.java. There, the intent's extras are read, its values converted to the constant names found in the Android Bluetooth API documentation or at runtime using reflection, and logged.
The only reason we don't do this parsing in onReceive() itself and move it to the receive() method in BluetoothHandler.java is to make things a bit more readable, and to allow us to more easily add processing for additional intents later without making onReceive() huge.
The application's Activity does not participate in the BroadcastReceiver mechanism. However, it must be launched for the BroadcastReceiver to begin to receive broadcast intents. It should continue to receive intents when the Activity is in the background. See "Launch controls on stopped applications" in http://developer.android.com/about/versions/android-3.1.html.
This was built for API levels 14 and above, using SDK version 21.
The following examples were captured on a Sprint Samsung Galaxy S4 running stock Android version 4.4.2.
Prior to this capture, Bluetooth was enabled with a number of paired devices, but none with an active connection.
With verbose logging unchecked:
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): BluetoothAdapter.ACTION_STATE_CHANGED
D/BtLogger(11912): EXTRA_PREVIOUS_STATE: STATE_ON
D/BtLogger(11912): EXTRA_STATE: STATE_TURNING_OFF
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): BluetoothAdapter.ACTION_SCAN_MODE_CHANGED
D/BtLogger(11912): EXTRA_SCAN_MODE: SCAN_MODE_NONE
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): BluetoothAdapter.ACTION_STATE_CHANGED
D/BtLogger(11912): EXTRA_PREVIOUS_STATE: STATE_TURNING_OFF
D/BtLogger(11912): EXTRA_STATE: STATE_OFF
With verbose logging checked:
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): Class: BluetoothAdapter Action: ACTION_STATE_CHANGED
D/BtLogger(11912): Extra: EXTRA_PREVIOUS_STATE Type: java.lang.Integer Value: STATE_ON (12)
D/BtLogger(11912): Extra: EXTRA_STATE Type: java.lang.Integer Value: STATE_TURNING_OFF (13)
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): Class: BluetoothAdapter Action: ACTION_SCAN_MODE_CHANGED
D/BtLogger(11912): Extra: EXTRA_SCAN_MODE Type: java.lang.Integer Value: SCAN_MODE_NONE (20)
D/BtLogger(11912): --------------------New Bluetooth Broadcast Intent-------------------
D/BtLogger(11912): Class: BluetoothAdapter Action: ACTION_STATE_CHANGED
D/BtLogger(11912): Extra: EXTRA_PREVIOUS_STATE Type: java.lang.Integer Value: STATE_TURNING_OFF (13)
D/BtLogger(11912): Extra: EXTRA_STATE Type: java.lang.Integer Value: STATE_OFF (10)
Portions of Android Bluetooth source from AOSP were consulted in order to obtain constant names for values that are provided in Bluetooth broadbast intent extras.
This project is governed by the Apache 2.0 license.
Copyright (C) 2015 PhoneDeveloper LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.