【问题标题】:android 4.3 Bluetooth ble don't called onCharacteristicRead()android 4.3 蓝牙 ble 不调用 onCharacteristicRead()
【发布时间】:2014-09-16 09:48:40
【问题描述】:

我已将通知设置为android,它没有调用方法onCharacteristicRead()???? 它不进入函数。为什么会这样??

感谢任何帮助

请求解决方案。

这是我的代码:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:"
                    + mBluetoothGatt.discoverServices());
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "Disconnected from GATT server.");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            gattServices = mBluetoothGatt
                    .getService(SampleGattAttributes.SERVICES_UUID);
            if (gattServices != null) {
                gattCharacteristics = gattServices
                        .getCharacteristic(SampleGattAttributes.CHARACTERISTIC_UUID);
                System.out.println("character-->" + gattCharacteristics);
            }
            if (gattCharacteristics != null) {
                System.out.println("Characteristic not null");
                System.out.println("Characteristic Properties-->"
                        + gattCharacteristics.getProperties());
                mBluetoothGatt.setCharacteristicNotification(gattCharacteristics,
                true);
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic, int status) {
        System.out.println("in read");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            byte[] data = characteristic.getValue();
            System.out.println("reading");
            System.out.println(new String(data));
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic) {
        //
        System.out.println("change");
        byte[] data = characteristic.getValue();
        System.out.println(new String(data));
    }
};

提前谢谢你!!

【问题讨论】:

    标签: java android bluetooth bluetooth-lowenergy android-notifications


    【解决方案1】:

    首先,onCharacteristicRead 会在您读取以下特征时触发:

     mBluetoothGatt.readCharacteristic(characteristic);
    

    读取特征和设置通知是两件不同的事情。您想从中获取数据的特征类型是什么?

    是吗:

    • 阅读
    • 通知
    • 表示

    如果是read,您可以使用mBluetoothGatt.readCharacteristic(characteristic); 方法读取特征,但如果是notify 或indicate,您必须首先通过调用读取特征的descriptor:

    mBluetoothGatt.readDescriptor(ccc);
    

    阅读后,它应该通过调用onDescriptorRead 回调返回数据。
    在这里,您可以通过通知或调用指示来设置(订阅)特征:

    mBluetoothGatt.setCharacteristicNotification(characteristic, true)
    

    一旦它返回true,您将需要再次写入描述符(通知或指示的值)

    BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
    clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    // or
    //clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(clientConfig);
    

    完成此操作后,您将在每次特征更改时通过onCharacteristicChanged 回调收到通知。

    您可以阅读更多关于 Android 上的蓝牙连接的信息here
    以及关于蓝牙特性here

    【讨论】:

    • 感谢答案。我丢失了描述符并设置了错误的 uuid,所以描述符为空,它不执行 clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    • 您好,本卡我在读写操作时遇到问题,请您帮帮我。
    • @chet's - 发布你的问题的链接,让我看看我能不能为你做点什么。
    • @benka 请在stackoverflow.com/questions/27443139/…查看我的问题
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2013-07-26
    • 2016-06-14
    • 2021-10-27
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多