zoukankan      html  css  js  c++  java
  • 蓝牙那些事之状态监听

        对于蓝牙状态的监听事件,此篇文章讲的是对于手机蓝牙的或者是设备自身蓝牙状态的监听,而并非是远程设备蓝牙状态的监听,当然如果要监听远程设备的蓝牙状态也不是没有办法,相对于监听自身蓝牙状态难度更大,资料页相对较少。

      如果要监听本身蓝牙状态,还是要注册广播

     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); // 不要忘了之后解除绑定    

    这里有多个过滤的条件,用户可以根据自身开发的需要加减条件以达到监听效果

    广播:

     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     };

    打印蓝牙的状态,在这里可以做相应的事件处理

     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     }

    好吧,监听事件也就写到这里了...

  • 相关阅读:
    oracle sql日期比较:
    vs 2008 过期问题
    silverlight带有复选框的列
    SQL 把一张表虚拟成两张表
    timeupdown
    ChildWindow 父窗体交互
    Debian CentOS修改时区
    如何优雅地使用命令行设置windows文件关联
    sql复制表结构,复制表内容语句
    VC6.0 中 添加/取消 块注释的Macro代码
  • 原文地址:https://www.cnblogs.com/yrhua/p/3792332.html
Copyright © 2011-2022 走看看