Android connectivity-Bluetooth (2)

Source: Internet
Author: User

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:

BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter ==null){
// Device does not support Bluetooth
}

2. Enable Bluetooth

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(!mBluetoothAdapter.isEnabled()){
    Intent enableBtIntent =newIntent(BluetoothAdapter.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 ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.