Determine if Bluetooth is turned on and Bluetooth is turned on

Source: Internet
Author: User

1. Simple layout

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.hllive.learn.MainActivity">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Huang Yuan Wave demo_ test Bluetooth is turned on"Android:id= "@+id/textview" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Turn on Bluetooth"Android:id= "@+id/openblue"Android:layout_below= "@+id/textview"Android:layout_centerhorizontal= "true" /></Relativelayout>

2, Mainactivity.java

 PackageCom.example.hllive.learn;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.bluetooth.BluetoothAdapter;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Chikcblue (); Button BT=(Button) Findviewbyid (R.id.openblue); Bt.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {chikcblue ();    }        }); }     Public voidChikcblue () {bluetoothadapter blueadapter=Bluetoothadapter.getdefaultadapter (); //Support Bluetooth Module        if(blueadapter!=NULL){            if(blueadapter.isenabled ()) {Toast TST= Toast.maketext (mainactivity. This, "Bluetooth is turned on", Toast.length_short);            Tst.show (); }            Else {                NewAlertdialog.builder (mainactivity. This). Settitle ("Bluetooth feature is not turned on, turn Bluetooth on"). SetIcon (Android. R.drawable.ic_dialog_info). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                                if(Turnonbluetooth ()) {Toast TST= Toast.maketext (mainactivity. This, "Turn Bluetooth on successfully", Toast.length_short);                                Tst.show (); }                                Else{Toast TST= Toast.maketext (mainactivity. This, "Bluetooth failed to turn on!! ", Toast.length_short);                                Tst.show (); }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {                                //Click "Back" after the action, not set here No action}}). Show (); }        }Else{//Bluetooth module not supportedToast TST = Toast.maketext (mainactivity. This, "The device does not support Bluetooth or does not have a Bluetooth module", Toast.length_short);        Tst.show (); }    }    /*** Force the Bluetooth on the current Android device to open *@returntrue: Force open Bluetooth success false: Force open Bluetooth failed*/     Public Static BooleanTurnonbluetooth () {bluetoothadapter bluetoothadapter=bluetoothadapter. Getdefaultadapter (); if(Bluetoothadapter! =NULL)        {            returnbluetoothadapter.enable (); }        return false; }}

3. Add permission code in Androidmanifest.xml

<android:name= "Android.permission.BLUETOOTH"/>    < Android:name = "Android.permission.BLUETOOTH_ADMIN"/> 

Determine if Bluetooth is turned on and Bluetooth is turned on

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.