从未调用Android BLE onCharacteristicRead和onCharacteristicChanged

从未调用Android BLE onCharacteristicRead和onCharacteristicChanged,android,bluetooth,Android,Bluetooth,我正在尝试连接蓝牙温度计。连接到设备工作正常。唯一让我头疼的是gattCallBack,它的onCharacteristicChanged/Read。“setNotification”和描述符“setValue”以及“writeDescriptor”都返回true。永远不会调用onCharacteristicChanged来返回值 我使用了Play Store中一个非常方便的小程序BLE Scanner来帮助我提供更多关于该设备及其服务和特性的信息 这就是为什么我只是硬编码服务2,特征0。我只

我正在尝试连接蓝牙温度计。连接到设备工作正常。唯一让我头疼的是gattCallBack,它的onCharacteristicChanged/Read。“setNotification”和描述符“setValue”以及“writeDescriptor”都返回true。永远不会调用onCharacteristicChanged来返回值

我使用了Play Store中一个非常方便的小程序BLE Scanner来帮助我提供更多关于该设备及其服务和特性的信息

这就是为什么我只是硬编码服务2,特征0。我只是不明白为什么在我写了脚本之后,我再也看不到任何东西回来了。有趣的是,我可以使用一些其他特性(一个是温度间隔),我确实收到了一个响应(尽管数据是乱码的)

另外,出于好奇,为什么在这个特征上有两个描述符

此代码包含在我的MainActivity方法中。我不确定这是否会带来不同。我已经看过并尝试了在这里张贴的几种方法,但没有成功

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    { ... }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
        mGatt = gatt;

        List<BluetoothGattService> services = mGatt.getServices();
        Log.i("onServicesDiscovered", services.toString());
        
        BluetoothGattCharacteristic characteristic = services.get(2).getCharacteristics().get(0);

        mGatt.setCharacteristicNotification(characteristic, true);
        
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        
        mGatt.writeDescriptor(descriptor);
        
    }            

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    { ... }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
    { ... }
};
有趣的是,状态返回13,即“写入操作超过了属性的最大长度”


我将进一步研究这一点。

我认为您缺少
mGatt.readCharacteristic(characteristic)


当您调用它时,它将等待读取完成,然后调用
onCharacteristicRead

我在这里发现了问题。我假设温度计使用标准的BLE服务和特性设置。事实并非如此。他们创造了自己的风俗特点。一旦我切换到该特性,“更改”方法就开始启动

@Override            
public void onDescriptorWrite(BluetoothGatt gatt,  BluetoothGattDescriptor descriptor, int status)
    {
        Log.i("descriptorWRITE", Integer.toString(status));
    }