If you want to know how to use 32feet.NET library to communicate with bluetooth devices, read the solution
我目前正试图通过蓝牙在计算机和自建的.NET Gadgeteer原型之间进行通信 .
Gadgeteer原型包括主板,电源和蓝牙模块 . 模块处于可发现模式 .
在计算机上运行基于32feet .NET蓝牙的自定义蓝牙程序 . 程序检测范围内的所有蓝牙设备并尝试与它们配对 . 但是,目前还没有自动完成,我必须输入设备的配对代码 .
How can I pair devices without entering the pairing code?
找到设备,问题是配对部分 . 我做了很多实验,但没有找到解决方案......
foreach (BluetoothDeviceInfo device in this.deviceList)
{
try
{
//BluetoothClient client = new BluetoothClient(this.CreateNewEndpoint(localAddress));
//BluetoothEndPoint ep = this.CreateNewEndpoint(device.DeviceAddress);
EventHandler<BluetoothWin32AuthenticationEventArgs> handler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(HandleRequests);
BluetoothWin32Authentication auth = new BluetoothWin32Authentication(handler);
BluetoothSecurity.PairRequest(device.DeviceAddress, null);
}
}
此代码块启动配对并且它可以工作,但Windows要求我输入设备的配对代码 . 我读到了关于BluetoothWin32Authentication以防止这种情况,但我没有把它弄好 .
private void HandleRequests(object that, BluetoothWin32AuthenticationEventArgs e)
{
e.Confirm = true;
}
这是事件处理程序的代码(http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)
如果您只是想让SSP设备连接时允许配对继续,那么处理回调和设置e.Confirm = True就足够了 - 但这有点不安全......
我很困惑-.-目标是应用程序和gadgeteer模块可以在没有任何用户干扰的情况下向两个方向发送数据 .
Is it true that I can't pair devices automatically without user interaction?
Is it true that if two device were already paired they can exchange data without user interaction?
1 回答
我想出了如何解决我的问题,而且我对蓝牙连接的了解现在有点大了 . 如果其他人有问题,我提供我的解决方案 . 代码示例代表具有32英尺蓝牙库的蓝牙控制器的C#实现 .
Scanning
这意味着检测到范围内的设备 . 我的代码:
Pairing
这意味着设备与本地蓝牙设备耦合 . 这需要通过输入双方的代码来完成一次 . 可以通过代码完成,这样用户甚至不会注意到已添加设备 . 我的代码用于此目的:
Connecting
这意味着 Build 连接和交换数据 . 再一些代码:
如果您保持订单扫描,配对,连接,一切都应该正常工作 . 要发送或接收数据,请使用
BluetoothClient
的GetStream()
方法 . 它提供了可以操作的网络流 .Receiving a connection
如果您希望其他设备与您的设备连接,则需要侦听传入的连接请求 . 这仅适用于之前已配对的设备 . 我的代码:
将
LOCAL_MAC
替换为有效的BluetoothAddress(例如,使用BluetoothAddress.Parse();
) . 设备连接后,它们可以通过底层流交换消息 . 如果连接不起作用,则可能存在身份验证问题,因此请尝试在侦听器中设置本地设备引脚(l.SetPin(LOCAL_MAC, MY_PASSWORD);