蓝牙串行端口 (SPP) 传入端口创建
Bluetooth Serial Port (SPP) incoming port creation
我有一个定制的蓝牙设备,我可以使用 windows10 与之配对和连接,它创建了 2 个 com 端口 - 一个列为传入端口,一个列为传出端口。
当我使用 32Feet C# 蓝牙库连接时,我能够发现并配对设备并启用 SPP 配置文件,但是,唉,我只有一个 COM 端口,它被列为 "outgoing"。
我需要使用其他人的代码连接到设备,并且需要提供一个 com 端口号。不幸的是,它想连接到 "incoming" 端口。
因此我的问题是我需要什么魔法来创建这个传入的 com 端口?我查看了 32Feet 代码和 BluetoothSetServiceState(...) 的底层 API 调用,它似乎没有任何参数来控制端口的创建方式。此功能是否有其他配置文件?
您必须使用 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());
}
如果您想使用 InTheHand BT 库并获得传入的 com 端口,您可以将以下代码添加到函数的底部
public void SetServiceState(Guid service, bool state, bool throwOnError)
在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);
}
我有一个定制的蓝牙设备,我可以使用 windows10 与之配对和连接,它创建了 2 个 com 端口 - 一个列为传入端口,一个列为传出端口。
当我使用 32Feet C# 蓝牙库连接时,我能够发现并配对设备并启用 SPP 配置文件,但是,唉,我只有一个 COM 端口,它被列为 "outgoing"。
我需要使用其他人的代码连接到设备,并且需要提供一个 com 端口号。不幸的是,它想连接到 "incoming" 端口。
因此我的问题是我需要什么魔法来创建这个传入的 com 端口?我查看了 32Feet 代码和 BluetoothSetServiceState(...) 的底层 API 调用,它似乎没有任何参数来控制端口的创建方式。此功能是否有其他配置文件?
您必须使用 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());
}
如果您想使用 InTheHand BT 库并获得传入的 com 端口,您可以将以下代码添加到函数的底部
public void SetServiceState(Guid service, bool state, bool throwOnError)
在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);
}