"Turn" Bluetooth communication-turn Bluetooth devices on and off

Source: Internet
Author: User

Original URL: http://www.2cto.com/kf/201303/199175.html

The minimum Bluetooth required version is android2.0, and since the Android emulator does not support Bluetooth, apps running Bluetooth must be tested on the real machine.

Bluetooth is an important short-range wireless communication protocol, widely used in a variety of devices (mobile phones, medical, automotive, etc.). Bluetooth is a relatively common wireless communication equipment, early research into the standard of mobile phones.

In Android, Bluetooth-related classes and interfaces are in the Android.bluetooth package. Where Bluetoothadapter is the core class in Bluetooth,

Represents a local Bluetooth adapter device. The Bluetoothadapter class allows users to perform basic Bluetooth tasks. For example: Initialize a device's search, query a matching set of devices, initialize a Bluetoothdevice class with a known MAC address, create a Bluetoothserversocket class to listen for other devices to the local connection request, and so on.

When we use Bluetooth, we will first determine whether the current phone has Bluetooth turned on, and then do the appropriate processing.

Let's see how to turn on the Bluetooth device.

1. In addition to the need to consider the API level at least 5, we should also pay attention to add the appropriate permissions, such as the use of communication needs to join in Androidmanifest.xml, and switch Bluetooth needs android.permission.BLUETOOTH_ Admin permissions.

As long as it is about Bluetooth applications, these two are not few.

2JAVA Main code:


[Java]
Package Com.example.open_local_bluetooth;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.bluetooth.BluetoothAdapter;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.widget.Toast;

public class Mainactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Bluetoothadapter Mbluetoothadapter = Bluetoothadapter
. Getdefaultadapter ();
if (Mbluetoothadapter = = null) {
Toast.maketext (This, "the machine does not find Bluetooth hardware or drivers! ", Toast.length_short). Show ();
Finish ();
}
If local Bluetooth is not turned on, turn on
if (!mbluetoothadapter.isenabled ()) {
The intent that we initiated through the Startactivityforresult () method will get the user's choice in the Onactivityresult () callback method, such as when the user clicks Yes to open the
Then you will receive the results of RESULT_OK,
If result_canceled on behalf of the user does not want to turn on Bluetooth
Intent mintent = new Intent (bluetoothadapter.action_request_enable);
Startactivityforresult (mintent, 1);
Use the Enable () method to turn on, without asking the user (affordable and silent to turn on the Bluetooth device), then need to use the Android.permission.BLUETOOTH_ADMIN permissions.
Mbluetoothadapter.enable ();
Mbluetoothadapter.disable ();//Turn off Bluetooth
}
}

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = 1) {
if (ResultCode = = RESULT_OK) {
Toast.maketext (This, "Bluetooth is turned on", Toast.length_short). Show ();
} else if (ResultCode = = result_canceled) {
Toast.maketext (This, "Do not allow Bluetooth on", Toast.length_short). Show ();
Finish ();
}
}

}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}

Package Com.example.open_local_bluetooth;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.bluetooth.BluetoothAdapter;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.widget.Toast;

public class Mainactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

  bluetoothadapter mbluetoothadapter = Bluetoothadapter
    . Getdefaultadapter ();
  if (Mbluetoothadapter = = null) {
   toast.maketext (this, "the computer does not find Bluetooth hardware or drivers! ", Toast.length_short). Show ();
   finish ();
  }
  //If local Bluetooth is not turned on, then turn on
  if (!mbluetoothadapter.isenabled ()) {
   // The intent that we initiated through the Startactivityforresult () method will get the user's choice in the Onactivityresult () callback method, such as when the user clicks Yes to open the
   // Then you will receive the result of the RESULT_OK,
   //if result_canceled on behalf of the user is unwilling to turn on Bluetooth
   intent mintent = New Intent (bluetoothadapter.action_request_enable);
   startactivityforresult (mintent, 1); The
   //is turned on using the Enable () method, without having to ask the user (an affordable and silent Bluetooth enabled device), which requires Android.permission.BLUETOOTH_ADMIN permissions.
   //mbluetoothadapter.enable ();
   //mbluetoothadapter.disable ();//Turn off Bluetooth
  }
 

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = 1) {
if (ResultCode = = RESULT_OK) {
Toast.maketext (This, "Bluetooth is turned on", Toast.length_short). Show ();
} else if (ResultCode = = result_canceled) {
Toast.maketext (This, "Do not allow Bluetooth on", Toast.length_short). Show ();
Finish ();
}
}

}

@Override
public boolean Oncreateoptionsmenu (Menu 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


[HTML]

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= "/>"



<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" >









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= "/>"

<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" >

"Turn" Bluetooth communication-turn Bluetooth devices on and off

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.