wince连接蓝牙打印机

qq_34477459 2016-05-23 10:32:51
我想做一个小工具,就是实现通过蓝牙将两个Windows Mobile系统的机子连接上,然后互相能建立端口(就是虚拟一个串口)用于发送内部GPS数据,现在我上网搜了一个库(InTheHand.Net.Personal.dll),利用它可以搜索到可见蓝牙,但是却连不上,总是出现"由于目标计算机主动拒绝,连接未能建立"的错误,而且连上之后怎么虚拟串口呢,也不会.下面是我的源码:
client = new BluetoothClient();
devices = client.DiscoverDevices();
Cursor.Current = Cursors.Default;
this.listBox1.Items.Clear();
if (devices != null && devices.Length > 0) {
for (int i = 0; i < devices.Length; i++) {
if (devices[i].DeviceName != null) {
this.listBox1.Items.Add(devices[i].DeviceName);
}
}
}
连接:
address = new BluetoothAddress(devices[index].DeviceID);
Guid service = BluetoothService.SerialPort;
client.Connect(new BluetoothEndPoint(address, service));
请路过的高手大神们帮忙看看,太纠结了,第一次接触这些东西,搞了好几天了,还是不知道错在哪里,谢谢大家了!!
...全文
2562 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
轻鸿万里 2016-06-16
  • 打赏
  • 举报
回复
/// <summary> /// 连接蓝牙设备 /// </summary> /// <param name="addr"></param> /// <param name="pin"></param> /// <returns></returns> public bool Connect(string addr) { comDevice = RegisterComPort(addr, comPortName); if (comDevice != IntPtr.Zero) { IntPtr hFile = NativeMethod.CreateFile(comPortName, NativeMethod.GENERIC_READ | NativeMethod.GENERIC_WRITE, 0, IntPtr.Zero, NativeMethod.OPEN_EXISTING, NativeMethod.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (hFile != NativeMethod.INVALID_HANDLE_VALUE) { DCB dcb = new DCB(); NativeMethod.GetCommState(hFile, ref dcb); dcb.BaudRate = 115200; dcb.ByteSize = 8; dcb.StopBits = 0;// 0,1,2 = 1, 1.5, 2 dcb.Parity = 0; // 0-4=no,odd,even,mark,space NativeMethod.SetCommState(hFile, ref dcb); handle = hFile; return true; } } return false; } /// <summary> /// 断开连接 /// </summary> public void Disconnect() { if (handle != NativeMethod.INVALID_HANDLE_VALUE) { NativeMethod.CloseHandle(handle); handle = NativeMethod.INVALID_HANDLE_VALUE; } UnregisterCOMPort(ref comDevice); }
轻鸿万里 2016-06-16
  • 打赏
  • 举报
回复
类似这个 /// <summary> /// 检测虚拟端口 /// </summary> /// <param name="comPort"></param> /// <returns></returns> private IntPtr FindComPort(string portName) { IntPtr hDevice = IntPtr.Zero; DEVMGR_DEVICE_INFORMATION ddi = new DEVMGR_DEVICE_INFORMATION(); ddi.dwSize = (uint)Marshal.SizeOf(ddi); IntPtr findHandle = NativeMethod.FindFirstDevice(DeviceSearchType.DeviceSearchByLegacyName, portName, ref ddi); if (findHandle != NativeMethod.INVALID_HANDLE_VALUE) { hDevice = ddi.hDevice; NativeMethod.FindClose(findHandle); } return hDevice; } /// <summary> /// 注册虚拟COM口 /// </summary> /// <param name="addr"></param> private IntPtr RegisterComPort(string addr, string portName) { //if (pin.Length < 1 | pin.Length > 16) //{ // throw new ArgumentException("Pin must be between 1 and 16 characters long."); //} ulong btAddr = ulong.Parse(addr, System.Globalization.NumberStyles.HexNumber); bool bRegistered = false; IntPtr hDevice = FindComPort(portName); if (hDevice != IntPtr.Zero) { IntPtr hFile = NativeMethod.CreateFile(portName, NativeMethod.GENERIC_READ | NativeMethod.GENERIC_WRITE, 0, IntPtr.Zero, NativeMethod.OPEN_EXISTING, NativeMethod.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (hFile != NativeMethod.INVALID_HANDLE_VALUE) { ulong pba = 0; uint bytesReturned; if (NativeMethod.DeviceIoControl(hFile, NativeMethod.IOCTL_BLUETOOTH_GET_PEER_DEVICE, IntPtr.Zero, 0, ref pba, 8, out bytesReturned, IntPtr.Zero)) { if (pba == btAddr) { bRegistered = true; } } NativeMethod.CloseHandle(hFile); } } if (bRegistered) { return hDevice; } if (hDevice != IntPtr.Zero) { UnregisterCOMPort(ref hDevice); } PORTEMUPortParams portInfo = new PORTEMUPortParams(); portInfo.device = btAddr; portInfo.channel = 1; hDevice = NativeMethod.RegisterDevice("COM", 8, "btd.dll", ref portInfo); if (hDevice == IntPtr.Zero) { int error = Marshal.GetLastWin32Error(); } return hDevice; } /// <summary> /// 注销虚拟COM /// </summary> /// <param name="comm"></param> private void UnregisterCOMPort(ref IntPtr hComPort) { if (hComPort != IntPtr.Zero) { NativeMethod.DeregisterDevice(hComPort); hComPort = IntPtr.Zero; } }

7,656

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧