藍牙那些事之狀態監聽

    對於藍牙狀態的監聽事件,此篇文章講的是對於手機藍牙的或者是設備自身藍牙狀態的監聽,而並不是是遠程設備藍牙狀態的監聽,固然若是要監聽遠程設備的藍牙狀態也不是沒有辦法,相對於監聽自身藍牙狀態難度更大,資料頁相對較少。ide

  若是要監聽自己藍牙狀態,仍是要註冊廣播this

 1     //註冊廣播接收器(監聽藍牙狀態的改變)
 2         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
 3         
 4         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);   
 5 //      filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//藍牙掃描狀態(SCAN_MODE)發生改變
 6 
 7 //      filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //指明一個遠程設備的鏈接狀態的改變。好比,當一個設備已經被匹配。   
 8         filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//指明一個與遠程設備創建的低級別(ACL)鏈接。
 9         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//指明一個來自於遠程設備的低級別(ACL)鏈接的斷開
10         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);//指明一個爲遠程設備提出的低級別(ACL)的斷開鏈接請求,並即將斷開鏈接。
11         
12 //      filter.addAction(BluetoothDevice.ACTION_FOUND);//發現遠程設備 
13 //      filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//本地藍牙適配器已經開始對遠程設備的搜尋過程。
14         this.registerReceiver(BluetoothReciever, filter); // 不要忘了以後解除綁定    

這裏有多個過濾的條件,用戶能夠根據自身開發的須要加減條件以達到監聽效果spa

廣播:code

 1 //藍牙狀態監聽
 2     private BroadcastReceiver BluetoothReciever = new BroadcastReceiver() {
 3           @Override
 4           public void onReceive(Context context, Intent intent) {
 5               System.out.println("==========此時藍牙的狀態是====11===="+intent.getAction());
 6               if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
 7                   int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_OFF);
 8                   System.out.println("==========此時藍牙的狀態是====22===="+btState);
 9                   //打印藍牙的狀態
10                   printBTState(btState);
11               }else if(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(intent.getAction())){
12                   Utils.getinstance().getStr(MainActivity.this, "藍牙已斷開");
13                   Intent in=new Intent();
14                   in.setAction("com.healthys.blue");
15                   in.putExtra("tizhong", "0123456");
16                   sendBroadcast(in);
17                   try {
18                       //設置兩秒後自動回連
19                     Thread.sleep(2000);
20                     BlueToothHelper.getinstance().getClient(null,Myfinal.btpath,1234);
21                 } catch (Exception e) {
22                     e.printStackTrace();
23                 }
24               }else if(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(intent.getAction())){
25                   Utils.getinstance().getStr(MainActivity.this, "藍牙即將斷開");
26               }
27           }
28     };

打印藍牙的狀態,在這裏能夠作相應的事件處理blog

 1 //打印藍牙的狀態
 2     private void printBTState(int btState) {
 3       switch (btState) {
 4           case BluetoothAdapter.STATE_OFF:
 5               System.out.println("============藍牙狀態:已關閉==========="+btState);
 6               Utils.getinstance().getStr(MainActivity.this, "藍牙已關閉");
 7               Myfinal.isconnection=false;
 8               Intent in=new Intent();
 9               in.setAction("com.healthys.blue");
10               in.putExtra("tizhong", "0123456");
11               sendBroadcast(in);
12               break;
13           case BluetoothAdapter.STATE_TURNING_OFF:
14                 System.out.println("========藍牙狀態:正在關閉=============="+btState);
15               break;
16           case BluetoothAdapter.STATE_TURNING_ON:
17               System.out.println("=====藍牙狀態:正在打開======"+btState);//當藍牙打開後自動鏈接設備
18               break;
19           case BluetoothAdapter.STATE_ON:
20               System.out.println("=========藍牙狀態:已打開========="+btState);
21 
22               Utils.getinstance().getStr(MainActivity.this, "藍牙已打開");
23               if(Myfinal.btpath!=null&&!Myfinal.btpath.equals("")){
24                   BlueToothHelper.getinstance().getClient(null,Myfinal.btpath,1234);
25               }
26               break;
27           default:
28               break;
29       }
30     }

解除註冊事件:事件

1 //解除註冊
2     @Override
3     protected void onDestroy() {
4         super.onDestroy();
5         unregisterReceiver(BluetoothReciever);
6         unregisterReceiver(loginReceiver);
7     }

 

好吧,監聽事件也就寫到這裏了...ci

相關文章
相關標籤/搜索