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:
[Java]
Import java. util. Iterator;
Import java. util. Set;
Import android. app. Activity;
Import android. bluetooth. javasthadapter;
Import android. bluetooth. systthdevice;
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 ){
// Obtain the thadapter object, which is supported by android 2.0.
Descrithadapter adapter = descrithadapter. getdefaadapter adapter ();
// If the adapter is not null, the local device 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 intent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE );
// Request to enable the bluetooth device
StartActivity (intent );
}
// Obtain a set of paired remote Bluetooth devices
Set <effecthdevice> devices = adapter. getBondedDevices ();
If (devices. size ()> 0 ){
For (Iterator <effecthdevice> it = devices. iterator (); it. hasNext ();){
Effecthdevice device = (effecthdevice) 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 ("there is no Bluetooth device on this machine! ");
}
}
});
}
}
Import java. util. Iterator;
Import java. util. Set;
Import android. app. Activity;
Import android. bluetooth. javasthadapter;
Import android. bluetooth. systthdevice;
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 ){
// Obtain the thadapter object, which is supported by android 2.0.
Descrithadapter adapter = descrithadapter. getdefaadapter adapter ();
// If the adapter is not null, the local device 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 intent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE );
// Request to enable the bluetooth device
StartActivity (intent );
}
// Obtain a set of paired remote Bluetooth devices
Set <effecthdevice> devices = adapter. getBondedDevices ();
If (devices. size ()> 0 ){
For (Iterator <effecthdevice> it = devices. iterator (); it. hasNext ();){
Effecthdevice device = (effecthdevice) 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 ("there is no Bluetooth device on this machine! ");
}
}
});
}
}
Author: t12x3456