蓝牙4.0总结

搞了一段时间的蓝牙,现在闲了下来,总结一下具体的流程。沉淀一下
接下来的流程中会涉及到一下几个类,现在提前说明一下作用

BluetoothManager 蓝牙管理类,用于获取蓝牙适配器,蓝牙连接状态
BluetoothAdapter 扫描,获取蓝牙设备的类
BluetoothGatt 管理远程蓝牙设备内部的service,特征值
BluetoothDevice 蓝牙设备的封装类,用于获取address,连接等作用
BluetoothAdapter.LeScanCallback 扫描设备的回调
BluetoothGattCallback 回调,蓝牙设备各种数据和状态回调
BluetoothGattService 跑在远程蓝牙设备中的服务
BluetoothGattCharacteristic 特征值 用于标识可读,,可写,可发通知等属性
BluetoothGattDescriptor 蓝牙协议中的Descriptor
  1. :获取设备,,

    • 获取BluetoothAdapter

      • 两种方式:BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
      • BluetoothAdapter.getDefaultAdapter();
    • 使用BluetoothAdapter扫描设备,这里需要传入LeScanCallback 用于接收扫描到的设备

    public boolean startLeScan(LeScanCallback callback) { return startLeScan(null, callback); }

    • 在LeScanCallback中可以获取到设备 BluetoothDevice 这个类封装了关于远程蓝牙设备的信息,有mac地址,设备名字,设备别名等信息,最重要的适用于手机和蓝牙设备建立连接
  2. 连接设备

    • 在这个方法中需要最重要的是传入BluetoothGattCallback,这个回调用于接受蓝牙设备的连接状态和数据,连接成功后会返回一个BluetoothGatt 获取service Characteristic Descriptor

        public BluetoothGatt connectGatt(Context context, boolean autoConnect,
            BluetoothGattCallback callback) {
                    return (connectGatt(context, autoConnect,callback, TRANSPORT_AUTO));
                }
      
    • 做完以上的步骤后连接蓝牙已经完成了,剩下的是监听和读写数据了

  3. 监听数据 上面提到BluetoothGattCallback 这个回调,用于接受蓝牙设备的连接状态和数据,挑几个比较常用的方法来说一下

    public abstract class BluetoothGattCallback {
    
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                        int newState) {
    //连接状态改变,系统会调用这个方法,基本套路是这样子的,
    调用mBluetoothGatt.discoverServices() 
    这个方法后就会回调onServicesDiscovered,如果不调用的话你就会痛苦的思考为什么 
    下面这个回调不走。
        if (newState == BluetoothProfile.STATE_CONNECTED) {    
             mBluetoothGatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
           
        }
    }
    
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
            //用于发现service,BluetoothGattCharacteristic ,BluetoothGattDescriptor 这三个对象是监听数据的关键,决定你能不能读写数据和接收通知
            1.获取硬件商提供好的服务
             BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);                
            2.在服务中获取特征值:
             BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
            3.在特征值中获取描述
             BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);

            上面的三个对象的获取都是靠UUID来的,蓝牙协议已经定义好一些UUID,
            硬件厂商也会根据需要定义自己的UUID。
            UUID相当于一个标示符,用于获取对象的数据或者对象,可以自行百度一下UUID
        } else {
           
        }
    }
    
    
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                                     int status) {
        //当远程蓝牙设备通过特征值来发送数据给手机的时候会回调,这里就是真正获取数据的地方之一,需要注意的,
        characteristic传递的是二进制数据,需要进行转换的。还有个很坑的地方,这里我觉得并非运行在主线程的,大家可以验证一下
    }
    
     
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic, int status) {
    // 写数据的回调   
    }
    
    
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {

    //当远程蓝牙设备通过特征值来发送数据给手机的时候会回调,这里就是真正获取数据的地方之一,
    }
  1. 总结:因为目前公司的手环只用到了读取数据的部分,写数据部分还没用到。等用到了再补一下,而且目前也是浅显用了一下蓝牙,希望接下来有更多的机会可以用到蓝牙,增加经验。
    原文作者:三少爷的码
    原文地址: https://www.jianshu.com/p/5ebff5ac2907
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞