0

Currently i am programming USB Bluetooth Dongle in C# 2010. I want to program in such a way that it automatically pairs with the Bluetooth device found. I don't want the user to manually accept the pairing request in both Mobile phone as well as in Windows 7. I am using my phone (X Peria S) to test this. Is this method of programming possible? I tried to code this using 32feet.net library for the Bluetooth, here is my code

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


            }
        }
    }
}
}

When i run this project i see the list of Bluetooth device in the surrounding but when ever i want to pair with it gives me an error saying "A connection attempt failed because the connected party did not properly respond a period of time"

I think the problem is private Guid service = BluetoothService.BluetoothBase but i am not sure, am i using the right service .BluetoothBase to pair with my phone?

Is there any existing solution for this? Any help and suggestion is highly appreciated.

Thanks.

  • I can't cite any specific documentation, but I'm pretty sure that's not how bluetooth works. There needs to be an agreement from the user to pair their device, otherwise that would be a bit of a security breech if a device could pair with theirs without their permission. – tnw May 10 '13 at 15:26

2 Answers2

1

You have to know the PIN for your dongle that will be requested during authentication.

If you want to connect to e.g. a mobile bluetooth RS-232 dongle, you have to know the PIN, but you don't have to accept the connection on the remote device (RS-232 dongle) because of the lack of a user interface. But on a mobile phone you have to.

I wrote the following interface:

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

Next, I implemented that interface for each different bluetooth stack. Here is the connection for a Widcomm stack:

/// <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
    } 
}
Salomonder
  • 336
  • 1
  • 6
  • Hello Salomonder, I am new in bluetooth programming so i don't have much knowledge. I am using Microsoft stack dongle. So what you mean is i need to have a PIN to connect to a device and the user have to accept the PIN request in the phone? I didn't really understood the code you posted, it would be helpful if you could post the whole code that you used in your case for connection. – Electronic Curious May 10 '13 at 18:30
  • The bluetooth stack of the remote device isn't that important. Before writing code, you have to find out what stack your client is using. 32feet provide the corresponding methods/properties. Any questions? – Salomonder May 10 '13 at 19:06
0

If you use a Windows Phone you can use the PeerFinder in Windows Phone to connect:

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