我有一个定制的蓝牙设备,我可以与之配对,并连接到使用windows 10,它创建了2个com端口-一个被列为传入,一个列在出站。
当我使用32 32Feet C#蓝牙库连接时,我能够发现并对设备和启用SPP配置文件进行配对,但是,唉,我只得到一个COM端口,它被列为“传出”。
我需要使用其他人的代码与设备接口,并且需要提供一个com端口号。不幸的是,它希望连接到“传入”端口。
因此,我的问题是,我需要什么魔法来创建这个传入的com端口?我看过32 32Feet代码和BluetoothSetServiceState的底层API调用(.)而且它似乎没有任何参数来控制如何创建端口。这个功能还有其他的配置文件吗??
发布于 2018-06-06 07:22:50
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());
}发布于 2018-06-04 08:27:58
您必须使用来自BluetoothAPIs.dll的无文档化的BluetoothAPIs.dll函数。
发布于 2018-06-12 04:34:25
如果您希望使用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);
}https://stackoverflow.com/questions/50675996
复制相似问题