1. Permissions
Using a Bluetooth device requires opening permissions in manifest, which is located below.
[HTML]View Plaincopy
- <manifest ... >
- <application ... >
- ...
- </Application>
- Permission to use a Bluetooth device
- <uses-permission android:name="Android.permission.BLUETOOTH" />
- Manage permissions for a Bluetooth device
- <uses-permission android:name="Android.permission.BLUETOOTH_ADMIN" />
- </manifest>
2. Turn on Bluetooth
Get the Bluetooth adapter (android.bluetooth.BluetoothAdapter), check if the device supports Bluetooth, and turn on Bluetooth if it is supported.
[Java]View Plaincopy
- Check if the device supports Bluetooth
- adapter = Bluetoothadapter.getdefaultadapter ();
- if (adapter = = null)
- {
- //device does not support Bluetooth
- }
- Turn on bluetooth
- if (!adapter.isenabled ())
- {
- Intent Intent = new Intent (bluetoothadapter.action_request_enable);
- //Set Bluetooth visibility for up to 300 seconds
- Intent.putextra (Bluetoothadapter.extra_discoverable_duration, 300);
- Context.startactivity (Intent);
- }
3. Get a paired Bluetooth device (Android.bluetooth.BluetoothDevice)
The first time you connect a Bluetooth device you need to pair, once the pairing is successful, the information of the device will be saved, later connected to no longer need to pair, so the paired device is not necessarily able to connect.
[Java]View Plaincopy
- Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();
- set<bluetoothdevice> devices = Adapter.getbondeddevices ();
- for (int i=0; I<devices.size (); i++)
- {
- Bluetoothdevice device = (bluetoothdevice) devices.iterator (). Next ();
- System.out.println (Device.getname ());
- }
4. Search for nearby Bluetooth devices
After the adapter searches for a Bluetooth device, the results are broadcast out, so you need to customize a class that inherits the broadcast and obtain and process the search results for the Bluetooth device in the OnReceive method.
[Java]View Plaincopy
- Set up broadcast information filtering
- Intentfilter intentfilter = new Intentfilter ();
- Intentfilter.addaction (Bluetoothdevice.action_found);
- Intentfilter.addaction (bluetoothdevice.action_bond_state_changed);
- Intentfilter.addaction (bluetoothadapter.action_scan_mode_changed);
- Intentfilter.addaction (bluetoothadapter.action_state_changed);
- Register a broadcast receiver to receive and process search results
- Context.registerreceiver (receiver, intentfilter);
- Looking for a Bluetooth device, Android will send the found device in broadcast form
- Adapter.startdiscovery ();
Custom Broadcast Classes
[Java]View Plaincopy
- Private Broadcastreceiver receiver = new Broadcastreceiver () {
- @Override
- public void OnReceive (context context, Intent Intent) {
- String action = Intent.getaction ();
- if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) {
- Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device);
- System.out.println (Device.getname ());
- }
- }
- }
5. Pairing and status monitoring of Bluetooth devices
[Java]View Plaincopy
- Private Broadcastreceiver receiver = new Broadcastreceiver () {
- @Override
- public void OnReceive (context context, Intent Intent) {
- String action = Intent.getaction ();
- if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) {
- //Get the Bluetooth device found
- Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device);
- System.out.println (Device.getname ());
- //If the found device meets the device to be connected, process
- if (Device.getname (). Equalsignorecase (name)) {
- //The process of searching for Bluetooth devices consumes a lot of resources, and once you find a device that needs to be connected, you need to close the search promptly
- Adapter.canceldiscovery ();
- //Get the Bluetooth device connection status
- Connectstate = Device.getbondstate ();
- switch (connectstate) {
- //Not paired
- Case Bluetoothdevice.bond_none:
- //Pairing
- try {
- Method Createbondmethod = Bluetoothdevice. Class.getmethod ("Createbond");
- Createbondmethod.invoke (device);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- Break ;
- //Paired
- Case bluetoothdevice.bond_bonded:
- try {
- //Connection
- Connect (device);
- } catch (IOException e) {
- E.printstacktrace ();
- }
- Break ;
- }
- }
- } Else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals (ACTION)) {
- //state-changed broadcast
- Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device);
- if (Device.getname (). Equalsignorecase (name)) {
- Connectstate = Device.getbondstate ();
- switch (connectstate) {
- Case Bluetoothdevice.bond_none:
- Break ;
- Case bluetoothdevice.bond_bonding:
- Break ;
- Case bluetoothdevice.bond_bonded:
- try {
- //Connection
- Connect (device);
- } catch (IOException e) {
- E.printstacktrace ();
- }
- Break ;
- }
- }
- }
- }
- }
6. Connection of Bluetooth devices
[Java]View Plaincopy
- Private void Connect (Bluetoothdevice device) throws IOException {
- //fixed UUID
- final String spp_uuid = "00001101-0000-1000-8000-00805F9B34FB";
- UUID uuid = uuid.fromstring (spp_uuid);
- Bluetoothsocket socket = Device.createrfcommsockettoservicerecord (UUID);
- Socket.connect ();
- }
Find and connect Android Bluetooth devices