2017-10-02 2 views
-1

J'ai des problèmes avec le foulage sur ce code bluetooth. J'utilise cette bibliothèque https://github.com/xabre/xamarin-bluetooth-le. Il fonctionne comme prévu pour android (Samsung SM G900V Api 23) mais quand je l'exécute sur iOS (iphone 6 10.3) les mises à jour ne sont jamais déclenchées à moins que j'ajoute un point de rupture. J'ai essayé quelques choses différentes mais je suis nouveau sur C# et threading. Qu'est-ce que je fais mal?C# méthodes async avec BLE

public async void ConnectToDeviceService(IDevice device) 
{ 
    service = await device.GetServiceAsync(Guid.Parse(_UUIDS.SERVICE_UUID)); 

    infoCharacter = await service.GetCharacteristicAsync(Guid.Parse(_UUIDS.DEVICE_INFO_UUID)); 
    infoCharacter.ValueUpdated += (o, args) => 
    { 
     var bytes = args.Characteristic.Value; 
     Console.WriteLine("Characteristic Value: {0}", bytes); 
     PrintByteArray(bytes); 
    }; 

    await infoCharacter.StartUpdatesAsync(); 
} 

Essayé cela aussi bien

await infoCharacter.StartUpdatesAsync().ContinueWith(result => 
{ 
    infoCharacter.ValueUpdated += (o, args) => 
    { 
     var bytes = args.Characteristic.Value; 
     Console.WriteLine("Characteristic Value: {0}", bytes); 
     PrintByteArray(bytes); 
    }; 
}); 

Répondre

-1

Ok donc j'ai trouvé une solution mais je ne comprends pas très bien pourquoi il semble que il y a une condition de course sur iOS entre le gestionnaire d'événements étant ajouté à la variable et les mises à jour à partir? Ce problème ne se produit pas pour moi sur les appareils Android.

La solution:

public async void ConnectToDeviceService(IDevice device) 
{ 
    service = await device.GetServiceAsync(Guid.Parse(_UUIDS.SERVICE_UUID)); 

    infoCharacter = await service.GetCharacteristicAsync(Guid.Parse(_UUIDS.DEVICE_INFO_UUID)); 
    infoCharacter.ValueUpdated += (o, args) => 
    { 
     var bytes = args.Characteristic.Value; 
     Console.WriteLine("Characteristic Value: {0}", bytes); 
     PrintByteArray(bytes); 
    }; 

    await Task.Delay(1000); 
    await infoCharacter.StartUpdatesAsync(); 
}