连接蓝牙设备/如何设置rfcomm功能

时间:2014-11-25 14:44:34

标签: c# bluetooth windows-8.1

我正在尝试连接蓝牙设备

我已将它配对,当我搜索它时,我发现它:

private async void Grid_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    ListBox1.Items.Clear();
    var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); 
    var device = devices.FirstOrDefault(c => c.Name.Contains("BMMTCA32"));

    foreach (var element in device.Properties)
    {
        var strMessage = element.Key + (element.Value == null ? "" : " = " + element.Value.ToString());
        ListBox1.Items.Add(strMessage);
    }
}

以下是我的ListBox中的输出:

System.ItemNameDisplay = BMMTCA32-01
System.Devices.DeviceInstanceId = BTHENUM\{00001101-0000-1000-8000-00805f9b34fb}_LOCALMFG&0048\8&f358302&0&0012F31DECF3_C00000000
System.Devices.Icon = C:\Windows\System32\DDORes.dll,-2001
{51236583-0C4A-4FE8-B81F-166AEC13F510} 123 = C:\Windows\SYSTEM32\DDORes.dll,-3001
System.Devices.InterfaceEnabled = True
System.Devices.IsDefault = False
System.Devices.PhysicalDeviceLocation

但我的问题是如何连接到它?

当我尝试使用Googeling时,我会得到答案,例如你设置了rfcomm功能吗?有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/apps/dn263090.aspx

但是当我看到那个页面时,我迷失了,因为我不知道在清单文件中要写什么。

简而言之:我如何连接到设备?

PS:这是一个Windows Tablet程序。

1 个答案:

答案 0 :(得分:1)

所以你想知道你必须在清单文件中写什么,以及如何连接?

清单文件:

   <m2:DeviceCapability Name="bluetooth.rfcomm">
      <m2:Device Id="any">
        <m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>
      </m2:Device>
    </m2:DeviceCapability>
  • 您可以将ID保留为"any"
  • 函数类型可以是"name:serialPort",也可以是示例中指定的serviceId。

连接:

StreamSocket _socket;    
RfcommDeviceService service = await RfcommDeviceService.FromIdAsync(device.id);
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);

应该可以做到这一点。