IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

bool await "Object reference not set to an instance of an object"


Sujet :

C#

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    424
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 424
    Points : 134
    Points
    134
    Par défaut bool await "Object reference not set to an instance of an object"
    Bonjour,

    j'ai cette erreur "Object reference not set to an instance of an object"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    var services =
        await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
            RfcommDeviceService.GetDeviceSelector(
                RfcommServiceId.ObexObjectPush));
     
    if (services.Count > 0)
    {
        // Initialize the target Bluetooth BR device
        var service = await RfcommDeviceService.FromIdAsync(services[0].Id);
     
        bool res = await IsCompatibleVersion(service);
        var dialog = new MessageDialog(res.ToString());
        await dialog.ShowAsync();
    }
     
    // This App relies on CRC32 checking available in version 2.0 of the service.
    private const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
     
    private const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A;   // UINT32
    private const uint MINIMUM_SERVICE_VERSION = 200;
     
    private async System.Threading.Tasks.Task<bool> IsCompatibleVersion(RfcommDeviceService service)
    {
        var attributes = await service.GetSdpRawAttributesAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
        var attribute = attributes[SERVICE_VERSION_ATTRIBUTE_ID];
        var reader = DataReader.FromBuffer(attribute);
     
        // The first byte contains the attribute' s type
        byte attributeType = reader.ReadByte();
        if (attributeType == SERVICE_VERSION_ATTRIBUTE_TYPE)
        {
            // The remainder is the data
            uint version = reader.ReadUInt32();
            return version >= MINIMUM_SERVICE_VERSION;
        }
        return false;
    }
    merci

  2. #2
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 780
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 780
    Points : 5 537
    Points
    5 537
    Par défaut
    As-tu vérifié que service est bien assigné ?

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    424
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 424
    Points : 134
    Points
    134
    Par défaut
    en fait j'ai 2 types d'erreur, pour l'erreur précédente j'ai corrigé mon manifest

    avec un autre PC
    Ce service n’est pas connu. Impossible de trouver le service dans l’espace nom spécifié. (Exception from HRESULT: 0x8007277C)
    erreur au niveau de la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var attributes = await service.GetSdpRawAttributesAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
    avec un téléphone Android
    Exception levée*: 'System.Collections.Generic.KeyNotFoundException' dans System.Private.CoreLib.dll
    erreur au niveau de la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var attribute = attributes[SERVICE_VERSION_ATTRIBUTE_ID];
    Il s'agit pourtant d'un exemple MS Bluetooth RFCOMM, mais ça n'a pas l'air de fonctionner correctement

  4. #4
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 780
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 780
    Points : 5 537
    Points
    5 537
    Par défaut
    C'est normal que ça ne fonctionne pas sous Androïd.
    Tu utilises une API faite pour UWP...

    C'est d'ailleurs la première chose qui est marquée sur la page que tu mets en lien.
    This article provides an overview of Bluetooth RFCOMM in Universal Windows Platform (UWP) apps, along with example code on how to send or receive a file..
    Autrement dit ça ne marchera que sur ces éléments
    Windows 10 device—PCs, Xbox One, HoloLens

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    424
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 424
    Points : 134
    Points
    134
    Par défaut
    ok pour Android, ça n'explique pas l'erreur avec l'autre PC Windows 10
    Ce service n’est pas connu. Impossible de trouver le service dans l’espace nom spécifié. (Exception from HRESULT: 0x8007277C)
    en fait je cherche une API en C# UWP ou autre qui permet de lancer l'interface Paramètres > Périphériques > Appareils Bluetooth et autres
    et qui permet de récupérer les résultats de l'opération : fichiers transférés, annuler par l'utilisateur, échec…
    et qui fonctionne sur Windows, Android…

    le même principe que l'interface Windows mais par code

    Nom : Sans titre2.png
Affichages : 269
Taille : 51,5 Ko

    Nom : Sans titre.png
Affichages : 247
Taille : 20,9 Ko

  6. #6
    Expert confirmé
    Avatar de popo
    Homme Profil pro
    Analyste programmeur Delphi / C#
    Inscrit en
    Mars 2005
    Messages
    2 780
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Analyste programmeur Delphi / C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 2 780
    Points : 5 537
    Points
    5 537
    Par défaut
    A priori, il t'en manque un bout.
    Est-ce que tu as essayé de télécharger le dépôt GitHub fourni ?

    Le lien est sur la page que tu as mentionné.
    Je le remets ci-dessous.
    https://github.com/Microsoft/Windows...oothRfcommChat

  7. #7
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    424
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 424
    Points : 134
    Points
    134
    Par défaut
    @popo

    RFCOMM est disponible sur Android, ça pourrait fonctionner avec une application WMP sur Windows ?

  8. #8
    Futur Membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2016
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Services de proximité

    Informations forums :
    Inscription : Juin 2016
    Messages : 13
    Points : 9
    Points
    9
    Par défaut
    Bonjour,
    Il faudrait commencé par géré l'erreur par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    try
    {
    //ton code
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

Discussions similaires

  1. Object reference not set to an instance of an object
    Par DjRusty dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 24/03/2007, 07h26
  2. Réponses: 4
    Dernier message: 14/02/2007, 18h48
  3. Réponses: 1
    Dernier message: 28/12/2006, 11h05
  4. Réponses: 1
    Dernier message: 20/04/2006, 12h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo