Bluetooth for Android (one)-Scan paired Bluetooth devices

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

I. What is Bluetooth?

1.1 Buletooth is currently the most widely used wireless communication protocol

1.2 Mainly for short-range equipment communication (10m)

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

Two. Bluetooth-related APIs

2.1 Bluetoothadapter:

Represents a local Bluetooth adapter

2.2 Bluetoothdevice

Represents a remote Bluetooth device

Three. Scan for paired Bluetooth devices (1)

Note: Must be deployed on real phone, emulator cannot be implemented

First you need to declare the Bluetooth permissions in Androidmanifest.xml

<user-permission android:name= "Android.permission.BLUETOOTH"/>

Pairing Bluetooth requires manual action:

1. Open Settings-Wireless network--Bluetooth tick on

2. Turn on the Bluetooth settings to scan the Bluetooth devices that are already turned on (you can pair them with your laptop), tap to Pair

A popup window will appear on your computer: Add device

Displays the pairing code between the calculation and the device, requiring confirmation of pairing

Similar prompts will appear on your phone.

Four. Scan for paired Bluetooth devices (2)

4.1 Getting Bluetoothadapter objects

4.2 Determine if Bluetooth is present in the current mobile device

4.3 Determine whether Bluetooth is turned on in the current mobile device

4.4 Get all Bluetooth device objects that have been paired

The implementation code is as follows:

Mainactivity:

[Java]View PlainCopy
  1. Import Java.util.Iterator;
  2. Import Java.util.Set;
  3. Import android.app.Activity;
  4. Import Android.bluetooth.BluetoothAdapter;
  5. Import Android.bluetooth.BluetoothDevice;
  6. Import android.content.Intent;
  7. Import Android.os.Bundle;
  8. Import Android.view.View;
  9. Import Android.view.View.OnClickListener;
  10. Import Android.widget.Button;
  11. Public class Mainactivity extends Activity {
  12. Private button button = null;
  13. /** Called when the activity is first created. * /
  14. @Override
  15. public void OnCreate (Bundle savedinstancestate) {
  16. super.oncreate (savedinstancestate);
  17. Setcontentview (R.layout.main);
  18. Button = (button) Findviewbyid (R.id.buttonid);
  19. Button.setonclicklistener (new Onclicklistener () {
  20. @Override
  21. public void OnClick (View v) {
  22. //Get Bluetoothadapter object, this API is supported by Android 2.0
  23. Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();
  24. //adapter is not equal to NULL, indicating that this machine has a Bluetooth device
  25. if (adapter! = null) {
  26. System.out.println ("This machine has Bluetooth device!  ");
  27. //If the Bluetooth device is not turned on
  28. if (!adapter.isenabled ()) {
  29. Intent Intent = new Intent (bluetoothadapter.action_request_enable);
  30. //Request to turn on the Bluetooth device
  31. StartActivity (Intent);
  32. }
  33. //Get a collection of paired remote Bluetooth devices
  34. set<bluetoothdevice> devices = Adapter.getbondeddevices ();
  35. if (devices.size () >0) {
  36. For (iterator<bluetoothdevice> it = Devices.iterator (); It.hasnext ();) {
  37. Bluetoothdevice device = (bluetoothdevice) it.next ();
  38. //Print out the physical address of the remote Bluetooth device
  39. System.out.println (Device.getaddress ());
  40. }
  41. }else{
  42. System.out.println ("There are no remote Bluetooth devices already paired!  ");
  43. }
  44. }else{
  45. System.out.println ("This machine does not have a Bluetooth device!  ");
  46. }
  47. }
  48. });
  49. }
  50. }

Bluetooth for Android (one)-Scan paired Bluetooth devices

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.