我正在尝试与自定义蓝牙设备建立连接,而不使用COM端口。但是,我收到一个错误:[10049]“请求的地址在其上下文中无效”。我做错了什么?
static Guid serviceClass= new Guid("4d36e978-e325-11ce-bfc1-08002be10318"); //GUID of device class
static BluetoothAddress addr = BluetoothAddress.Parse("001210160177"); //from device
BluetoothDeviceInfo device = new BluetoothDeviceInfo(addr);
device.SetServiceState(serviceClass, true);
Console.WriteLine(BluetoothSecurity.PairRequest(device.DeviceAddress, "0000")); //pairing my device - writes True
BluetoothEndPoint ep = new BluetoothEndPoint(addr, serviceClass);
BluetoothClient conn = new BluetoothClient(ep); //10049 error
conn.Connect(ep);
Console.WriteLine(conn.GetStream());
答案 0 :(得分:2)
这一切都在项目文档中有所涉及。 : - )
简而言之,删除SetServiceState
行不必要/不好。每次进行配对也是不必要的,有点慢但如果运作良好可能不值得改变。
文档:
1)http://32feet.codeplex.com/documentation
2)http://32feet.codeplex.com/wikipage?title=General%20Bluetooth%20Data%20Connections
BluetoothAddress addr
= BluetoothAddress.Parse("001122334455");
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
// - or - etc
// serviceClass = MyConsts.MyServiceUuid
//
var ep = new BluetoothEndPoint(addr, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write/Read ...
3)http://32feet.codeplex.com/wikipage?title=Errors
即。错误的服务类ID。
答案 1 :(得分:0)
这是最终如何滚动的。
device.SetServiceState(serviceClass, true); //do it before pairing
...
BluetoothClient conn = new BluetoothClient();
conn.Connect(ep);
另外,我的错误在这里:
static Guid serviceClass = new Guid("4d36e978-e325-11ce-bfc1-08002be10318");
//GUID of device class
应该是:
static Guid serviceClass = new Guid("00001101-0000-1000-8000-00805f9b34fb");
//GUID of bluetooth service
要查看正确的GUID,请参阅您的设备(不是加密狗的)设置/属性。你可以在Windows上看到它们。