美文网首页
Android之Bluetooth通信-BLE(Gatt)客户端

Android之Bluetooth通信-BLE(Gatt)客户端

作者: 锄禾豆 | 来源:发表于2022-02-28 08:44 被阅读0次

    参考学习

    客户端
    https://blog.csdn.net/qq_38950819/article/details/103067487
    https://www.jianshu.com/p/3711cfbf7128
    https://blog.csdn.net/nz90921001/article/details/81259921/
    https://www.jianshu.com/p/6cb304089cc3
    https://www.jianshu.com/p/795bb0a08beb
    
    客户端+服务端
    https://blog.csdn.net/a_zhon/article/details/96166596
    https://github.com/a1anwang/okble/blob/master/README_zh.md   //代码
    
    服务端:
    https://www.cnblogs.com/Free-Thinker/p/9314775.html
    http://a1anwang.com/post-47.html
    http://a1anwang.com/post-36.html
    http://a1anwang.com/post-37.html
    
    
    charac说明:
    https://www.cnblogs.com/asam/p/8676369.html
    
    案例
    https://android.googlesource.com/platform/development/
    

    原理学习

    BLE的广播, BLE设备之所以能被手机扫描到,是因为 BLE设备一直在每隔 一段时间广播一次,这个广播里面包含很多数据
    
    MTU: 最大传输单元(MAXIMUM TRANSMISSION UNIT), 指在一个PDU (Protocol Data Unit:协议数据单元,在一个传输单元中的有效传输数据)能够传输的最大数据量(多少字节可以一次性传输到对方)
    

    案例

    源码里面的demo在哪里呢?
    development
    例如蓝牙相关
    development/samples/
    BluetoothLeGatt
    BluetoothChat
    BluetoothHDP
    
    系统服务的demo
    frameworks/base/tests
    

    重点说明
    客户端

    前言
    客户端的是通过从特定的服务(BluetoothGattService)里面获取特性(BluetoothGattCharacteristic)
    
    
    客户端
    BluetoothGatt  --- 通过此类发送消息
    BluetoothGattService  --  特定服务
    BluetoothGattCharacteristic  -- 特定字符
    BluetoothGattCallback   -- 回调监听
    
    第一个维度:
    怎么获取?
    1.我们怎么获取BluetoothGatt?
    BluetoothGatt gatt = BluetoothDevice.connectGatt(context, false , bluetoothGattCallback);
    
    2.怎么从BluetoothGatt中获取BluetoothGattService
    1)先启动发现服务:gatt.discoverServices();
    2)再从bluetoothGattCallback.onServicesDiscovered的回调方法中调用
    gatt.getServices()通过特定uuid找特定服务。例如:
            for (BluetoothGattService service : gatt.getServices()) {
                Log.d(TAG, "service uuid " + service.getUuid() + " type "+service.getType());
                if (service.getUuid().toString().equals(serviceUUID)) {//客户端默认一个uuid
                    bluetoothGattService = service;
                }
            }
            
    
    3.怎么从BluetoothGattService中获取BluetoothGattCharacteristic
    从2中找到了BluetoothGattService,就可以在通过uuid找对应的BluetoothGattCharacteristic
    例如。bluetoothGattService.getCharacteristic(uuid)
    或者
            for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) {
                Log.d(TAG, "characteristic uuid "+characteristic.getUuid()+" type "+characteristic.getProperties());
                if (characteristic.getUuid().toString().equals(readUUID)) {  //读特征
                    readCharacteristic = characteristic;
                } else if (characteristic.getUuid().toString().equals(writeUUID)) {  //写特征
                    writeCharacteristic = characteristic;
                }
            }
    
    
    第二个维度:
    目的干什么?
    我们的目的是把数据发出去,而发数据的关键是BluetoothGattCharacteristic
    发数据的工具是BluetoothGatt
    例如
    客户端主动要求的:
    BluetoothGatt.writeCharacteristic
    回调于
    bluetoothGattCallback.onCharacteristicWrite
    
    BluetoothGatt.readCharacteristic
    回调于
    bluetoothGattCallback.onCharacteristicRead
    
    
    客户端端被动接收的:
    按照以往的思路,先要设置监听对象
    BluetoothGatt.setCharacteristicNotification  
    设置需要监听的BluetoothGattCharacteristic
    
    回调于
    bluetoothGattCallback.onCharacteristicRead
    
    注意:
    如果你想监听几个,就需要设置几个BluetoothGattCharacteristic
    

    服务端 -- BluetoothGattServer

    关键类
    BluetoothGattServer   --- 发送数据的关键类
    BluetoothGattService  --  ble特定服务
    BluetoothGattCharacteristic  -- ble特定字符
    BluetoothGattServerCallback  -- 回调监听
    
    BluetoothLeAdvertiser --- 广播ble
    AdvertiseData         --- 广播所带数据
    AdvertiseSettings     --- 广播属性设置
    
    
    简单来说:
    1.如果我们需要我们的ble service被监听到,就需要时时广播
    
    2.广播需要分两步走:
    a.先广播:BluetoothLeAdvertiser.startAdvertising
    发送的广播被客户端扫描的时候接收到
    
    b.再启动服务:BluetoothGattServer.addService
    服务是我们自定义
    服务里面的属性需要BluetoothGattService.addCharacteristic进去
    这里就涉及uuid的设置了
    
    3.接下来就是客户端获取监听成功后的回调监听
    

    怎么查看bluetooth ble的address

    /data/misc/bluedroid/bt_config.conf
    

    相关文章

      网友评论

          本文标题:Android之Bluetooth通信-BLE(Gatt)客户端

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