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