1

I am trying to program a uwp app in C#, that must connect to any bluetooth device (not LE) that will be paired with the computer.

I started by copying the Microsoft RFComm Chat sample app, but ran into the problem described here (it was too slow, and sometimes dropped the connection for no reason). This SO user solved the problem by using a SerialDevice approach, so I implemented that.

From this SO answer, I discovered that Microsoft recommends to use a DeviceWatcher to find all connected Bluetooth devices, so I implemented that, and it works without a problem. I get a full list of Bluetooth devices as DeviceInformation objects.

However, I then try to use a selected DeviceInformation object to create a SerialDevice, it always fails with "Data is invalid" exception.

SerialDevice sd = await SerialDevice.FromIdAsync(myDeviceInfo.Id);

gives

"Die Daten sind unzulässig. (Exception from HRESULT: 0x8007000D)"

("the data are invalid")

The contents of myDeviceInfo.Id are Bluetooth#Bluetoothc0:b6:f9:c1:62:62-00:80:25:40:95:7b

I have also tried giving this string in directly as a string parameter, with the same result. The device is already paired with the computer, and was used many times successfully with this Id string and the RfcommService code.

In the manifest, I added the following lines:

 <Capabilities>
<DeviceCapability Name="bluetooth.rfcomm">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>

How can I successfully get a SerialDevice object for the Bluetooth device?

EDIT 1: Corrected the name of the DeviceCapability to "serialcommunication". I still get the same exception.

EDIT 2: The device exposes incoming and outgoing COM ports as follows: enter image description here

user1725145
  • 3,993
  • 2
  • 37
  • 58
  • It is always to call FromIdAsync by passing the device interface string and get the SerialDevice object. If it throws an exception, you could check these conditions that the [official document](https://learn.microsoft.com/en-us/uwp/api/windows.devices.serialcommunication.serialdevice?view=winrt-19041#remarks) mentioned. – dear_vv Mar 29 '21 at 09:47
  • @AryaDing Hi, thank you for your comment. The official documentation includes the example code line "SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id);" Therefore I used the Id field of the deviceInformation object. Why did it not work? What format should the Id field have? – user1725145 Mar 29 '21 at 15:11
  • It may be that the serial device is inaccessible. To allow the app to communicate with the serial device, you need to set the DeviceCapability to serialcommunication. As follows: ****. – dear_vv Mar 30 '21 at 08:29
  • I have already changed the DeviceCapability to "serialcommunication" and tested it, but I get the same exception. How do I know if the serial device is inaccessible? I am trying to communicate with an external optical probe, which is about ten years old I think. – user1725145 Mar 30 '21 at 08:31
  • @AryaDing-MSFT Please see updates to question. I can't see any reason why the serial device would be inaccessible, but perhaps I have misunderstood something? – user1725145 Mar 30 '21 at 09:17
  • I figured out what the problem was, and updated the question at https://topanswers.xyz/csharp?q=1715#a1937 – user1725145 Mar 30 '21 at 17:29

0 Answers0