Thanks Mike.
I have found that the following code works for me across various devices/bt versions:
Server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BlueChatServer
{
public partial class fmMain : Form
{
private wcl.wclAPI wclAPI;
private wcl.wclBluetoothDiscovery wclBluetoothDiscovery;
private wcl.wclServer wclServer;
private wcl.wclAuthenticator wclAuthenticator;
private wcl.wclBluetoothRadio Radio = null;
private bool SelectRadio()
{
bool Result = false;
wcl.wclBluetoothRadios Radios = new wcl.wclBluetoothRadios();
int Res = wclBluetoothDiscovery.EnumRadios(Radios);
if (Res != wcl.wclErrors.WCL_E_SUCCESS)
{
//lbLog.Items.Add("Enum radios error: " + wcl.wclErrors.wclGetErrorMessage(Res));
if (Radio != null)
{
Radio = null;
Result = true;
}
}
else
{
if (Radios.Count == 0)
{
//lbLog.Items.Add("No radios were found");
if (Radio != null)
{
Radio = null;
Result = true;
}
}
else
if (Radio != null)
{
if (Radios[0].API != Radio.API)
{
Result = true;
Radio.Assign(Radios[0]);
}
}
else
{
Result = true;
Radio = new wcl.wclBluetoothRadio();
Radio.Assign(Radios[0]);
}
}
Radios = null;
return Result;
}
public fmMain()
{
InitializeComponent();
wclAPI = new wcl.wclAPI();
wclAPI.AfterLoad += new System.EventHandler(wclAPI_AfterLoad);
wclAPI.OnChanged += new System.EventHandler(wclAPI_OnChanged);
wclAPI.AfterUnload += new System.EventHandler(wclAPI_AfterUnload);
wclBluetoothDiscovery = new wcl.wclBluetoothDiscovery();
wclServer = new wcl.wclServer();
wclServer.OnClosed += new System.EventHandler(wclServer_OnClosed);
wclServer.OnData += new wcl.wclDataEventHandler(wclServer_OnData);
wclServer.OnDisconnected += new System.EventHandler(wclServer_OnDisconnected);
wclServer.OnConnected += new System.EventHandler(wclServer_OnConnected);
wclServer.OnListen += new wcl.wclConnectEventHandler(wclServer_OnListen);
wclAuthenticator = new wcl.wclAuthenticator();
wclAuthenticator.OnNumericComparison += new wcl.wclNumericComparisonEventHandler(wclAuthenticator_OnNumericComparison);
wclAuthenticator.OnPasskey += new wcl.wclPasskeyEventHandler(wclAuthenticator_OnPasskey);
wclAuthenticator.OnPINRequest += new wcl.wclPINRequestEventHandler(wclAuthenticator_OnPINRequest);
}
private void StartListener()
{
if (wclServer.State != wcl.wclServerState.ssClosed)
MessageBox.Show("Server is active");
else
{
if (Radio != null)
{
wclAuthenticator.Radio = Radio;
int Res = wclAuthenticator.Open();
if (Res != wcl.wclErrors.WCL_E_SUCCESS)
{
//lbLog.Items.Add("Unable open Authenticator: " + wcl.wclErrors.wclGetErrorMessage(Res));
Radio = null;
}
else
{
//lbLog.Items.Add("Authenticator has been opened");
wclServer.Transport = wcl.wclTransport.trBluetooth;
wclServer.BluetoothParams.Radio = Radio;
wclServer.BluetoothParams.Authentication = true;
wclServer.BluetoothParams.Encryption = true;
wclServer.BluetoothParams.Service = new Guid("{71D47FA1-87B6-4629-AB78-77B482AE5316}");
wcl.wclErrors.wclShowError(wclServer.Listen());
}
}
}
}
private void StopListener()
{
if (wclAuthenticator.Active == true)
{
wclAuthenticator.Close();
}
wclAuthenticator.Radio = null;
if (wclServer.State != wcl.wclServerState.ssClosed)
{
wclServer.Close();
}
}
private void fmMain_Load(object sender, EventArgs e)
{
wclServer.BluetoothParams.Authentication = true;
wclServer.BluetoothParams.Name = "WCL OPP Server";
wclAPI.Load();
}
private void fmMain_FormClosed(object sender, FormClosedEventArgs e)
{
StopListener();
wclAPI.Unload();
wclAuthenticator.Dispose();
wclServer.Dispose();
wclBluetoothDiscovery.Dispose();
wclAPI.Dispose();
Radio.Dispose();
}
private void wclAPI_AfterLoad(object sender, EventArgs e)
{
SelectRadio();
StartListener();
}
private void wclAPI_AfterUnload(object sender, EventArgs e)
{
MessageBox.Show("wclAPI_AfterUnload");
}
private void wclAPI_OnChanged(object sender, EventArgs e)
{
MessageBox.Show("wclAPI_OnChanged");
}
void wclAuthenticator_OnPasskey(object sender, wcl.wclNumericComparisonEventArgs e)
{
e.Confirm = true;
}
void wclAuthenticator_OnNumericComparison(object sender, wcl.wclNumericComparisonEventArgs e)
{
e.Confirm = true;
}
private void wclServer_OnClosed(object sender, EventArgs e)
{
listBox.Items.Add("Closed");
}
private void wclServer_OnConnected(object sender, EventArgs e)
{
listBox.Items.Add("Connected: " + wclServer.Address + " " + wclServer.DeviceName);
}
private void wclServer_OnDisconnected(object sender, EventArgs e)
{
listBox.Items.Add("Disconnected " + wclServer.Address + " " + wclServer.DeviceName);
}
private void wclServer_OnListen(object sender, wcl.wclConnectEventArgs e)
{
listBox.Items.Add("Listen: " + e.Error.ToString());
}
private void btSend_Click(object sender, EventArgs e)
{
string str = edCmd.Text;
Encoding Coder = Encoding.ASCII;
byte[] Buf = Coder.GetBytes(str);
wcl.wclErrors.wclShowError(wclServer.Write(Buf, (uint)str.Length));
}
private void wclServer_OnData(object sender, wcl.wclDataEventArgs e)
{
Encoding Coder = Encoding.ASCII;
string str = Coder.GetString(e.Data);
listBox.Items.Add(str);
}
private void wclAuthenticator_OnPINRequest(object sender, wcl.wclPINRequestEventArgs e)
{
//lbLog.Items.Add("Device " + e.Device.Address + " requires authentication");
//if (edPIN.Text == "")
// edPIN.Text = "0000";
e.PIN = "0000";
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BlueChatClient
{
public partial class fmMain : Form
{
private wcl.wclAPI wclAPI;
//private wcl.wclAuthenticator wclAuthenticator;
private wcl.wclBluetoothDiscovery wclBluetoothDiscovery;
private wcl.wclClient wclClient;
private wcl.wclBluetoothRadio Radio = null;
private bool SelectRadio()
{
bool Result = false;
wcl.wclBluetoothRadios Radios = new wcl.wclBluetoothRadios();
int Res = wclBluetoothDiscovery.EnumRadios(Radios);
if (Res != wcl.wclErrors.WCL_E_SUCCESS)
{
//lbLog.Items.Add("Enum radios error: " + wcl.wclErrors.wclGetErrorMessage(Res));
if (Radio != null)
{
Radio = null;
Result = true;
}
}
else
{
if (Radios.Count == 0)
{
//lbLog.Items.Add("No radios were found");
if (Radio != null)
{
Radio = null;
Result = true;
}
}
else
if (Radio != null)
{
if (Radios[0].API != Radio.API)
{
Result = true;
Radio.Assign(Radios[0]);
}
}
else
{
Result = true;
Radio = new wcl.wclBluetoothRadio();
Radio.Assign(Radios[0]);
}
}
Radios = null;
return Result;
}
public fmMain()
{
InitializeComponent();
wclAPI = new wcl.wclAPI();
wclAPI.AfterLoad += new System.EventHandler(wclAPI_AfterLoad);
wclAPI.OnChanged += new System.EventHandler(wclAPI_OnChanged);
wclAPI.AfterUnload += new System.EventHandler(wclAPI_AfterUnload);
//wclAuthenticator = new wcl.wclAuthenticator();
//wclAuthenticator.OnPINRequest += new wcl.wclPINRequestEventHandler(wclAuthenticator_OnPINRequest);
//wclAuthenticator.OnNumericComparison += new wcl.wclNumericComparisonEventHandler(wclAuthenticator_OnNumericComparison);
//wclAuthenticator.OnPasskey += new wcl.wclPasskeyEventHandler(wclAuthenticator_OnPasskey);
wclBluetoothDiscovery = new wcl.wclBluetoothDiscovery();
wclBluetoothDiscovery.OnDiscoveryStarted += new wcl.wclBluetoothDiscoveryStartedEventHandler(wclBluetoothDiscovery_OnDiscoveryStarted);
wclBluetoothDiscovery.OnDiscoveryComplete += new wcl.wclBluetoothDiscoveryCompleteEventHandler(wclBluetoothDiscovery_OnDiscoveryComplete);
wclClient = new wcl.wclClient();
wclClient.OnDisconnect += new System.EventHandler(wclClient_OnDisconnect);
wclClient.OnData += new wcl.wclDataEventHandler(wclClient_OnData);
wclClient.OnConnect += new wcl.wclConnectEventHandler(wclClient_OnConnect);
}
private void wclAPI_AfterLoad(object sender, EventArgs e)
{
SelectRadio();
}
private void wclAPI_AfterUnload(object sender, EventArgs e)
{
MessageBox.Show("wclAPI_AfterUnload");
}
private void wclAPI_OnChanged(object sender, EventArgs e)
{
MessageBox.Show("wclAPI_OnChanged");
}
void wclAuthenticator_OnPasskey(object sender, wcl.wclNumericComparisonEventArgs e)
{
e.Confirm = true;
}
void wclAuthenticator_OnNumericComparison(object sender, wcl.wclNumericComparisonEventArgs e)
{
e.Confirm = true;
}
private void fmMain_Load(object sender, EventArgs e)
{
wclAPI.Load();
}
private void fmMain_FormClosed(object sender, FormClosedEventArgs e)
{
//if (wclAuthenticator.Active)
// wclAuthenticator.Close();
wclAPI.Unload();
wclClient.Dispose();
wclBluetoothDiscovery.Dispose();
//wclAuthenticator.Dispose();
wclAPI.Dispose();
}
private void btDiscover_Click(object sender, EventArgs e)
{
if (Radio != null)
{
wcl.wclErrors.wclShowError(wclBluetoothDiscovery.Discovery(Radio));
}
}
private void wclBluetoothDiscovery_OnDiscoveryStarted(object sender, wcl.wclBluetoothDiscoveryStartedEventArgs e)
{
lvDevices.Items.Clear();
MessageBox.Show("Started");
}
private void wclBluetoothDiscovery_OnDiscoveryComplete(object sender, wcl.wclBluetoothDiscoveryCompleteEventArgs e)
{
if (e.Devices == null)
MessageBox.Show("Error discovering");
else
if (e.Devices.Count == 0)
MessageBox.Show("Nothing found");
else
for (uint i = 0; i < e.Devices.Count; i++)
{
wcl.wclBluetoothDevice Device = e.Devices[i];
ListViewItem Item = lvDevices.Items.Add(Device.Address);
string str = "";
Device.GetName(e.Radio, ref str);
Item.SubItems.Add(str);
}
}
private void btConnect_Click(object sender, EventArgs e)
{
if (Radio != null)
{
if (lvDevices.SelectedItems.Count == 0)
MessageBox.Show("Selected device");
else
{
//wclAuthenticator.Radio = Radio;
//wclAuthenticator.Open();
wcl.wclBluetoothDevice Device = new wcl.wclBluetoothDevice();
string Address = lvDevices.SelectedItems[0].Text;
Device.Address = Address;
Device.Pair(Radio, "0000");
wclClient.BluetoothParams.Address = Address;
wclClient.BluetoothParams.Radio = Radio;
wclClient.BluetoothParams.Service = new Guid("{71D47FA1-87B6-4629-AB78-77B482AE5316}");
wclClient.BluetoothParams.Authentication = true;
wclClient.BluetoothParams.Encryption = true;
wclClient.Transport = wcl.wclTransport.trBluetooth;
wcl.wclErrors.wclShowError(wclClient.Connect());
}
}
}
private void wclAuthenticator_OnPINRequest(object sender, wcl.wclPINRequestEventArgs e)
{
e.PIN = "0000";
}
private void wclClient_OnConnect(object sender, wcl.wclConnectEventArgs e)
{
if (e.Error != wcl.wclErrors.WCL_E_SUCCESS)
MessageBox.Show("Unable connect: " + e.Error.ToString());
else
MessageBox.Show("Connected");
}
private void btDisconnect_Click(object sender, EventArgs e)
{
wclClient.Disconnect();
}
private void wclClient_OnDisconnect(object sender, EventArgs e)
{
MessageBox.Show("Disconnected");
}
private void btSend_Click(object sender, EventArgs e)
{
string str = edCmd.Text;
Encoding Coder = Encoding.ASCII;
byte[] Buf = Coder.GetBytes(str);
wcl.wclErrors.wclShowError(wclClient.Write(Buf, (uint)str.Length));
}
private void wclClient_OnData(object sender, wcl.wclDataEventArgs e)
{
Encoding Coder = Encoding.ASCII;
string str = Coder.GetString(e.Data);
edAns.Text = edAns.Text + "/r" + str;
}
}
}
To summarize what is going on there:
- wclAuthenticator running on server
- wclAuthenticator NOT running on client
- client and server are set to Authentication = True
- before connecting, client will Pair with the server
Would you consider this a viable workaround until you figure out what is going on with wclAuthenticator having issues when run on both client and server?