将蓝牙设备与具有 32feet .NET 蓝牙库的计算机配对 [英] Pair bluetooth devices to a computer with 32feet .NET Bluetooth library

查看:39
本文介绍了将蓝牙设备与具有 32feet .NET 蓝牙库的计算机配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您想了解如何使用32feet.NET库与蓝牙设备进行通信,请阅读解决方案

我目前正在尝试通过蓝牙在计算机和自建 .NET Gadgeteer 原型之间进行通信.

I am currently trying to communicate via bluetooth between a computer and a self-built .NET Gadgeteer prototype.

Gadgeteer 原型由主板、电源和蓝牙模块组成.模块处于可发现模式.

The Gadgeteer prototype consists of the mainboard, a power supply and a bluetooth module. The module is in discoverable mode.

计算机上正在运行基于 32feet .NET 蓝牙的自定义蓝牙程序.该程序检测范围内的所有蓝牙设备并尝试与它们配对.但是,目前这不是自动完成的,我必须输入设备的配对码.

On the computer a custom bluetooth program based on 32feet .NET Bluetooth is running. The program detects all bluetooth devices in range and tries to pair with them. However, this is not done automatically at the moment, I have to enter a pairing code for the device.

如何在不输入配对码的情况下配对设备?

找到设备,问题是配对部分.我尝试了很多,但没有找到解决方案...

Devices are found, the problem is the pairing part. I experimented a lot, but didn't find a solution...

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 以防止这种情况,但我没有弄明白.

This code block initiates the pairing and it works, but Windows is asking me to enter the pairing code for the device. I read about the BluetoothWin32Authentication to prevent this case but I don't get it right.

private void HandleRequests(object that, BluetoothWin32AuthenticationEventArgs e)
{
    e.Confirm = true;
}

这是事件处理程序的代码(http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)

This is the code of the event handler (http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)

如果您只是想在 SSP 设备连接时允许配对继续进行,那么处理回调并设置 e.Confirm=True 就足够了——但这有点不安全......

If you simply want to allow the pairing to go ahead when to SSP devices are connecting then handling the callback and setting e.Confirm=True will be enough -- but that is a little insecure...

<小时>

我很困惑-.- 目标是应用程序和gadgeteer 模块可以在没有任何用户干扰的情况下双向发送数据.


I am confused -.- The goal is that the application and the gadgeteer module can send data in both directions without any user interference.

在没有用户交互的情况下我无法自动配对设备是真的吗?

如果两台设备已经配对,它们是否可以在没有用户交互的情况下交换数据?

推荐答案

我想出了如何解决我的问题,现在我对蓝牙连接的了解更多了.如果其他人对此有疑问,我会提供我的解决方案.代码示例表示带有 32feet 蓝牙库的蓝牙控制器的 C# 实现.

I figured out how to solve my problems and my knowledge about Bluetooth connections is a bit bigger now. If someone else has problems with that, I provide my solution. The code examples represent the C# implementation of a bluetooth controller with the 32feet Bluetooth library.

扫描

这意味着检测到范围内的设备.我的代码:

This means that devices in range are detected. My code:

// mac is mac address of local bluetooth device
BluetoothEndPoint localEndpoint = new BluetoothEndPoint(mac, BluetoothService.SerialPort);
// client is used to manage connections
BluetoothClient localClient = new BluetoothClient(localEndpoint);
// component is used to manage device discovery
BluetoothComponent localComponent = new BluetoothComponent(localClient);
// async methods, can be done synchronously too
localComponent.DiscoverDevicesAsync(255, true, true, true, true, null);
localComponent.DiscoverDevicesProgress += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesProgress);
localComponent.DiscoverDevicesComplete += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesComplete);

private void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
    // log and save all found devices
    for (int i = 0; i < e.Devices.Length; i++)
    {           
        if (e.Devices[i].Remembered)
        {
            Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is known");
        }
        else
        {
            Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is unknown");
        }
        this.deviceList.Add(e.Devices[i]);         
    }
}

private void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
    // log some stuff
}

配对

这意味着设备与本地蓝牙设备耦合.这需要通过输入双方的代码来完成一次.可以通过代码完成,这样用户甚至不会注意到添加了设备.我为此目的的代码:

This means that devices get coupled with the local bluetooth device. This needs to be done once by entering a code of both sides. Can be done via code so that the user doesn't even notice that a device was added. My code for this purpose:

// get a list of all paired devices
BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
// check every discovered device if it is already paired 
foreach (BluetoothDeviceInfo device in this.deviceList)
{
    bool isPaired = false;
    for (int i = 0; i < paired.Length; i++)
    {
        if (device.Equals(paired[i]))
        {
            isPaired = true;
            break;
        }
    }

    // if the device is not paired, pair it!
    if (!isPaired)
    {
        // replace DEVICE_PIN here, synchronous method, but fast
        isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, DEVICE_PIN);
        if (isPaired)
        {
            // now it is paired
        }
        else
        {
            // pairing failed
        }
    }
}

连接

这意味着建立连接并交换数据.再次一些代码:

This means establishing a connection and exchanging of data. Again some code:

// check if device is paired
if (device.Authenticated)
{
    // set pin of device to connect with
    localClient.SetPin(DEVICE_PIN);
    // async connection method
    localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);
}

// callback
private void Connect(IAsyncResult result)
{
    if (result.IsCompleted)
    {
        // client is connected now :)
    }
}

如果您保持扫描、配对、连接的顺序,一切都应该正常.要发送或接收数据,请使用 BluetoothClientGetStream() 方法.它提供了一个可以被操纵的网络流.

If you keep the order scan, pair, connect, everything should work fine. To send or receive data, use the GetStream() method of the BluetoothClient. It provides a network stream that can be manipulated.

接收连接

如果您希望另一台设备与您的设备连接,您需要监听传入的连接请求.这仅适用于设备之前已经配对的情况.我的代码:

If you want another device to connect with your device you need to listen to incoming connection requests. This only works if the device have already been paired before. My code:

BluetoothListener l = new BluetoothListener(LOCAL_MAC, BluetoothService.SerialPort);
l.Start(10);
l.BeginAcceptBluetoothClient(new AsyncCallback(AcceptConnection), l);

void AcceptConnection(IAsyncResult result){
    if (result.IsCompleted){
        BluetoothClient remoteDevice = ((BluetoothListener)result.AsyncState).EndAcceptBluetoothClient(result);    
    }    
}

用有效的蓝牙地址替换LOCAL_MAC(例如,使用BluetoothAddress.Parse();).设备连接后,它们可以通过底层流交换消息.如果连接不起作用,则可能存在身份验证问题,因此请尝试在侦听器中设置本地设备 pin (l.SetPin(LOCAL_MAC, MY_PASSWORD);

Replace LOCAL_MAC with a valid BluetoothAddress (e.g. by using BluetoothAddress.Parse();). After the devices are connected they can exchange messages via the underlying stream. If the connection does not work there might be authentication issues, so try setting the local device pin in the listener (l.SetPin(LOCAL_MAC, MY_PASSWORD);

这篇关于将蓝牙设备与具有 32feet .NET 蓝牙库的计算机配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆