Bluetooth operations for Android

Source: Internet
Author: User

1. What is Bluetooth?

Bluetooth is one of the most widely used wireless communication protocols;
Communication is mainly set for short distances; 5 to 10 meters
Connect headphones, mouse, and mobile communication devices;


Bluetooth-related APIs:
Descrithadapter: the object of this class represents the local Bluetooth adapter;

Common Methods for bluetoothadapter:

Isenabled () determines whether the Bluetooth is enabled. If enabled, true is returned. Otherwise, false is returned.

Getaddress () Get the local Bluetooth address

Getname () Get the local Bluetooth name

Startdiscovery () to start searching. This is the first step of searching.

Isdiscovering () determines whether the device is currently being searched. True is returned.

Getstate () gets the current status of the local Bluetooth adapter (it may be needed during debugging)

Getdefaultadapter () obtains the default thadapter. In fact, only this method can be used to obtain the thadapter.

Getremotedevice (string address) obtains the remote Bluetooth device based on the Bluetooth address.

Getbondeddevices () Get all pairs of Bluetooth adapter objects

Disable () Disable Bluetooth
Enable () enables Bluetooth. This method does not display a prompt;

In more cases, we need to ask whether the user can enable the service. The two lines of code also enable Bluetooth, but the user will be prompted:

// Create an intent object used to start an activity and prompt the user to create a bluetooth device
Intent intent = new intent (effecthadapter. action_request_enable );
Startactivity (intent );

Effecthdevice: represents a remote Bluetooth device;

Common Methods: getaddress (), getname (), same as descrithadapter


To use a bluetooth device, you must declare the Bluetooth permission in androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. Bluetooth"/>

2. Modify the visibility of the local Bluetooth device

Declare the Bluetooth permission in androidmanifest. xml:

<Uses-Permission Android: Name = "android. Permission. effecth_admin"/>

The following is a small case. You can: Turn on Bluetooth, modify the visibility of Bluetooth devices, and scan Bluetooth devices that can be paired. Main. the XML file is not provided here with three buttons. You can directly view thactivity. java files

Public class extends thactivity extends activity {private button button1, button2, button3; descrithadapter implements thadapter; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button1 = (button) findviewbyid (R. id. button1); button2 = (button) findviewbyid (R. id. button2); button3 = (button) findviewbyid (R. id. button3); // create an intentfilter Object. action_foud intentfilter = new intentfilter (effecthdevice. action_found); fig = new fig (); // register the broadcast receiver registerher Er (FIG, intentfilter); fig = fig. getdefaadapter adapter (); // enable Bluetooth button1.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// get bluuetoot Hadapter object export thadapter adapter = export thadapter. getdefaadapter adapter (); // determines whether the export thadapter object is null. If it is null, it indicates that the local machine does not have a bluetooth device if (adapter! = NULL) {system. Out. println ("local Bluetooth device"); // call the isenabled () method to determine whether the current bluetooth device is available if (! Adapter. isenabled () {// create an intent object, which is used to start an activity and prompts the user to open an account. action_request_enable); startactivity (intent);} // obtain all pairs of Bluetooth adapter objects set <effecthdevice> devices = adapter. getbondeddevices (); If (devices. size ()> 0) {for (iterator = devices. iterator (); iterator. hasnext ();) {effecthdevice = (effecthdevice) iterator. next (); // obtain the remote Bluetooth device address system. out. println (effecthdevice. getaddress () ;}} else {system. out. println ("no Bluetooth device") ;}}); // This listener is used to modify the visibility of the bluetooth device button2.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view V) {// create an intent object and set its action value to descrithadapter. action_request_discoverableintent discoverableintent = new intent (descrithadapter. action_request_discoverable); // stores a key-value pair to an intent object, which is mainly used to specify the duration of the visible State discoverableintent. putextra (effecthadapter. extra_discoverable_duration, 500); startactivity (discoverableintent) ;}}); // scan the paired bluetooth device button3.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {descrithadapter. startdiscovery () ;}});} private class extends threceiver extends broadcastreceiver {@overridepublic void onreceive (context, intent) {string action = intent. getaction (); If (descrithdevice. action_found.equals (Action) {// you can retrieve the intent object representing the remote Bluetooth adapter from the received intent object. Export thdevice device = intent. getparcelableextra (effecthdevice. extra_device); system. out. println (device. getaddress ());}}}}

Before doing this, you must have an Android mobile phone with Bluetooth and a computer with Bluetooth. Otherwise, the Operation will fail,
Do not forget to declare the Bluetooth permission in androidmanifest. xml:

<Uses-Permission Android: Name = "android. Permission. Bluetooth"/>
<Uses-Permission Android: Name = "android. Permission. effecth_admin"/>


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.