Android开发之蓝牙(Bluetooth)

作者: Reathin | 来源:发表于2017-05-03 23:26 被阅读485次

    Talk一下

    在上一篇中有介绍了Wifi与网络连接处理
    Android开发之WiFi与网络连接处理
    下面,来继续说说Android中蓝牙的基本使用。

    Bluetooth

    Bluetooth是目前使用的最广泛的无线通讯协议之一,主要针对短距离设备通讯(10米),常用于连接耳机、鼠标和移动通讯设备等。

    值得一提的是:
    android4.2新增了部分新功能,但是对于Bluetooth熟悉的人或许开始头疼了,那就是Android4.2引入了一个新的蓝牙协议栈针BLE。谷歌和Broadcom之间的合作,开发新的蓝牙协议栈,取代了基于堆栈的Bluez。因此市场上出现了老设备的兼容问题,很多蓝牙设备在android4.2手机上不能正常使用。

    BluetoothAdapter

    BluetoothAdapter简单点来说就是代表了本设备(手机、电脑等)的蓝牙适配器对象。

    功能

    • 开关蓝牙设备
    • 扫描蓝牙设备
    • 设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等;

    first:we need permission
    要操作蓝牙,先要在AndroidManifest.xml里加入权限

    <uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permissionandroid:name="android.permission.BLUETOOTH" />
    

    **下面来看看如何使用蓝牙。 **↓↓↓****
    Demo已就绪:


    使用

    第一步:

    public static synchronized BluetoothAdapter getDefaultAdapter ()
    

    返回值:如果设备具备蓝牙功能,返回BluetoothAdapter 实例;否则,返回null对象。

    第二步:

    打开蓝牙设备的方式:
    1.直接调用函数enable()去打开蓝牙设备 ;
    2.系统API去打开蓝牙设备,该方式会弹出一个对话框样式的Activity供用户选择是否打开蓝牙设备。

    if (!mAdapter.isEnabled())
    {
         Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
         startActivity(intent);
         mAdapter.enable();
         return;
    }
    

    注意: 1.如果蓝牙已经开启,不会弹出该Activity界面。2.在目前大多数Android手机中,是不支持在飞行模式下开启蓝牙的。如果蓝牙已经开启,那么蓝牙的开关 ,状态会随着飞行模式的状态而发生改变。

    第三步:

    1. 搜索蓝牙设备
    使用BluetoothAdapter的startDiscovery()方法来搜索蓝牙设备
    startDiscovery()方法是一个异步方法,调用后会立即返回。该方法会进行对其他蓝牙设备的搜索,该过程会持续12秒。该方法调用后,搜索过程实际上是在一个System Service中进行的,所以可以调用cancelDiscovery()方法来停止搜索(该方法可以在未执行discovery请求时调用)。

    系统开始搜索蓝牙设备
    ^( *  ̄(oo) ̄ ) ^ 系统会发送以下三个广播:

    • BluetoothAdapter.ACTION_DISCOVERY_START:开始搜索
    • BluetoothAdapter.ACTION_DISCOVERY_FINISHED:搜索结束
    • BluetoothDevice.ACTION_FOUND:找到设备
      这个Intent中包含两个extra fields:EXTRA_DEVICE和EXTRA_CLASS,分别包含BluetooDevice和BluetoothClass

    2.扫描设备

    if (mAdapter.isDiscovering())
        {
          mAdapter.cancelDiscovery();
        }
        else
        {
          // 开启发现蓝牙设备
          mAdapter.startDiscovery();
        }
    

    3.定义广播接收器接收搜索结果

    private BroadcastReceiver mReceiver = new BroadcastReceiver()
      {
        @Override
        public void onReceive(Context context, Intent intent)
        {
          String action = intent.getAction();
    
          if (action.equals(BluetoothDevice.ACTION_FOUND))
          {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED)
            {
              list.add(device);
            }
            mMyBluetoothAdapter.notifyDataSetChanged();
          }
          else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
          {
            setTitle("搜索蓝牙设备");
          }
        }
      };
    

    4.注册广播

    // 注册用以接收到已搜索到的蓝牙设备的receiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver,filter);
    
    // 注册搜索完时的receiver
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver,filter);
    

    获取附近的蓝牙设备

    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    list.add(device);
    adapter.notifyDataSetChanged();
    

    第四步:蓝牙连接

    第一步建立连接:首先Android sdk(2.0以上版本)支持的蓝牙连接是通过BluetoothSocket建立连接,服务端BluetoothServerSocket和客户端(BluetoothSocket)需指定同样的UUID,才能建立连接,因为建立连接的方法会阻塞线程,所以服务器端和客户端都应启动新线程连接。

    (这里的服务端和客户端是相对来说的)
    两个蓝牙设备之间的连接,则必须实现服务端与客户端的机制。
    当两个设备在同一个RFCOMM channel下分别拥有一个连接的BluetoothSocket,这两个设备才可以说是建立了连接。

    服务端设备与客户端设备获取BluetoothSocket的途径是不同的。
    1,服务端设备是通过accepted一个incoming connection来获取的,
    2,客户端设备则是通过打开一个到服务端的RFCOMM channel来获取的。

    服务端
    通过调用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务端之间的配对)

    BluetoothServerSocket serverSocket = mAdapter.listenUsingRfcommWithServiceRecord(serverSocketName,UUID);
    serverSocket.accept();
    
    private class AcceptThread extends Thread {       
          
        private final BluetoothServerSocket mmServerSocket;        
          
        public AcceptThread() {       
            BluetoothServerSocket tmp = null;       
            try {       
                // MY_UUID 是应用对应设备中得蓝牙的UUID       
                tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);       
            } catch (IOException e) { }        
            mmServerSocket = tmp;        
        }       
           
        public void run() {        
            BluetoothSocket socket = null;        
          
            while (true) {        
                try {        
                    socket = mmServerSocket.accept();       
                } catch (IOException e) {        
                    break;       
                }       
          
                // 当连接被接收       
                if (socket != null) {      
                    // 管理接收的客户端      
                    // 管理逻辑...        
                    socket.close();        
                    break;       
                }       
            }        
        }         
          
        // 取消监听客户端请求       
        public void cancel() {        
            try {        
                mmServerSocket.close();        
            } catch (IOException e) { }        
        }        
    }
    

    客户端
    调用BluetoothService的createRfcommSocketToServiceRecord(UUID)方法获取BluetoothSocket(该UUID应该同于服务端的UUID)。
    调用BluetoothSocket的connect()方法(该方法为block方法),如果UUID同服务端的UUID匹配,并且连接被服务端accept,则connect()方法返回。

        private class ConnectThread extends Thread {    
            private final BluetoothSocket mmSocket;       
              
            private final BluetoothDevice mmDevice;    
            public ConnectThread(BluetoothDevice device) {       
              
                BluetoothSocket tmp = null;       
                mmDevice = device;    
                try {      
                    // MY_UUID 是服务端对应的UUID
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);     
                } catch (IOException e) { }        
                mmSocket = tmp;       
            }       
              
            public void run() {       
                mBluetoothAdapter.cancelDiscovery();       
                try {      
                    mmSocket.connect();        
                } catch (IOException connectException) {       
                    try {        
                        mmSocket.close();       
                    } catch (IOException closeException) { }       
                     return;       
                }       
               // 处理连接逻辑...
            }        
              
            public void cancel() {       
                try {        
                    mmSocket.close();       
                } catch (IOException e) { }       
             }        
        }   
    

    数据传递,通过以上操作,就已经建立的BluetoothSocket连接了,数据传递无非是通过流的形式
    获取流

    inputStream = socket.getInputStream();
    outputStream = socket.getOutputStream();
    

    BluethoothDevice

    该类就是关于远程蓝牙设备的一个描述。通过它可以和本地蓝牙设备---BluetoothAdapter连接通信。

    好多东西我也不知道怎么描述,下面给出Demo:
    刚好有刚学习的小伙伴问我ListView怎么用,那我就用ListView。

    源码:
    RairDemo
    GitHub:https://github.com/Rairmmd/RairDemo/tree/master/BluetoothDemo
    Coding:https://coding.net/u/Rair/p/RairDemo/git

    相关文章

      网友评论

      本文标题:Android开发之蓝牙(Bluetooth)

      本文链接:https://www.haomeiwen.com/subject/uwaltxtx.html