locked
Why does RfcommDeviceService.FromIdAsync return null? RRS feed

  • Question

  • The "RfcommDeviceService.FromIdAsync" always returns null.

    I'm developing an App, which needs to read data from the Bluetooth device. The followings are my environment:

    1. OS: Windows 8.1 Pro (64-bit)

    2. VS 2013

    3. Build-in Bluetooth 3.0 of ThinkPad E520

    4. I established the Bluetooth connection between two laptops, and built a Windows program to make sure that the laptops can communicate with each other through the Serial Port services of Bluetooth!

        I can send a file to another PC with Bluetooth too, but "RfcommDeviceService.FromIdAsync" always returns null. Why? The followings are my C# codes:

            async void InitializeRfcommClient()
            {
                DeviceInformationCollection services = null;
                string selector = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
                services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(selector);
                if (services != null && services.Count > 0)
                {
                    int count = services.Count;
                    DeviceInformation deviceInfo = null;
                    RfcommDeviceService svr = null;
                     for (int i = 0; i < count; i++){
                     {
                         deviceInfo = services[i];
                         svr = await RfcommDeviceService.FromIdAsync(deviceInfo.Id);
                         if (svr != null) break;
                     }
                       if (svr == null) // Why is svr always null?
                    {
                        NotifyError(new Exception("Cannot gain the RfcommDeviceService instance!"));
                        return;
                    }
                    StreamSocket socket = new StreamSocket();
                    await socket.ConnectAsync(svr.ConnectionHostName,
                        svr.ConnectionServiceName,
                        SocketProtectionLevel.PlainSocket);

                    //......
                }
                else
                {
                    NotifyError(new Exception("No device is found!"));
                }
            }

    #

    Thanks in advance!

    -- Kevin



    Thursday, October 24, 2013 4:04 AM

Answers

  • Did you set the rfcomm capability?  see http://msdn.microsoft.com/en-us/library/windows/apps/dn263090.aspx for some details. They don't include an explicit demonstration of the rfcomm capability for SPP; it's like the very last XML sample code except that:

    1. You need Name="bluetooth.rfcomm" instead of the gatt thing

    2. You need name:serialPort instead of their name:heartRate

    I've just finished actually hooking up a small, cheap Bluetooth gamepad to a Metro app; getting the capability was the most frustrating (it's not super well documented).


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Kevin Wang CD Friday, October 25, 2013 2:25 AM
    Thursday, October 24, 2013 7:40 PM

All replies

  • Did you set the rfcomm capability?  see http://msdn.microsoft.com/en-us/library/windows/apps/dn263090.aspx for some details. They don't include an explicit demonstration of the rfcomm capability for SPP; it's like the very last XML sample code except that:

    1. You need Name="bluetooth.rfcomm" instead of the gatt thing

    2. You need name:serialPort instead of their name:heartRate

    I've just finished actually hooking up a small, cheap Bluetooth gamepad to a Metro app; getting the capability was the most frustrating (it's not super well documented).


    Network Developer Experience Team (Microsoft)

    • Marked as answer by Kevin Wang CD Friday, October 25, 2013 2:25 AM
    Thursday, October 24, 2013 7:40 PM
  • Also, are you calling this from the UI thread as documented?

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, October 24, 2013 7:42 PM
    Moderator
  • Thank you very much, Network Developer Experience Team!

    It indeed works!

    -- Kevin


    Friday, October 25, 2013 2:19 AM
  • Yes, I'm calling this from the UI thread as documented!

    I have solved this issue by setting the rfcomm capability according to the reply of Network Developer Experience Team.

    Thank you, Jeff!

    -- Kevin

    Friday, October 25, 2013 2:22 AM
  • also thy this

    <m2:FunctionType="name:serialPort"/>insteadofthistrybelow

    <m2:FunctionType="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>

    Thursday, April 30, 2015 6:37 AM