android 4.3蓝牙ble不要调用onCharacteristicRead()

Jianping Zhu

我已将通知设置为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每次特性更改时,您都会通过回调获得通知

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android的BLE onScanResult不会被调用

BLE Android-不调用onConnectionStateChange

Android BLE:onCharacteristicRead()似乎被线程阻止

在Android BLE中未调用onBatchScanResults

如何使用android ble扫描应用程序检查蓝牙LE ScanResult?

Android:从Java代码调用Python脚本(通过SL4A)

Android 4上的Android Studio出现NoClassDefFoundError

BLE广告的Android 5静态蓝牙MAC地址

RxJava-如何在Android中的4次点击事件上调用订户

关闭蓝牙然后再次打开后,Android 6.0将不再连接到BLE设备

从未调用Android BLE onCharacteristicRead和onCharacteristicChanged

亚行logcat cordova android 4

Android BLE客户端仅在onCharacteristicRead中返回600字节的数据

如何使用意图通过蓝牙BLE发送双[]数组-Android Studio(API 17)

Android蓝牙BLE我可以修改哪些连接的配置参数

使用BLE设备从Android完成蓝牙通信

有没有同时支持Bluetooth / BLE 5.0和4.x的蓝牙模块?

Android BLE:onCharacteristicRead仅在第一次使用

如何修复“尝试在空对象引用上调用虚拟方法“ android.support.v4.app.p android.support.v4.app.ka()””

与BLE的蓝牙配对在Android中不起作用

在Ionic 4应用中进行的API调用不适用于Android设备

禁用蓝牙识别Android / Kotlin上的BLE设备

如何通过蓝牙在Raspberry Pi 4和Arduino Nano BLE之间执行读写操作?

skype 4 android,调用意图

Android 4+的Roboto字体?

尝试在空对象引用上调用虚拟方法'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'

尝试在null对象上调用虚拟方法'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()'

从静态方法调用活动时,Android 4中的工具栏样式会更改

HM10 与 Arduino 和 Android BLE 之间的低功耗蓝牙大数据传输