我有一个定制的蓝牙设备,我可以配对并连接到使用Windows 10,它会创建2个COM端口 - 一个列为传入,一个列在传出。
当我使用32Feet C#蓝牙库连接时,我能够发现并配对设备并启用SPP配置文件,但是,唉,我只获得一个COM端口,它被列为"传出"
我需要使用其他人的代码与设备连接,并且需要提供一个com端口号。不幸的是,它想要连接到"传入"端口。
因此我的问题是创建这个传入的COM端口需要什么魔力?我查看了32位代码和BluetoothSetServiceState(...)的底层API调用,它似乎没有任何参数来控制端口的创建方式。是否有此功能的另一个配置文件??
答案 0 :(得分:2)
您必须使用BluetoothAPIs.dll
中未记录的InstallIncomingComPort函数答案 1 :(得分:2)
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 :(得分:0)
如果您希望使用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);
}