Android Bluetooth API detailed

Source: Internet
Author: User

With nearly two years of wearable products gradually into people's lives, Bluetooth development has become an important module of Android development, let's say a Bluetooth API.

1. There are two main APIs for Bluetooth development:

Buletoothadapter: The local Bluetooth adapter, which means that the current application is running Bluetooth on the Android device

Buletoothdevice: Remote Bluetooth adapter, which means you want to connect the adapter to the Android device.

2. Bluetooth Permissions:

Android.permission.BLUETOOTH: Allow the program to connect to the paired Bluetooth device, request connection/Receive connection/Transfer data need to change the permissions, mainly for pairing after the operation;

Android.permission.BLUETOOTH_ADMIN: Allows the program to discover and pair Bluetooth devices, which is used to manage Bluetooth devices, with this permission, the application can use the Bluetooth device, mainly for the pre-pairing operation;

Priority: Bluetooth permission is the premise of bluetooth_admin permissions, if there is no Bluetooth permission, you can not use bluetooth_admin permissions;

3. Bluetooth Status value:

Bluetooth off: int State_off, with a value of 10, the Bluetooth module is turned off;

bluetooth on: int state_turning_on, the value is 11, the Bluetooth module is open;

bluetooth on: int state_on, the value is 12, the Bluetooth module is in the open state;

bluetooth on: int State_turning_off, the value is 13, the Bluetooth module is shutting down;

4. Bluetooth-related broadcasts:

Switch State change:

Bluetoothadapter.action_state_change:

The status of the current Bluetooth change can be obtained through Intent.getintextra (Bluetoothadapter.extra_state, Bluetoothadapter.error).

To search nearby available devices:

Bluetoothdevice.action_found:

Can be passed Intent.getparcelableextra (Bluetoothdevice.extra_device); Gets the remote device that is currently being searched.

Pairing Request:

Bluetoothdevice.action_pairing_request

Pairing status change:

Bluetoothdevice.action_pairing_request:

5. Common methods:

Turn Bluetooth on/off:

bluetoothadapter madapter = Bluetoothadapter.getdefaultadapter (); Get a local Bluetooth instance

if (!madapter.isenabled ()) {

Madapter.enable (); Turn on bluetooth

}else{

Madapter.enable (); Turn off Bluetooth

}

To set Bluetooth visibility:

Visible: Adapter.setscanmode (bluetoothadapter.scan_mode_connectable_discoverable);

Not visible: Adapter.setscanmode (bluetoothadapter.scan_mode_connectable);

Set visibility time-out: adapter.setdiscoverabletimeout (bluetooth_dscoverable_time);

Start/stop scanning of nearby devices:

Madapter.startdiscovery (); Start scanning

Madapter.startdiscovery (); Stop scanning

Get basic information about Bluetooth:

MAC Address: madapter.getaddress ();

Name: Madapter.getname ();

Pairing with a remote device:

try {

Method Createbondmethod = Bluetoothdevice.class. GetMethod ("Createbond");
Createbondmethod.invoke (device);
} catch (Exception e) {
E.printstacktrace ();
}

Android Bluetooth API detailed

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.