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
- Import Java.util.Iterator;
- Import Java.util.Set;
- Import android.app.Activity;
- Import Android.bluetooth.BluetoothAdapter;
- Import Android.bluetooth.BluetoothDevice;
- 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 button = null;
- /** Called when the 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 () {
- @Override
- public void OnClick (View v) {
- //Get Bluetoothadapter object, this API is supported by Android 2.0
- Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter ();
- //adapter is not equal to NULL, indicating that this machine has a Bluetooth device
- if (adapter! = null) {
- System.out.println ("This machine has Bluetooth device! ");
- //If the Bluetooth device is not turned on
- if (!adapter.isenabled ()) {
- Intent Intent = new Intent (bluetoothadapter.action_request_enable);
- //Request to turn on the Bluetooth device
- StartActivity (Intent);
- }
- //Get a collection of paired remote Bluetooth devices
- set<bluetoothdevice> devices = Adapter.getbondeddevices ();
- if (devices.size () >0) {
- For (iterator<bluetoothdevice> it = Devices.iterator (); It.hasnext ();) {
- Bluetoothdevice device = (bluetoothdevice) it.next ();
- //Print out the physical address of the remote Bluetooth device
- System.out.println (Device.getaddress ());
- }
- }else{
- System.out.println ("There are no remote Bluetooth devices already paired! ");
- }
- }else{
- System.out.println ("This machine does not have a Bluetooth device! ");
- }
- }
- });
- }
- }
Bluetooth for Android (one)-Scan paired Bluetooth devices