Android 蓝牙难点总结

Android 蓝牙4.0难点总结

基础请看蓝牙官方文档https://developer.android.google.cn/guide/topics/connectivity/bluetooth.html

//初始化ble设配器

private void initBle() {

        BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

        bluetoothAdapter = manager.getAdapter();

    }

// 实现扫描回调接口

implements BluetoothAdapter.LeScanCallback

两种方式本身实现或new 一个新class

扫描回调

bluetoothAdapter.startLeScan(this/leScanCallback)
// 扫描到新设备时,会回调该接口。

public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {}

返回的有蓝牙设备、无线接收信号强度(RSSI)、广播包

byte[] scanRecord数据如下图:

可根据 https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/ /蓝牙官网协议文档进行解析

如果知道要扫描的设备的serviceUUID 可以通过Android sdk 自身的方法进行扫描。

public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) {

// 连接蓝牙设备,device为之前扫描得到的

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

使用BluetoothGattCallback 来跟设备进行通信

低功耗蓝牙(BLE)设备的通信基本协议是 GATT, 要操作 BLE 设备,第一步就是要连接设备,其实就是连接 BLE 设备上的 GATT service。

// 实现连接回调接口[关键]

class BleGattCallback extends BluetoothGattCallback {

会继承以下几个关键方法

// 连接状态改变(连接成功或失败)时回调该接口

mBluetoothGatt是连接完成时的对象,调用这句后,会走回调函数的onServicesDiscovered方法。在这个方法中去获取设备的一些服务,蓝牙通道,然后通过这些通道去发送数据给外设。

在蓝牙设备中, 其包含有多个BluetoothGattService, 而每个BluetoothGattService中又包含有多个BluetoothGattCharacteristic。

获取到设备中的服务列表  mBluetoothGatt.getServices(); 或者通过uuid 来获取某一个服务。

通过Gatt这个对像,就是蓝牙连接完成后获取到的对象,通过这个对象设置好指定的通道向设备中写入和读取数据。

    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {}

        // status 用于返回操作是否成功,会返回异常码。

        // newState 返回连接状态,

public void onServicesDiscovered(BluetoothGatt gatt, int status) {}

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

//数据在这里接收

final byte[] data = characteristic.getValue();

根据蓝牙协议进行数据解析

}

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {}

public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {}

public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {}

做通信要已知设备的通信协议,Service_UUID,Characteristic_UUID_WRITE,Characteristic_UUID_NOTIFY,如果多通道的话,要监听所有通道,否则数据丢失,没办法对接收到的数据进行解析。

发送指令:监听UUID_WRITE,

BluetoothGattCharacteristic write = gattService

                .getCharacteristic(Characteristic_UUID_WRITE);

将byte[]写入特征

  write.setValue(value);

通过gatt传递写特征gatt.writeCharacteristic(write);

Android 蓝牙5.0难点

BluetoothLeScanner 蓝牙适配器里面的扫描器,用于扫描BLE外设。

扫描 mBluetoothLeScanner.startScan(this); 

public void onScanResult(int callbackType, ScanResult result) {}// 扫描到新设备时,会回调该接口。

蓝牙5.0 这个方法相对4.0将数据封装

这个实习最大的收获Android 蓝牙开发,在实习的过程中,我可以独立编写蓝牙数据的封装sdk,实习的过程就是不断学习,我真的,没有相对我我自己在这六个月的实习过程中,可以独立开发一个项目,变成更好的自己,做开发本身也是一个不断学习的过程,学无止境。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南:-)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值