[英]Android BLE onCharacteristicRead and onCharacteristicChanged never called
我正在尝试连接到蓝牙LE温度计。 连接到设备工作正常。 使我挂断的唯一部分是gattCallBack,它处于onCharacteristicChanged / Read状态。 'setNotification'和描述符'setValue'和'writeDescriptor'都返回true。 从不调用onCharacteristicChanged返回值。
我使用了来自Play商店的一个非常方便的小程序,称为BLE Scanner,可以帮助我向我提供有关该设备及其服务和特性的更多信息。
这就是为什么我只是对服务2(特征0)进行硬编码的原因。我似乎无法弄清楚为什么在我编写了Descriptor之后,再也看不到任何东西。 有趣的是,我可以使用其他一些特性(一个是温度间隔),并且确实会收到响应(尽管数据有乱码。)
另外,出于好奇,为什么在这个特征上有两个描述符?
此代码包含在我的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)
{ ... }
};
更新:我决定检查onDescriptorWrite方法并记录一些信息。
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
{
Log.i("descriptorWRITE", Integer.toString(status));
}
有趣的是,状态返回13,即“写操作超出了属性的最大长度”。
我将对此进行进一步调查。
我在这里发现了问题。 我以为温度计正在使用标准BLE服务和特性设置。 它不是。 他们创建了自己的自定义特征。 一旦我切换到该特性,就开始触发“更改”方法。
我认为您缺少mGatt.readCharacteristic(characteristic)
。
调用它时,它将等待读取完成并调用onCharacteristicRead
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.