I have a project to connect bluetooth between C# application and my phone, and use AT command through Stream to dial number on my app.
I have seen https://github.com/sidiandi/Sidi.HandsFree but I cannot understand this code.
I try scan and pair, it work well but I cannot connect.
Code:
private void button2_Click(object sender, EventArgs e) {
BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
foreach (BluetoothDeviceInfo device in devices) {
bool isPaired = false;
for (int i = 0; i < paired.Length; i++) {
if (device.Equals(paired[i])) {
isPaired = true;
break;
}
}
// if the device is not paired, pair it
if (!isPaired) {
// replace DEVICE_PIN here, synchronous method, but fast
isPaired = BluetoothSecurity.PairRequest(BluetoothAddress.Parse(Addconnect), null);
if (isPaired) {
// now it is paired
} else {
// pairing failed
}
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
Addconnect = dataGridView1.CurrentCell.Value.ToString();
}
private void button3_Click(object sender, EventArgs e) {
foreach (BluetoothDeviceInfo device in devices) {
if (device.DeviceAddress.ToString() == Addconnect) {
try {
localClient.SetPin(null);
localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);
}
catch {
}
}
}
}