c# - 蓝牙串行端口 (SPP) 传入端口创建

标签 c# .net bluetooth 32feet spp

我有一个定制的蓝牙设备,我可以使用 Windows 10 与之配对和连接,它创建了 2 个 com 端口 - 一个列为传入端口,一个列为传出端口。

当我使用 32Feet C# 蓝牙库连接时,我能够发现并配对设备并启用 SPP 配置文件,但是,遗憾的是,我只有一个 COM 端口并且它被列为“传出”。

我需要使用其他人的代码连接到设备,并且需要提供一个 com 端口号。不幸的是,它想连接到“传入”端口。

因此我的问题是我需要什么魔法来创建这个传入的 com 端口?我查看了 32Feet 代码和 BluetoothSetServiceState(...) 的底层 API 调用,它似乎没有任何参数来控制端口的创建方式。此功能是否有其他配置文件??

最佳答案

private const UInt16 BLUETOOTH_MAX_SERVICE_NAME_SIZE = 256;
private const UInt16 BLUETOOTH_DEVICE_NAME_SIZE  = 256;

private static Guid SerialPortServiceClass_UUID = new Guid("{00001101-0000-1000-8000-00805F9B34FB}");

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct BLUETOOTH_LOCAL_SERVICE_INFO
{
            public Boolean Enabled;
            public Int64 btAddr;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BLUETOOTH_MAX_SERVICE_NAME_SIZE)]
            public String szName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BLUETOOTH_DEVICE_NAME_SIZE)]
            public String szDeviceString;
};

[DllImport("BluetoothAPIs.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern UInt32 BluetoothSetLocalServiceInfo(IntPtr hRadioIn, ref Guid pClassGuid, UInt32 ulInstance, ref BLUETOOTH_LOCAL_SERVICE_INFO pServiceInfoIn);

private void CreateComPort(Boolean Create)
{
            BLUETOOTH_LOCAL_SERVICE_INFO s = new BLUETOOTH_LOCAL_SERVICE_INFO();
            s.btAddr = 0;
            s.Enabled = Create;
            s.szName = "MyComPort";
            s.szDeviceString = "COM10";

            UInt32 Res = BluetoothSetLocalServiceInfo(IntPtr.Zero,
                ref SerialPortServiceClass_UUID, 1, ref s);
            MessageBox.Show(Res.ToString());
}

关于c# - 蓝牙串行端口 (SPP) 传入端口创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50675996/

相关文章:

c# - 从顶部将纹理平铺到矩形

.net - 如何将 Enterprise Library 升级到最新版本?

c# - Visual Studio Express ClickOnce - 无法使用应用程序发布 .NET 4.5 脱机安装程序

c# - 创建与 AspNetUsers 表的关系

Android 蓝牙 VS 低功耗蓝牙

java - 我如何在一秒内实时获得蓝牙 RSSI 10 次,持续 10 秒

c# - MassTransit 如何处理 RabbitMQ 宕机

c# - 在 C# .net 中创建两个任务并在特定时间段后关闭其中一个任务

ios - 在 iOS 上使用蓝牙

c# - 在 asp.net mvc razor 中使用 JQuery 范围 slider