BLE 低功耗蓝牙开发

开发步骤:

1.权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-feature
    android:name="android.hardware.audio.low_latency"
    android:required="true" />
<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true"/>

缺少权限不一定会造成启动失败,但是当执行扫描或是连接的时候一定会失败。

开发遇到的问题:在开发时遗漏了一个权限,蓝牙启动成功了,但是connect 连接方法一直没有结果,也不报错就是没有结果,后来检查到缺少了一个权限,抱着试试看的想法补充了权限才连接成功。

2.获取adapter检测蓝牙是否可用:

获取 bluetoothmanager : context.getSystemService(Context.BLUETOOTH_SERVICE) 
获取 adapter : bluetoothManager.adapter 
检测 adapter.isEnabled   蓝牙是否可用

3.扫描周围蓝牙设备:

adapter.bluetoothLeScanner.startScan 扫描周围的低功耗蓝牙设备

开发遇到问题:startScan 方法包含三个参数,其中 callback 参数 与 stopScan 停止扫描方法必须使用同一个对象,这样才能知道你执行关闭的是哪个扫描方法。

4.连接蓝牙:

device.connectGatt():
 device 是扫描之后获得的结果 scanresult 中的设备对象是一组16进制数的MAC地址;
connectGatt()有三个参数:
    BluetoothGattCallback:是连接之后的回调,包括之后的读写回调都是这个对象返回的;
device.connectGatt(context, false, object : BluetoothGattCallback() {

    override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
        super.onConnectionStateChange(gatt, status, newState)
        checkPermission()
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            gatt?.discoverServices()//连接建立 进行服务发现
        } else if (newState == BluetoothProfile.STATE_CONNECTING) {

        } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

        }

    }

    override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
        super.onServicesDiscovered(gatt, status)
          //服务发现成功   获取 gatt
    }

    override fun onCharacteristicWrite(
        gatt: BluetoothGatt?,
        characteristic: BluetoothGattCharacteristic?,
        status: Int
    ) {
        super.onCharacteristicWrite(gatt, characteristic, status)

    }

    override fun onCharacteristicChanged(
        gatt: BluetoothGatt?,
        characteristic: BluetoothGattCharacteristic?
    ) {
        super.onCharacteristicChanged(gatt, characteristic)
        
    }
}, BluetoothDevice.TRANSPORT_LE)

开发遇到问题:1. callback方法是异步,gatt获取必须是在服务发现成功之后,返回值都是异步的;

                         2. 方法和回调函数要对应,比如 gatt.writeDescriptor(descriptor)是过时的方法对应的是

override fun onCharacteristicChanged(
    gatt: BluetoothGatt?,
    characteristic: BluetoothGattCharacteristic?
) 

这个函数,如果换成

override fun onCharacteristicChanged(
    gatt: BluetoothGatt,
    characteristic: BluetoothGattCharacteristic,
    value: ByteArray
) 

会收不到返回值;

5.读取

UUID:硬件设备服务的识别码,这是生产生写入的,有三种,serviceUUID、charactericUUID、descriptorUUID,这个是开发硬件设备的生产商提供的,比如我开发的这个蓝牙电路板是我通过对方提供的文档找到的,额外讲一下,这么重要的东西应该放在明显易读的地方结果他们还藏着就是不想让你顺顺利利的找到不知道他们怎么想的;

根据UUID  

gatt.getService(UUID.fromString(serviceUUIDStr))
serviceR.getCharacteristic(UUID.fromString(characteristicReadUUIDStr))

然后你要确定,特征值 characteristic 是否可读

characteristic.properties  特征值的属性是一个INT数值,

我开发这个板子不可读是利用的notify来获取蓝牙传出值的,

val isNotify = gatt.setCharacteristicNotification(characteristic, true)
val descriptor =
    characteristic.getDescriptor(UUID.fromString(descriptorNotifyUUIDStr))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
val isDescriptorWrite = gatt.writeDescriptor(descriptor)

开发遇到的问题:

1.UUID不对导致开启notify一直失败,供货方提供文档写的UUID不清晰,后来轮询输出每个特征值characteristic和特征值的descriptor,有的characteristic是没有descriptor的这个不能用,然后看他们的UUID,把有descriptor的characteristic都试了一遍才找到正确的

2.无法获取蓝牙传送的数据,这个是上面提到的gatt.writeDescriptor方法过时要用匹配的过时的callback重载函数

3.调用的时候notify的设置要在蓝牙写入方法的前面,不知道是不是特例,我试了后调用notify设置会失败

6.写入:

 val serviceR = gatt.getService(UUID.fromString(serviceUUIDStr))
 val characteristic =           serviceR.getCharacteristic(UUID.fromString(characteristicWriteUUIDStr))
 characteristic?.value = writeContent.toByteArray()
 val writeResult = gatt.writeCharacteristic(characteristic)

开发遇到问题:

           还是UUID,如果文档没有清楚的交代那只能一个一个是试;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值