首页 文章

与Windows 7和手机自动(蓝牙配对)

提问于
浏览
0

目前我在C#2010中编程USB蓝牙适配器 . 我想以这样一种方式编程,它自动与找到的蓝牙设备配对 . 我不希望用户在手机和Windows 7中手动接受配对请求 . 我正在使用我的手机(X Peria S)来测试这一点 . 这种编程方法是否可行?我尝试使用32feet.net库为蓝牙编码,这是我的代码

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


            }
        }
    }
}
}

当我运行这个项目时,我看到周围的蓝牙设备列表,但是当我想要配对时它给我一个错误说“ A connection attempt failed because the connected party did not properly respond a period of time

我认为问题是 private Guid service = BluetoothService.BluetoothBase 但我不确定,我使用正确的 service .BluetoothBase 与我的手机配对吗?

有没有现成的解决方案?任何帮助和建议都非常感谢 .

谢谢 .

2 回答

  • 1

    您必须知道在身份验证期间将要求的加密狗的PIN .

    如果你想连接到例如移动蓝牙RS-232加密狗,您必须知道PIN,但由于缺少用户界面,您不必接受远程设备(RS-232加密狗)上的连接 . 但是你必须在手机上 .

    我写了以下界面:

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

    接下来,我实现了该接口 for each different bluetooth stack . 这是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

    如果您使用Windows Phone,则可以使用Windows Phone中的PeerFinder进行连接:

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

相关问题