Bluetooth communication-enable and disable a bluetooth device

Source: Internet
Author: User

The minimum version required by Bluetooth is android2.0. Because Android simulators do not support Bluetooth, applications running Bluetooth must be tested and run on a real machine.

Bluetooth is an important short-distance wireless communication protocol and is widely used in various devices (such as mobile phones, medical care, and automobiles ). Bluetooth is a commonly used wireless communication device and has become a standard device for mobile phones.

In Android, Bluetooth-related classes and interfaces are included in the Android. Bluetooth package. The thadapter is the core class in Bluetooth,

Represents a local Bluetooth adapter device. The thadapter class allows you to perform basic Bluetooth tasks. For example, initialize the device search, query the device sets that can be matched, and use a known MAC address to initialize a gatethdevice class, create a descrithserversocket class to listen for connection requests from other devices to the local machine.

When we use Bluetooth, we first determine whether the current mobile phone has enabled Bluetooth, and then proceed accordingly.

Next let's take a look at how to turn on the bluetooth device.

1. in addition to setting the API level to at least 5, you must add the corresponding permissions. For example, you must use androidmanifest for communication. add <uses-permissionandroid: Name = "android. permission. bluetooth"
/>, And the Android. Permission. policth_admin permission is required to switch Bluetooth.

Only Bluetooth-related applications are required.

2java main code:

Package COM. example. open_local_bluetooth; import android. OS. bundle; import android. app. activity; import android. bluetooth. export thadapter; import android. content. intent; import android. view. menu; import android. widget. toast; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); blu Descrithadapter mblustmthadapter = descrithadapter. getdefaadapter adapter (); If (mblustmthadapter = NULL) {toast. maketext (this, "no Bluetooth hardware or driver is found on this machine! ", Toast. length_short). Show (); finish () ;}// enable if (! Mblustmthadapter. isenabled () {// intent that we initiate through the startactivityforresult () method will get the user's selection in the onactivityresult () callback method. For example, if the user clicks yes to enable it, // The result of result_ OK will be received. // If result_canceled is returned, the user is unwilling to enable Bluetooth intent mintent = new intent (effecthadapter. action_request_enable); startactivityforresult. permission. bluetooth_admin permission. // Mblustmthadapter. enable (); // mblustmthadapter. disable (); // disable Bluetooth }}@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubsuper. onactivityresult (requestcode, resultcode, data); If (requestcode = 1) {If (resultcode = result_ OK) {toast. maketext (this, "Bluetooth enabled", toast. length_short ). show ();} else if (resultcode = result_canceled) {toast. maketext (this, "cannot enable Bluetooth", toast. length_short ). show (); finish () ;}}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}}

3 configuration file: androdmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.open_local_bluetooth"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.BLUETOOTH"/>    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.open_local_bluetooth.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

Running result:


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.