简体   繁体   English

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

[英]Bluetooth Serial Port (SPP) incoming port creation

I have a bespoke bluetooth device that I can pair with and connect to using windows 10 and it creates 2 com ports - one is listed as incoming and one is listed at outgoing. 我有一个定制的蓝牙设备,可以与Windows 10配对并连接到该设备,它会创建2个com端口-一个列为传入端口,一个列于传出端口。

When I connect using the 32Feet C# bluetooth libraries I am able to discover and pair the device and the enable the SPP profile but, alas, I only get one COM port and it is listed as "outgoing". 当我使用32Feet C#蓝牙库进行连接时,我能够发现并配对设备并启用SPP配置文件,但是,可惜,我仅获得一个COM端口,它被列为“传出”。

I am required to interface to the device using someone else's code and it needs to be supplied a com port number. 我需要使用其他人的代码连接到设备,并且需要提供一个COM端口号。 Unfortunately it wants to connect to the "incoming" port. 不幸的是,它想连接到“传入”端口。

Hence my question is what magic do I need to create this incoming com port? 因此,我的问题是创建传入的COM端口需要什么魔术? I have looked at the 32Feet code and the underlying API call of BluetoothSetServiceState(...) and it doesn't seem to have any parameters to control how the ports are created. 我已经看过32Feet代码和BluetoothSetServiceState(...)的底层API调用,它似乎没有任何参数可以控制端口的创建方式。 Is there another profile for this feature?? 有此功能的另一个配置文件吗?

您必须使用BluetoothAPIs.dll中未记录的InstallIncomingComPort函数

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());
}

If you are looking to use the InTheHand BT library and get an incoming com port you can add the following code to the bottom of the function 如果您想使用InTheHand BT库并获取传入的COM端口,则可以在函数底部添加以下代码

public void SetServiceState(Guid service, bool state, bool throwOnError)

in WindowsBlurtoothDeviceInfo.cs 在WindowsBlurtoothDeviceInfo.cs中

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM