You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中BLE Indication失效(Notification正常)问题求助

BLE设备Indication模式无法触发onCharacteristicChanged的问题

我正在开发一个与BLE设备交互的项目,使用Notification时数据接收正常、功能一切正常,但切换为Indication后,程序完全无法进入onCharacteristicChanged方法。

开启Indication/Notification的代码

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {

        Log.d(TAG, "I was in notificator");

        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(getString(R.string.CLIENT_CHARACTERISTIC_CONFIG)));

        int properties_local = characteristic.getProperties();
        if (Utils.hasIndicateProperty(properties_local) != 0) {
            Log.d(TAG, "Indication on this characteristic");
        }
        if (Utils.hasNotifyProperty(properties_local) != 0) {
            Log.d(TAG, "Notification on this characteristic");
        }
        else Log.d(TAG, "Notification are NOT on this characteristic");
        byte [] indi = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
        byte [] noti = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;

        if (Activity_BTLE_Services.SWITCHED == 0) {
            if (enabled) {
                boolean a = descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                Log.d(TAG, "ENABLE INDICATION " + a);
            }
            else {
                descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            }
        }

        else {
            if (enabled) {
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            }

            else {
                descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            }
        }

        boolean answer = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        Log.d(TAG, String.valueOf(answer));

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Log.d(TAG, "To short time");
        }

        /*try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Log.d(TAG, "To short time");
        }*/

        mBluetoothGatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    }

后续操作代码(切换模式后发送指令)

else {
                    BluetoothGattCharacteristic characteristic_fourth = characteristics_ArrayList.get(2);
                    BluetoothGattCharacteristic characteristic_fifth = characteristics_ArrayList.get(3);
                    btn_scan = findViewById(R.id.btn_scan);
                    image_view = findViewById(R.id.imageView4);
                    GraphView graph = (GraphView) (findViewById(R.id.graph));
                    graph.removeAllSeries();
                    list.clear();
                    mBTLE_Service.setCharacteristicNotification(characteristic_fifth, true);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "To short time");
                    }
                    byte[] data_local = {11, 0};
                    characteristic_fourth.setValue(data_local);
                    mBTLE_Service.writeCharacteristic(characteristic_fourth);
                    btn_scan.setImageResource(R.drawable.baseline_pause_24);
                    image_view.setVisibility(View.VISIBLE);
                }

补充说明

在NRF Connect工具中测试该设备的Indication功能是正常的,可以正常接收数据。

排查与修复要点

  • 写Descriptor参数错误:代码最后一行固定传入BluetoothGattDescriptor.ENABLE_INDICATION_VALUE,不管之前配置的是Notification还是Indication的value,这会导致配置不匹配。应改为写入descriptor已设置好的value:
    // 修复前
    mBluetoothGatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    // 修复后
    mBluetoothGatt.writeDescriptor(descriptor, descriptor.getValue());
    
  • 移除主线程Sleep:setCharacteristicNotification里的Thread.sleep(10000)会阻塞主线程,引发ANR同时打乱BLE异步回调时序,必须移除。改用onDescriptorWrite回调来触发后续逻辑(比如发送设备指令)。
  • 确认Indication属性:确保目标特征确实支持Indication,检查日志是否输出"Indication on this characteristic"。
  • 设备端确认机制:Indication需要设备收到ACK后才会继续发送,确保设备端正确处理了Indication的确认流程。

内容的提问来源于stack exchange,提问作者Даниил Белковский

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:23:13