Bluetooth operations for Android development (1)-scan paired Bluetooth devices

Source: Internet
Author: User

1. What is Bluetooth )?

1.1 buletooth is the most widely used wireless communication protocol.

1.2 mainly for short-range device communication (10 m)

1.3 is usually used to connect headphones, mouse, and mobile communication devices.

2. Bluetooth-related APIs

2.1 define thadapter:

Represents the local Bluetooth adapter

2.2 define thdevice

Represents a remote Bluetooth device

3. Scan the paired bluetooth device (1)

Note: The simulator cannot be implemented because it must be deployed on a real mobile phone.

First, declare the Bluetooth permission in androidmanifest. xml.

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

Manual operation is required for pairing Bluetooth:

1. Enable settings --> Wireless Network --> enable Bluetooth

2. Enable the Bluetooth settings to scan the Bluetooth devices that have been enabled around (you can pair them with your laptop), and click "pairing ".

A prompt window will pop up on the computer: Add a device

The matching code between the calculation and the device is displayed. Check whether the device is paired.

A similar prompt is displayed on the phone.

4. Scan the paired bluetooth device (2)

4.1 obtain the thadapter object

4.2 determine whether the current mobile device has Bluetooth

4.3 determine whether Bluetooth is enabled on the current mobile device

4.4 obtain all paired bluetooth device objects

The implementation code is as follows:

Mainactivity:

Import Java. util. iterator; import Java. util. set; import android. app. activity; import android. bluetooth. export thadapter; import android. bluetooth. export thdevice; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity {private button = NULL;/** called when Activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. buttonid); button. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// obtain the javasthadapter object. This API is supported by javasthadapter adapter = javasthadapter starting from Android 2.0. getdefaadapter adapter ();// Adapter is not equal to null, indicating that the local machine has a bluetooth device if (adapter! = NULL) {system. Out. println ("the local device has a bluetooth device! "); // If the bluetooth device is not enabled if (! Adapter. isenabled () {intent = new intent (effecthadapter. action_request_enable); // request to enable the bluetooth device startactivity (intent);} // obtain the set of paired remote Bluetooth devices <symbol thdevice> devices = adapter. getbondeddevices (); If (devices. size ()> 0) {for (iterator <symbol thdevice> it = devices. iterator (); it. hasnext ();) {descrithdevice device = (descrithdevice) it. next (); // print the physical address of the remote Bluetooth device system. out. println (device. getaddress () ;}} else {System. Out. println ("no remote Bluetooth device has been paired! ") ;}} Else {system. Out. println (" no Bluetooth device on this machine! ");}}});}}

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.