首页 文章

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

提问于
浏览
1

我有一个定制的蓝牙设备,我可以配对并连接到使用Windows 10,它创建了2个COM端口 - 一个列为传入,一个列在传出 .

当我使用32Feet C#蓝牙库连接时,我能够发现并配对设备并启用SPP配置文件,但是,唉,我只获得一个COM端口,它被列为“传出” .

我需要使用其他人的代码与设备连接,并且需要提供一个com端口号 . 不幸的是,它想要连接到“传入”端口 .

因此,我的问题是创建这个传入的COM端口需要什么魔力?我查看了32个函数代码和BluetoothSetServiceState(...)的底层API调用,它似乎没有任何参数来控制端口的创建方式 . 这个功能还有另一个配置文件吗?

3 回答

  • 0
    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

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

  • 2

    如果您要使用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);
    }
    

相关问题