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