我想从HC-06设备接收到PC
的消息,我使用32英尺库来管理蓝牙,但每当我尝试连接时,都会遇到异常。
HC-06
使用SPP (Serial Port Profile)
,因此其想法是在设备管理器中使用虚拟COM端口。然后使用SerialPort
类进行通信。
例外
System.Net.Socket.SocketException(0x80004005):连接尝试失败,因为连接方在一段时间,或由于连接而建立的连接失败主机无法响应我的蓝牙地址:在System.Net.Socket.Socket.DoConnect(端点endPointSnapshot,SocketAddress SocketAddress)位于的System.Net.Socket.Socket.Connect(EndpointremoteEP)InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.Connect(蓝牙端点remoteEP)InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothEndPointremoteEP)。。。。
当前C#
代码:
string my_pin ="1234"; //default pin for HC-06 device
BluetoothAddress address = BluetoothAddress.Parse("201311111662"); //address of HC-06
BluetoothEndPoint ep = new BluetoothEndPoint(address, BluetoothService.SerialPort);
bool t = false;
BluetoothSecurity.PairRequest(address, my_pin);
BluetoothClient cli = new BluetoothClient();
cli.Connect(ep);
t = cli.Connected;
此外,我还尝试在线路上使用RFCommProtocol
(服务类)而不是SerialPort
BluetoothEndPoint ep = new BluetoothEndPoint(address, BluetoothService.SerialPort);
但未成功
为了测试HC-06设备实际上发送了消息,我使用Realterm软件进行了检查,我在终端COM8 PORT 8=BthModem0
中分配了波特率为9600
的端口,消息出现在终端中。BthModem通过TDI接口将所有I/O和控制操作引导至RfComm
我错过了什么?我必须使用虚拟连接或其他东西才能获得HC-06
消息吗?
如果这是一个"普通"的Windows应用程序(而不是Windows应用商店应用程序),那么使用SerialPort类可能会更容易。