android 4.3 蓝牙 ble 不调用 onCharacteristicRead() [英] android 4.3 Bluetooth ble don't called onCharacteristicRead()

查看:76
本文介绍了android 4.3 蓝牙 ble 不调用 onCharacteristicRead()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've set the notification into android, It is not calling to method onCharacteristicRead()???? It does not enter into the function. Why it is happening so??

感谢任何帮助

请求解决方案.

这是我的代码:

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));
    }
};

提前谢谢你!!

推荐答案

首先 onCharacteristicRead 将在您通过以下方式读取特征时触发:

First of all onCharacteristicRead will fire if you have read a characteristic by:

 mBluetoothGatt.readCharacteristic(characteristic);

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

Reading a characteristic and setting up notifications are two different things. What is the type of your characteristic you want to get data from?

是吗:

  • 阅读
  • 通知
  • 表明

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

If it is read you can read the characteristic using the mBluetoothGatt.readCharacteristic(characteristic); method but if its notify or indicate first you will have to read the characteristic's descriptor by calling:

mBluetoothGatt.readDescriptor(ccc);

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

Once you read it, it should return data by calling the onDescriptorRead callback.
Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:

mBluetoothGatt.setCharacteristicNotification(characteristic, true)

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

once it returns true you will need to write to the descriptor again (the value of notification or indication)

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

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

Once this is done you will get notifications throuhg onCharacteristicChanged callback every time the characteristic changes.

您可以在此处
阅读有关 Android 上蓝牙连接的更多信息关于蓝牙特性这里

这篇关于android 4.3 蓝牙 ble 不调用 onCharacteristicRead()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆