Android Bluetooth Communication overview

Source: Internet
Author: User
Tags filter stub

Under normal circumstances, our main operation of Bluetooth is: Turn on and off Bluetooth, search peripheral equipment, can be found by peripheral devices, to obtain matching equipment, Bluetooth equipment between the data transmission.

1, open Bluetooth (of course, first of all to ensure that your mobile phone is a Bluetooth device)

Bluetooth devices are mainly divided into two parts, one for local devices and the other for remote devices.

bluetoothadapter--a local device, the Bluetooth operation first requires a Bluetoothadapter instance. Several common methods are as follows:

Canceldiscovery ()--cancels the search operation on the local Bluetooth device, and if the local device is searching, then the search operation is stopped when the method is called.

Disable ()--Turn off the Bluetooth device.

Enable ()--opens the Bluetooth device. I believe we all have the experience of opening Bluetooth, usually will pop a window, said is requesting to open the Bluetooth device, you are not allowed so.

GetAddress ()--Gets the MAC address of the Bluetooth device.

Getdefaultadapter ()--Get the local Bluetooth device

GetName ()--Get the name of the local Bluetooth

Getremotedevice (String Address)--Obtaining a remote device based on a remote device's MAC addresses

StartDiscovery ()--Bluetooth device starts searching for peripherals
buletoothdevice--remote devices.

It contains the same methods as Bluetoothadapter, no longer tired.

View Code   
 //Get local Bluetooth Adapter instance  
             Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();  
             if (adapter!=null)  
             {  
                 if (!adapter.isenabled ())  
                 {  
                     ////The method is used to request the opening of our Bluetooth device  
                     Intent Intent = new Intent (bluetoothadapter.action_request_enable);  
                     StartActivity (intent);  
                 }  
             else
             {  
                 System.out.println ("Local device driver exception!");  
             }

2, Search Peripheral equipment

For Android find Bluetooth devices use the Bluetoothadapter class StartDiscovery () method to perform an asynchronous way to get the surrounding Bluetooth device because it is an asynchronous method so we don't have to consider the thread being blocked. The whole process takes about 12 seconds, at which point we can register a Broadcastreceiver object to receive the information of the Bluetooth device we find, and we filter the Action_found this intent action through filter to get the detailed information of each remote device. The device type Bluetoothclass that contains each Bluetoothdevice object and object is available through the intent field extra_device and Extra_class.

Implement a Broadcastreceiver class of your own and register this class.

View Code   
     Private class Bluetoothreciever extends Broadcastreceiver {  
       
         @Override public
         void OnReceive ( Context context, Intent Intent) {  
             //TODO auto-generated method stub  
             String action = intent.getaction ();  
             if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) {  
                 Bluetoothdevice device = Intent  
                         . Getparcelableextra ( Bluetoothdevice.extra_device);  
                 System.out.println (Device.getaddress ());}}  
       
     
Intentfilter intentfilter = new Intentfilter (bluetoothdevice.action_found);  
bluetoothreceive = new Bluetoothreciever ();  
Registerreceiver (bluetoothreceive, Intentfilter);

Because after registering a receiver, the program doesn't know when to recycle it, so we need to rewrite the OnDestroy () method of the activity class ourselves.

@Override
     protected void OnDestroy () {  
         //TODO auto-generated method stub  
         Unregisterreceiver ( bluetoothreceive);  
         Super.ondestroy ();  
     }

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.