Android藍牙----打開,關閉操作


① 我們先在AndroidManifest里面增加我們的Bluetooth權限

<uses-permission android:name="android.permission.BLUETOOTH"/>  //使用藍牙所需要的權限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> //使用掃描和設置藍牙的權限(申明這一個權限必須申明上面一個權限)

② 寫個簡單的布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.administrator.bluetoothdemo.MainActivity">
 8 
 9 
10     <Button
11         android:id="@+id/Btn1"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:layout_alignParentTop="true"
15         android:layout_centerHorizontal="true"
16         android:layout_marginTop="53dp"
17         android:text="open Bluetooth"
18         android:textAllCaps="false"
19         />
20     <Button
21         android:id="@+id/Btn2"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_alignParentTop="true"
25         android:layout_centerHorizontal="true"
26         android:layout_marginTop="100dp"
27         android:text="clase Bluetooth"
28         android:textAllCaps="false"
29         />
30 </RelativeLayout>

③ 接下來寫MainActivity類的代碼

 1 package com.example.administrator.bluetoothdemo;
 2 
 3 import android.bluetooth.BluetoothAdapter;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.Toast;
 9 
10 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
11 
12     private Button mBtn,mBtn1;
13     private BluetoothAdapter mBluetooth;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         findView();   //查找控件
19         mBtn.setOnClickListener(this);  //創建點擊事件
20         mBtn1.setOnClickListener(this);
21     }
22     public void onClick(View v)
23     {
24         switch(v.getId())
25         {
26             case R.id.Btn1:
27             {
28                 if(!mBluetooth.isEnabled())
29                 {
30                         mBluetooth.enable();
31                 }
32                 Toast.makeText(MainActivity.this,"打開成功", Toast.LENGTH_SHORT).show();
33             }break;
34 
35             case R.id.Btn2:
36             {
37                 if(mBluetooth.isEnabled())
38                 {
39                         mBluetooth.disable();
40                 }
41                 Toast.makeText(MainActivity.this,"關閉成功", Toast.LENGTH_SHORT).show();
42             }break;
43         }
44     }
45     public void findView()
46     {
47                 mBtn = (Button)findViewById(R.id.Btn1);
48                 mBtn1 = (Button) findViewById(R.id.Btn2);
49                 mBluetooth = BluetoothAdapter.getDefaultAdapter(); //獲取Bluetooth適配器
50     }
51 }

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM