The following describes how to use Bluetooth in Android.
1. Import related packages:
Import Android. Bluetooth .*;
2. Set permissions.
<Uses-Permission Android: Name = "android. Permission. Bluetooth"/>
If you want more advanced user permission settings
<Uses-Permission Android: Name = "android. Permission. effecth_admin"/>
3. Android has a javasthadapter Singleton class, which must be used first, that is
Descrithadapter Bluetooth = descrithadapter. getdefaadapter adapter ();
Then determine whether the device supports Bluetooth.
Descrithadapter Bluetooth = descrithadapter. getdefaadapter adapter (); If (Bluetooth! = NULL ){}
If not null
Determine whether the user has enabled the Bluetooth function:
If (Bluetooth. isenabled () {} else {}
4. Next, a user is displayed. If Bluetooth is enabled, the name and status of the bluetooth device are displayed.
If the bluetooth device is not enabled, notify the user
String status; If (Bluetooth. isenabled () {string mydeviceaddress = Bluetooth. getaddress (); string mydevicename = Bluetooth. getname (); status = mydevicename + ":" + mydeviceaddress;} else {status = "Bluetooth is not enabled. ";} toast. maketext (this, status, toast. length_long ). show ();
Getname () is used to obtain the device name.
<Uses-Permission Android: Name = "android. Permission. effecth_admin"/>
You can set the bluetooth device name as follows:
Bluetooth. setname ("androidcoder ");
5. display the Bluetooth status:
String state = Bluetooth. getstate (); status = mydevicename + ":" + mydeviceaddress + ":" + state;
The Bluetooth status includes:
State_turning_on
State_on
State_turning_off
State_off