java - android 4.3 蓝牙蓝牙不调用 onCharacteristicRead()

标签 java android bluetooth bluetooth-lowenergy android-notifications

我已经将通知设置到 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));
    }
};

提前致谢!

最佳答案

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

 mBluetoothGatt.readCharacteristic(characteristic);

读取特性和设置通知是两件不同的事情。您希望从哪种类型的特征中获取数据?

是吗:

  • 阅读
  • 通知
  • 指出

如果是read,您可以使用mBluetoothGatt.readCharacteristic(characteristic); 方法读取特征,但如果是notifyindicate 首先,您必须通过调用来读取特征的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

关于java - android 4.3 蓝牙蓝牙不调用 onCharacteristicRead(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25865587/

相关文章:

java - 这个子类如何隐式调用其父类的构造函数?

java - GSON - 特定情况下的自定义序列化程序

java - 如何从抽象类实例化子类对象中访问子类方法

Android导入导入com.google.android.gms.common.AccountPicker报错

swift - 以编程方式更改热敏蓝牙打印机上的字体大小

java - 这两副牌设计模式中哪一种更稳定/更受欢迎?

android - 尝试使用 PowerMockito stub android Activity 类抛出 RuntimeException "Stub!"

java - MainActivity 中弹出窗口中的 AutoCompleteTextView

ios - 核心蓝牙 - 无法使用 CBCharacteristicWriteType.WithResponse 写入值

安卓蓝牙速度