Ma question est un peu une mise à jour à ma dernière question: c# Bluetooth LE - write configuration error - ValueChanged never calledC# Bluetooth LE - ValueChanged n'a jamais été appelé - configuration supplémentaire nécessaire?
Le problème est, je connecte mon WPF C# -Demande à mon appareil de BLE, mais characteristic.ValueChanged est jamais appelé. Peut-être que je manque quelque chose avec la configuration ou besoin d'envoyer une configuration supplémentaire à l'appareil, idk.
Voici la méthode de connexion de l'application du dispositif:
private async Task ConnectToWatcher(DeviceInformation deviceInfo) {
try {
// get the device
BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
// get the GATT service
Thread.Sleep(150);
var gattServicesResult = await device.GetGattServicesForUuidAsync(new Guid(RX_SERVICE_UUID));
service = gattServicesResult.Services[0];
// get the GATT characteristic
Thread.Sleep(150);
var gattCharacteristicsResult = await service.GetCharacteristicsForUuidAsync(new Guid(RX_CHAR_UUID));
characteristic = gattCharacteristicsResult.Characteristics[0];
// register for notifications
Thread.Sleep(150);
characteristic.ValueChanged += (sender, args) => {
Debug.WriteLine($"[{device.Name}] Received notification containing {args.CharacteristicValue.Length} bytes");
};
enableTXNotification();
} catch (Exception ex) when ((uint)ex.HResult == 0x800710df) {
Debug.WriteLine("bluetooth error 1");
// ERROR_DEVICE_NOT_AVAILABLE because the Bluetooth radio is not on.
}
}
Et voici la méthode d'envoi de la configuration:
public async void enableTXNotification()
{
Debug.WriteLine("enableTXNotification");
var gattCharacteristicsResult = await service.GetCharacteristicsForUuidAsync(new Guid(TX_CHAR_UUID));
if (gattCharacteristicsResult == null
|| gattCharacteristicsResult.Status != GattCommunicationStatus.Success
|| gattCharacteristicsResult.Characteristics == null
|| gattCharacteristicsResult.Characteristics?.Count < 1)
{
Debug.WriteLine(" Failed to find GATT characteristic.");
return;
}
txCharacteristic = gattCharacteristicsResult.Characteristics[0];
txCharacteristic.ValueChanged += (sender, args) => {
Debug.WriteLine("[{device.Name}] Received notification containing {args.CharacteristicValue.Length} bytes");
};
Debug.WriteLine(" Writing CCCD...");
GattWriteResult result =
await txCharacteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
Debug.WriteLine($" Characteristics write result: status={result.Status}, protocolError={result.ProtocolError}");
}
La dernière ligne crée la sortie status = succès. Toujours pas de méthode ValueChanged (je soupçonne que le second est unnecassary) est appelé lorsque j'utilise l'appareil. Je suis à court d'idées. Quelqu'un peut-il aider?
Vous semblez avoir déjà posé cette question un jour avant ... Si vous souhaitez développer cette question, alors il suffit de l'éditer, ne pas continuer à re-poster la même question à nouveau. –