Bluetooth permission
To use the BLUETOOTH function in your application, you must declare at least one of two BLUETOOTH permissions (BLUETOOTH and th_admin.
To perform any BLUETOOTH communication (such as requesting a connection, receiving a connection, and transmitting data), you must apply for the BLUETOOTH permission.
To enable device discovery or maintain Bluetooth settings, you must apply for the th_admin permission. Most applications that require this permission are only used to discover local Bluetooth devices. The other capabilities granted by this permission should not be used. Unless it is a power management application, it will modify the Bluetooth settings according to the user's request. Note: If you use the th_admin permission, you must have the BLUETOOTH permission.
Declare the Bluetooth permission in your application list file, for example:
<Manifest...>
<Uses-permission android: name = "android. permission. BLUETOOTH"/>
...
</Manifest>
For more information about declaring application permissions, see <uses-permission>
Set Bluetooth
Before the application can communicate through the Bluetooth channel, make sure that the device supports Bluetooth communication.
If Bluetooth is not supported, you should disable all Bluetooth functions. If Bluetooth is supported but disabled, You need to request the user to enable the Bluetooth function without leaving your application. In this setting, you need to use the javasthadapter object, complete the following two steps.
1. Obtain the thadapter object
The descrithadapter object is required for all Bluetooth activities. To obtain this object, you must call the static getdefaadapter adapter () method. This method returns a thadapter object that represents the device's own Bluetooth adapter. The entire system has a Bluetooth adapter that your application can use to interact with each other. If the getdefaadapter adapter () method returns null, the device does not support Bluetooth and your processing ends here. For example:
Descrithadapter mblustmthadapter = descrithadapter. getdefaadapter adapter (); if (mblustmthadapter = null) {// Device does not support Bluetooth} 2. Enable the Bluetooth function
Next, make sure that Bluetooth is available. Call the isEnabled () method to check whether the current Bluetooth is available. If this method returns false, Bluetooth is disabled. To enable Bluetooth, call the startActivityForResult () method with the ACTION_REQUEST_ENABLE operation intent. It sends a Bluetooth enabled request to the system settings (do not terminate your application ). For example:
If (! Mblustmthadapter. isEnabled () {Intent enableBtIntent = newIntent (effecthadapter. ACTION_REQUEST_ENABLE); startActivityForResult (enableBtIntent, REQUEST_ENABLE_BT );}
A dialog box requesting users to enable the Bluetooth function is displayed, as shown in figure 1:
Figure 1. Enable the Bluetooth function dialog box.
If the user responds "Yes", the system starts to enable the Bluetooth function to complete the startup process (which may fail), and the focus will be returned to your application.
The REQUEST_ENABLE_BT constant passed to the startActivityForResult () method is an integer defined in your application (it must be greater than 0 ), the system will return it to your onActivityResult () callback implementation as the requestCode parameter.
If the Bluetooth function is enabled successfully, your Activity will receive the RESULT_ OK result in the onActivityResult () callback. If the Bluetooth is not started (or the user responds to "No "), the result is encoded as RESULT_CANCELED.
(Optional) Your application can also listen to the ACTION_STATE_CHANGED broadcast Intent. The System Broadcasts this Intent whenever the Bluetooth status changes. The additional fields EXTRA_STATE and extra_previus_state in this broadcast indicate the new and old Bluetooth states respectively. Possible values of these additional fields are STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF, and STATE_OFF. Listening to this broadcast is useful for detecting the Bluetooth status when the application is running.
Tip: enabling the discoverable function automatically starts the Bluetooth function. If you plan to enable the device's discoverable mechanism before performing a Bluetooth activity, you can skip step 1 above. For details, see "enable Bluetooth discoverable" below ".