2013-05-28 85 views
20

如果你想知道如何使用32feet.NET库与蓝牙设备通信,阅读解决方案配对蓝牙设备与32英尺.NET蓝牙库


目前我想电脑通过蓝牙在计算机和自建的.NET Gadgeteer原型之间进行通信。 Gadgette原型由主板,电源和蓝牙模块组成。该模块处于可发现模式。

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

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

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

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 =真就足够了 - 但是这是一个有点不安全......


我很困惑-.-的目标是,应用程序和摆弄小玩意MOD可以在两个方向上发送数据,而不会有任何用户干扰。

确实,我无法在没有用户交互的情况下自动配对设备?

确实如果两个设备已经配对,他们可以在没有用户交互的情况下交换数据吗?

+1

在Windows 10中,这不起作用。操作系统提示我“点击以设置设备” - 我从来没有使用上面的代码获得回调到“HandleRequests”事件处理程序。有关如何绕过此提示的任何想法? – TheJeff

+0

是的,正如TheJeff所说,配对在Windows 10中不起作用,设备已配对,但它不可见'播放设备',有什么办法解决它? – sebastso

+0

系统要求没有列出Windows 10,只有Windows 8。也许这是不可能的。 – xmashallax

回答

34

我想出了如何解决我的问题,现在我对蓝牙连接的知识有点大了。如果其他人遇到问题,我提供我的解决方案。代码示例代表32feet蓝牙库的蓝牙控制器的C#实现。

扫描

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

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

配对

这意味着设备得到再加上本地的蓝牙设备。这需要通过输入双方的代码来完成。可以通过代码完成,以便用户甚至不会注意到已添加设备。我为了这个目的代码:

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

连接

这意味着建立连接和数据交换的。再一些代码:

// 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()方法。它提供了可以被操纵的网络流。

接收到连接

如果想要另一个设备与您的设备,你需要听传入的连接请求连接。这只适用于设备之前已经配对的情况。我的代码:

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

+0

您是否还需要自己的代码才能在远程设备上运行? –

+1

@xmarshallax Socket未通过BeginConnect方法连接.Socket.connected状态为false,这就是为什么Getstream()方法无法发送或接收数据。 – Harsh

+0

这是一个问题还是一个陈述?上面的代码适用于我 – xmashallax