Steps for using Bluetooth in Android

Source: Internet
Author: User
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

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.