[英]Bluetooth Serial Port (SPP) incoming port creation
我有一个定制的蓝牙设备,可以与Windows 10配对并连接到该设备,它会创建2个com端口-一个列为传入端口,一个列于传出端口。
当我使用32Feet C#蓝牙库进行连接时,我能够发现并配对设备并启用SPP配置文件,但是,可惜,我仅获得一个COM端口,它被列为“传出”。
我需要使用其他人的代码连接到设备,并且需要提供一个COM端口号。 不幸的是,它想连接到“传入”端口。
因此,我的问题是创建传入的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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.