一、簡單佈局java
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黃元浪DEMO_測試藍牙是否打開" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打開藍牙" android:id="@+id/openBlue" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> </RelativeLayout>
二、MainActivity.javaandroid
package com.example.hllive.learn; import android.app.Activity; import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ChikcBlue(); Button bt = (Button) findViewById(R.id.openBlue); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ChikcBlue(); } }); } public void ChikcBlue(){ BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter(); //支持藍牙模塊 if (blueadapter!=null){ if (blueadapter.isEnabled()){ Toast tst = Toast.makeText(MainActivity.this, "藍牙已經打開", Toast.LENGTH_SHORT); tst.show(); } else { new AlertDialog.Builder(MainActivity.this).setTitle("藍牙功能還沒有打開,是否打開藍牙") .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (turnOnBluetooth()){ Toast tst = Toast.makeText(MainActivity.this, "打開藍牙成功", Toast.LENGTH_SHORT); tst.show(); } else { Toast tst = Toast.makeText(MainActivity.this, "打開藍牙失敗!!", Toast.LENGTH_SHORT); tst.show(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 點擊「返回」後的操做,這裏不設置沒有任何操做 } }).show(); } }else{//不支持藍牙模塊 Toast tst = Toast.makeText(MainActivity.this, "該設備不支持藍牙或沒有藍牙模塊", Toast.LENGTH_SHORT); tst.show(); } } /** * 強制開啓當前 Android 設備的 Bluetooth * @return true:強制打開 Bluetooth 成功 false:強制打開 Bluetooth 失敗 */ public static boolean turnOnBluetooth() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) { return bluetoothAdapter.enable(); } return false; } }
三、在Androidmanifest.xml裏添加權限代碼app
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />