首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝牙串口(SPP)传入端口的创建

蓝牙串口(SPP)传入端口的创建
EN

Stack Overflow用户
提问于 2018-06-04 08:07:42
回答 3查看 2K关注 0票数 1

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

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

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-06 07:22:50

代码语言:javascript
复制
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());
}
票数 2
EN

Stack Overflow用户

发布于 2018-06-04 08:27:58

您必须使用来自BluetoothAPIs.dll的无文档化的BluetoothAPIs.dll函数。

票数 2
EN

Stack Overflow用户

发布于 2018-06-12 04:34:25

如果您希望使用InTheHand BT库并获得传入的com端口,则可以将以下代码添加到函数的底部

代码语言:javascript
复制
public void SetServiceState(Guid service, bool state, bool throwOnError)

在WindowsBlurtoothDeviceInfo.cs中

代码语言:javascript
复制
if (service == BluetoothService.SerialPort)
{
    NativeMethods.BLUETOOTH_LOCAL_SERVICE_INFO s = new NativeMethods.BLUETOOTH_LOCAL_SERVICE_INFO();
    s.btAddr = deviceInfo.Address;
    s.Enabled = state;
    s.szName = "RemScan";
    s.szDeviceString = "COM10";
    UInt32 Res = NativeMethods.BluetoothSetLocalServiceInfo(IntPtr.Zero, ref NativeMethods.SerialPortServiceClass_UUID, 1, ref s);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50675996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档