我正在尝试连接蓝牙 LE 温度计。连接到设备工作正常。唯一让我挂断的部分是 gattCallBack 和 onCharacteristicChanged/Read。 'setNotification' 和描述符 'setValue' 和 'writeDescriptor' 都返回 true。永远不会调用 onCharacteristicChanged 来返回值。
我使用了 Play 商店中名为 BLE Scanner 的一个非常方便的小程序来帮助我提供有关该设备及其服务和特性的更多信息。
这就是为什么我简单地硬编码服务 2,特征 0。我似乎无法弄清楚为什么在我写描述符之后,我再也看不到任何东西回来了。有趣的是,我可以使用其他一些特征(一个是温度间隔)并且我确实收到了响应(尽管数据是乱码。)
另外,出于好奇,为什么这个特征有 2 个描述符?
此代码包含在我的 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 服务和特性设置。它不是。他们创建了自己的自定义特征。一旦我切换到该特性,“更改”方法就开始触发。
关于Android BLE onCharacteristicRead 和 onCharacteristicChanged 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659340/