locked
PeerFinder.SupportedDiscoveryTypes returns none in WinRT Bluetooth app?

    Question

  •    

    I am trying to establish a connection between my Windows Store App to my Windows Phone over Bluetooth.  I am working with one of the MSDN Bluetooth samples.

    However, when I run the app, I get an error message saying "Browsing for peers is not supported".  So I can't even execute a scan for paired Bluetooth devices it seems.  I looked in the code and found this:

         public PeerFinderScenario()
            {
                this.InitializeComponent();
    
                this.Loaded += OnLoaded;
                _socketHelper.RaiseSocketErrorEvent += SocketErrorHandler;
                _socketHelper.RaiseMessageEvent += MessageHandler;
    
                // Scenario 1 init
                _triggeredConnectSupported = (PeerFinder.SupportedDiscoveryTypes & PeerDiscoveryTypes.Triggered) ==
                                             PeerDiscoveryTypes.Triggered;
    
                _browseConnectSupported = (PeerFinder.SupportedDiscoveryTypes & PeerDiscoveryTypes.Browse) ==
                                          PeerDiscoveryTypes.Browse;
    
                Window.Current.SizeChanged += Current_SizeChanged;
            }


    At run-time when this initialization call is made, _browseConnectSupported is being set to FALSE due to what's coming back from the PeerFinder object.  To be specific PeerFinder.SupportedDiscoveryTypes is equal to "None".  Why is this happening and what do I need to do to get the right value into SupportedDiscoveryTypes?  Is it a manifest declaration issue?  A PC wide Bluetooth settings issue?

    I do have a properly functioning Bluetooth dongle.  I know this because I am able to pair with my Windows Phone properly.  Also, in my manifest capabilities section I have requested "InternetClientServer" and "proximity" capabilities, and added a declaration for a Bluetooth serial port:

    <Capabilities>
        <Capability Name="internetClientServer" />
        <DeviceCapability Name="proximity" />
        <!-- Added Bluetooth serial port capability manually -->
        <m2:DeviceCapability Name="bluetooth.rfcomm">
          <m2:Device Id="any">
            <m2:Function Type="name:serialPort" />
          </m2:Device>
        </m2:DeviceCapability>
      </Capabilities>


    -- roschler



    • Edited by roschler Friday, September 19, 2014 12:14 AM Added manifest info
    Thursday, September 18, 2014 9:19 PM

All replies

  • Hi Roschler - I am currently investigating this area of the APIs for problems. I'll update this thread when I know more. Please feel free to bump it if I haven't updated in a while.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, September 19, 2014 7:12 PM
    Moderator
  • Thanks Matt.  I've got a heavyweight project dependent on this and a deadline to go with it, please let me know as soon as you find an answer.

    -- roschler

    Friday, September 19, 2014 7:42 PM
  • Hi Roschler - the PeerFinder APIs work between PC over Wifi Direct, and between phones using Bluetooth. I would recommend that you use the Bluetoth RFComm Chat Sample to achieve this.

    About the sample:  I recently found a bug in it that you'll have to fix. In Scenario1_ChatClient_xaml.cs, inside the BluetoothRfcommChat.Shared project, please replace the method ServiceList_Tapped with the below code:

    private async void ServiceList_Tapped(object sender, TappedRoutedEventArgs e)
    {

         try

                {
                    RunButton.IsEnabled = false;
                    ServiceSelector.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                    var chatServiceInfo = chatServiceInfoCollection[ServiceList.SelectedIndex];

                    await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>

                    {

                        chatService = await RfcommDeviceService.FromIdAsync(chatServiceInfo.Id);

                        if (chatService == null)

                        {

                            MainPage.Current.NotifyUser("Access to the device is denied because the application was not granted access", NotifyType.StatusMessage);

                            return;

                        }

                        var attributes = await chatService.GetSdpRawAttributesAsync();

                        if (!attributes.ContainsKey(SdpServiceNameAttributeId))

                        {

                            MainPage.Current.NotifyUser( "The Chat service is not advertising the Service Name attribute (attribute id=0x100). " + "Please verify that you are running the BluetoothRfcommChat server.",    NotifyType.ErrorMessage);

                            return;

                        }


                        var attributeReader = DataReader.FromBuffer(attributes[SdpServiceNameAttributeId]);

                        var attributeType = attributeReader.ReadByte();

                        if (attributeType != SdpServiceNameAttributeType)

                        {

                            MainPage.Current.NotifyUser(

                                "The Chat service is using an unexpected format for the Service Name attribute. " +

                                "Please verify that you are running the BluetoothRfcommChat server.",

                                NotifyType.ErrorMessage);

                            return;

                        }


                        var serviceNameLength = attributeReader.ReadByte();

                        // The Service Name attribute requires UTF-8 encoding.

                        attributeReader.UnicodeEncoding = UnicodeEncoding.Utf8;

                        ServiceName.Text = "Service Name: \"" + attributeReader.ReadString(serviceNameLength) + "\""; 

                        lock (this)

                        {

                           chatSocket = new StreamSocket();

                        }


                        await chatSocket.ConnectAsync(chatService.ConnectionHostName, chatService.ConnectionServiceName);

                        chatWriter = new DataWriter(chatSocket.OutputStream);

                        ChatBox.Visibility = Windows.UI.Xaml.Visibility.Visible;

                        DataReader chatReader = new DataReader(chatSocket.InputStream);

                        ReceiveStringLoop(chatReader);

                    });

                }

                catch (Exception ex)

                {

                    RunButton.IsEnabled = true;

                    MainPage.Current.NotifyUser("Error: " + ex.HResult.ToString() + " - " + ex.Message,

                        NotifyType.ErrorMessage);

                }

            }



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, October 3, 2014 5:07 PM
    Moderator
  • Thanks Matt.  I'll have a look.

    -- roschler

    Saturday, October 4, 2014 10:29 AM