2013-05-10 33 views
0

目前,我正在C#2010中編程USB藍牙適配器。我想以這種方式進行編程,使其可以自動與找到的藍牙設備配對。我不希望用戶手動接受手機以及Windows 7中的配對請求。我正在使用我的手機(X Peria S)對此進行測試。這種編程方法是可行的嗎?我試圖代碼,藍牙這種使用32feet.net庫,這裏是我的代碼帶有Windows 7和手機的自動(藍牙配對)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

using InTheHand.Net; 
using InTheHand.Net.Bluetooth; 
using InTheHand.Net.Sockets; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

    private Guid service = BluetoothService.BluetoothBase; 
    private BluetoothClient bluetoothClient; 




    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Search_Bluetooth(object sender, EventArgs e) 
    { 
     BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable; 
     bluetoothClient = new BluetoothClient(); 
     Cursor.Current = Cursors.WaitCursor; 

     BluetoothDeviceInfo [] bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10); 
     comboBox1.DataSource = bluetoothDeviceInfo; 
     comboBox1.DisplayMember = "DeviceName"; 
     comboBox1.ValueMember = "DeviceAddress"; 
     comboBox1.Focus(); 
     Cursor.Current = Cursors.Default; 
    } 

    private void Pair(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedValue != null) 
     { 
      try 
      { 
       bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service)); 
       MessageBox.Show("Connected"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 


      } 
     } 
    } 
} 
} 

當我運行這個項目中,我看到在藍牙設備列表中,但周圍當過我要配對它給我一個錯誤說「連接嘗試失敗,因爲連接的方沒有正確響應一段時間

我認爲這個問題是私人的Guid =服務BluetoothService.BluetoothBase,但我不知道,我是使用正確的服務.BluetoothBase與我的手機配對?

這是否有任何現有的解決方案?任何幫助和建議,高度讚賞。

謝謝。

+0

我一時舉不出任何具體文件,但我很確定這不是藍牙的工作原理。用戶需要有一個協議來配對他們的設備,否則如果一個設備可以在未經他們許可的情況下與他們配對,那麼這將會是一個安全漏洞。 – tnw 2013-05-10 15:26:38

+0

你好Salomonder, – 2013-05-10 18:21:44

回答

1

您必須知道身份驗證過程中將要求加密狗的PIN碼。

如果要連接到例如一個移動藍牙RS-232適配器,您必須知道PIN碼,但由於缺少用戶界面,您不必接受遠程設備(RS-232適配器)上的連接。但在手機上,你必須。

我寫了下面的界面:

interface IStackAdapter 
{ 
    IList<IRemoteBTDevice> DiscoveredDevices { get; } 
    void LoadStack(); 
    void DoInquiry(); 
    void DoConnection(IRemoteBTDevice rd); 
    void ReleaseLink(); 
} 

接下來,我實現了這個接口爲每個不同的藍牙堆棧。下面是一個Widcomm堆棧連接:

/// <summary> 
/// Connects to a remote device. 
/// </summary> 
/// <param name="rd">Remote device that the adapter is supposed to connect to.</param> 
public void DoConnection(IRemoteBTDevice rd) 
{ 
    BluetoothAddress remoteAddress = new BluetoothAddress(Convert.ToInt64(rd.Id, 16)); 
    BluetoothDeviceInfo bdi = new BluetoothDeviceInfo(remoteAddress); 

    try 
    { 
     if (!bdi.Authenticated) 
     { 
      string pair = rd.Pin; /* PIN for your dongle */ 
      bool paired = BluetoothSecurity.PairRequest(bdi.DeviceAddress, pair); 
     } 
    } 
    catch (Exception ex) 
    { 
     //Log and rethrow 
    } 
} 
+0

你好Salomonder, 我是新的藍牙編程,所以我沒有太多的知識。我正在使用Microsoft堆棧加密狗。那麼你的意思是我需要有PIN碼才能連接到設備,用戶必須接受手機中的PIN碼請求? 我並沒有真正瞭解您發佈的代碼,如果您可以發佈您在案例中用於連接的整個代碼,這將會很有幫助。 – 2013-05-10 18:30:07

+0

遠程設備的藍牙堆棧並不那麼重要。在編寫代碼之前,你必須找出你的客戶端使用的是什麼棧。 32feet提供相應的方法/屬性。任何問題? – Salomonder 2013-05-10 19:06:54

0

如果您使用的是Windows手機您可以使用PeerFinder在Windows手機連接:

PeerFinder.AlternateIdentities["Bluetooth:PAIRED"] = ""; 
     var available_devices = await PeerFinder.FindAllPeersAsync(); 
     HostName hostName = null; 
     for (int i = 0; i < available_devices.Count; i++) 
     { 
      PeerInformation dispositivo = available_devices[i]; 
      if (dispositivo.DisplayName.ToUpper() == /*Name of you device */) 
      { 
       hostName = dispositivo.HostName; 
       break; 
      } 
     } 
     if (hostName != null) 
     { 
      var socket = new StreamSocket(); 
      await socket.ConnectAsync(hostName, "1"); 
     }