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

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

问题描述

如果您想知道如何使用32feet.NET库与蓝牙设备通信,读解

目前我正在试图通过一台电脑和一个自建.NET摆弄小玩意原型之间的蓝牙通信。

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

的摆弄小玩意原型包括主板,一个电源和一个蓝牙模块。该模块处于可发现模式。

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

在基于.NET32英尺蓝牙定制蓝牙程序运行的计算机。该方案在检测范围内的所有蓝牙设备并尝试配对他们。但是,这不是自动的时刻做了,我必须输入配对code的设备。

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.

如何配对设备,而无需进入配对code?

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

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);
    }
}

这code座开始配对和它的作品,但是Windows要求我输入配对code的设备。我看了一下BluetoothWin32Authentication至prevent这种情况下,但我不明白它的权利。

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;
}

这是事件处理程序的code(<一个href=\"http://32feet.$c$cplex.com/wikipage?title=BluetoothWin32Authentication\">http://32feet.$c$cplex.com/wikipage?title=BluetoothWin32Authentication)

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

如果您只是想允许配对继续前进时,SSP设备的连接,然后处理回调并设置e.Confirm =真就足够了 - 但是这是一个有点不安全...

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...

我感到困惑-.-的目标是,在应用程序和摆弄小玩意模块可以发送在两个方向上的数据而无需任何用户干预。


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

这是真的,我不能没有用户交互的自动配对设备?

这是真的,如果两个设备已经配对,他们可以在没有用户交互的数据交换?

推荐答案

我想通了,如何解决我的问题,我对蓝牙连接的知识是有点大了。如果其他人有那个问题,我提供我的解决方案。在code例子重新present与32英尺蓝牙库的蓝牙控制器的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.

扫描

这意味着,检测范围内的设备。我的code:

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
}

配对

这意味着,设备得到再加上本地蓝牙设备。这需要通过输入双方的code做一次。可以通过code来完成,以使用户甚至不会注意到,加入的装置。我的code用于此目的:

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
        }
    }
}

连接

这意味着建立连接和数据交换的。又有些code:

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 :)
    }
}

如果你保持这个顺序扫描,对,连接,一切都应该正常工作。发送或接收数据时,使用的 BluetoothClient GetStream()方法。它提供了一个可以被操纵一个网络流中。

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.

接收到连接

如果你想其他设备与您的设备连接,你需要听传入的连接请求。仅当该设备已经配对之前的作品。我的code:

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(例如,通过使用 BluetoothAddress.Parse(); )。设备连接之后,他们可以通过底层流交换消息。如果连接不工作可能有认证的问题,所以尽量设置本地器件引脚的监听器( 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);

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

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