WM CF进行蓝牙Bluetooth设备配对的开发_代码

?using System;

using System.Collections.Generic;
using https://www.360docs.net/doc/e412652412.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using https://www.360docs.net/doc/e412652412.html,;
using https://www.360docs.net/doc/e412652412.html,.Bluetooth;
using https://www.360docs.net/doc/e412652412.html,.Sockets;


namespace BluetoothPairing
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

BluetoothClient client = new BluetoothClient();
Dictionary deviceAddresses = new Dictionary();
private void menuItem2_Click(object sender, EventArgs e)
{
//Turn on the bluetooth
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
radio.Mode = RadioMode.Connectable;

//Scan the nearby devices
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
listBoxDevices.Items.Clear();
deviceAddresses.Clear();
foreach(BluetoothDeviceInfo device in devices)
{
listBoxDevices.Items.Add(device.DeviceName);
deviceAddresses[device.DeviceName] = device.DeviceAddress;
}
}

private void menuItem5_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void menuItem3_Click(object sender, EventArgs e)
{
try
{
BluetoothAddress deviceAddress = deviceAddresses[listBoxDevices.SelectedItem.ToString()];
client.Authenticate;
client.SetPin(deviceAddress, textBoxPin.Text.Trim());
client.Connect(deviceAddress, BluetoothService.Handsfree); //if connect ot Hands free.
//client.Connect(deviceAddress, BluetoothService.SerialPort); //if connect to cell phone and so forth.
MessageBox.Show("Pair successful.");

//transfer data.....
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

相关文档
最新文档