there is my dll code to connect:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Windows.Forms;
using InTheHand.Net;
using System.Runtime.InteropServices;
namespace LibrairieDeConnexionBluetoothAvecDLL
{
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("220081A3-4B8F-4834-A47D-0531877D483E"), ComVisible(true)]
public interface IBluetooth
{
bool connection();
void initialisation();
void detecterLesDispositifs();
void choisirUnDispositif();
void ajouterLesControleALaFentreDeConnexion(Form fenetreDeSelectionDuBluetooth, ListBox listeDesAdresseDesPériphérique, ListBox listeDesNomDesPériphérique, Button Connexion, Button refresh);
void confgurerFenetreDeConnexion(Form fenetreDeSelectionDuBluetooth);
void configurerLeBouton(Button Connexion, int marginGauche, int marginHaut, string nom, bool etat);
void configurerLaListe(ListBox liste, int with, int height, int marginGauche, int marginHaut, bool enable);
void seConnecter();
void Win32AuthCallbackHandler(object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e);
void listeDesNomDesPériphérique_SelectedIndexChanged(object sender, EventArgs e);
void Refresh_Click(object sender, EventArgs e);
void Connexion_Click(object sender, EventArgs e);
}
[ClassInterface(ClassInterfaceType.None), Guid("90A2335C-0310-49C1-AF57-E9BF993647D5"), ProgId("LibrairieDeConnexionBluetoothAvecDLL.Bluetooth"), ComVisible(true)]
public class Bluetooth : UserControl, IBluetooth
{
private BluetoothClient listeDePeripheriqueBluetooth;
private BluetoothClient SerialPort;
private BluetoothDeviceInfo optifive;
private Guid service;
private BluetoothDeviceInfo[] bdi;
private BluetoothRadio radio;
private Form fenetreDeSelectionDuBluetooth;
private ListBox listeDesNomDesPériphériques;
private ListBox listeDesAdresseDesPériphériques;
private Button Connexion;
private Button Refresh;
public bool connection()
{
initialisation();
detecterLesDispositifs();
listeDesNomDesPériphériques.SelectedIndexChanged += new System.EventHandler(
listeDesNomDesPériphérique_SelectedIndexChanged);
Refresh.Click += new System.EventHandler(Refresh_Click);
Connexion.Click += new System.EventHandler(Connexion_Click);
return optifive.Connected;
}
public void initialisation()
{
radio = BluetoothRadio.PrimaryRadio;
if (radio != null && radio.Mode == RadioMode.PowerOff)
{
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
}
listeDePeripheriqueBluetooth = new BluetoothClient();
service = BluetoothService.SerialPort;
fenetreDeSelectionDuBluetooth = new Form();
confgurerFenetreDeConnexion(fenetreDeSelectionDuBluetooth);
listeDesNomDesPériphériques = new ListBox();
listeDesAdresseDesPériphériques = new ListBox();
Connexion = new Button();
Refresh = new Button();
configurerLaListe(listeDesNomDesPériphériques, 200, 300, 5, 5, true);
configurerLaListe(listeDesAdresseDesPériphériques, 200, 300, 220, 5, false);
configurerLeBouton(Connexion, 220, 310, "Connexion", false);
configurerLeBouton(Refresh, 5, 310, "Refresh", true);
ajouterLesControleALaFentreDeConnexion(fenetreDeSelectionDuBluetooth, listeDesAdresseDesPériphériques, listeDesNomDesPériphériques, Connexion, Refresh);
fenetreDeSelectionDuBluetooth.Show();
}
public void detecterLesDispositifs()
{
Cursor.Current = Cursors.WaitCursor;
bdi = listeDePeripheriqueBluetooth.DiscoverDevices();
if (listeDesAdresseDesPériphériques.Items.Count>0)
listeDesAdresseDesPériphériques.Items.Clear();
if(listeDesNomDesPériphériques.Items.Count>0)
listeDesNomDesPériphériques.Items.Clear();
bdi.ToList().ForEach(delegate(BluetoothDeviceInfo Device)
{
listeDesNomDesPériphériques.Items.Add(Device.DeviceName);
listeDesAdresseDesPériphériques.Items.Add(Device.DeviceAddress.ToString().Insert(10, ":").Insert(8, ":").Insert(6, ":").Insert(4, ":").Insert(2, ":"));
});
Cursor.Current = Cursors.Default;
}
public void choisirUnDispositif()
{
bdi.ToList().ForEach(delegate(BluetoothDeviceInfo device)
{
if (device.DeviceName == listeDesNomDesPériphériques.SelectedItem.ToString().Replace(":", ""))
optifive = device;
});
}
public void ajouterLesControleALaFentreDeConnexion(Form fenetreDeSelectionDuBluetooth, ListBox listeDesAdresseDesPériphérique, ListBox listeDesNomDesPériphérique, Button Connexion, Button refresh)
{
fenetreDeSelectionDuBluetooth.Controls.Add(listeDesNomDesPériphérique);
fenetreDeSelectionDuBluetooth.Controls.Add(listeDesAdresseDesPériphérique);
fenetreDeSelectionDuBluetooth.Controls.Add(Connexion);
fenetreDeSelectionDuBluetooth.Controls.Add(refresh);
}
public void confgurerFenetreDeConnexion(Form fenetreDeSelectionDuBluetooth)
{
fenetreDeSelectionDuBluetooth.StartPosition = FormStartPosition.CenterScreen;
fenetreDeSelectionDuBluetooth.Height = 378;
fenetreDeSelectionDuBluetooth.Width = 445;
fenetreDeSelectionDuBluetooth.Text = "Connexion Bluetooth";
}
public void configurerLeBouton(Button Connexion, int marginGauche, int marginHaut, string nom, bool etat)
{
Connexion.Left = marginGauche;
Connexion.Top = marginHaut;
Connexion.Width = 100;
Connexion.Text = nom;
Connexion.Enabled = etat;
}
public void configurerLaListe(ListBox liste, int with, int height, int marginGauche, int marginHaut, bool enable)
{
liste.Height = height;
liste.Width = with;
liste.Left = marginGauche;
liste.Top = marginHaut;
liste.Enabled = enable;
if (liste == null)
MessageBox.Show("n'éxiste pas");
else
MessageBox.Show("éxiste");
}
public void seConnecter()
{
try
{
BluetoothWin32Authentication authetification = new BluetoothWin32Authentication(Win32AuthCallbackHandler);
SerialPort = new BluetoothClient();
SerialPort.Connect(new BluetoothEndPoint(optifive.DeviceAddress, service));
}
catch (Exception ex)
{
MessageBox.Show("n'arrive pas a se connecter : " + ex.Message);
}
}
public void Win32AuthCallbackHandler(object sender, InTheHand.Net.Bluetooth.BluetoothWin32AuthenticationEventArgs e)
{
string address = e.Device.DeviceAddress.ToString();
Console.WriteLine("Received an authentication request from address " + address);
e.Pin = "0000";
e.Confirm = true;
}
public void listeDesNomDesPériphérique_SelectedIndexChanged(object sender, EventArgs e)
{
choisirUnDispositif();
listeDesAdresseDesPériphériques.SelectedIndex = listeDesNomDesPériphériques.SelectedIndex;
Connexion.Enabled = true;
}
public void Refresh_Click(object sender, EventArgs e)
{
detecterLesDispositifs();
}
public void Connexion_Click(object sender, EventArgs e)
{
seConnecter();
}
}
}