Hi,
I have the chat client sample running in Authentication mode and that seems to work fine, my device listens and accepts the Auth. connection.
Ultimately I need to have the PC behaving as Multiple Secure Servers for SPP, so my plan is to take the OPPSecureServer and MultiBtOPPServers samples and modify them for the serial data connection.
Unfortunately I'm running into a problem in getting the Auth. connection to work. Upon my device to connect to the server the server receives the PIN request on the wclAuthenticator_OnPINRequest event and supplies the correct pin, but then there is no connection and my device throws out the following error: RFC_RES_ACK_TIMEOUT.
In the Bluetooth Device Explorer under windows the devices looks to be paired... am I missing something?
Thanks in advance, my code is below.
public partial class fmMain : Form
{
private wcl.wclAPI wclAPI;
private wcl.wclBluetoothDiscovery wclBluetoothDiscovery;
private wcl.wclServer wclServer;
private wcl.wclAuthenticator wclAuthenticator;
public fmMain()
{
InitializeComponent();
wclAPI = new wcl.wclAPI();
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.OnAuthenticationRequest += new wcl.wclAuthenticationRequestEventHandler(wclAuthenticator_OnAuthenticationRequest);
wclAuthenticator.OnPINRequest += new wcl.wclPINRequestEventHandler(wclAuthenticator_OnPINRequest);
wclServer.BluetoothParams.Authentication = true;
wclServer.BluetoothParams.Name = "WCL Serial Server";
}
private void fmMain_Load(object sender, EventArgs e)
{
wclAPI.Load();
}
private void fmMain_FormClosed(object sender, FormClosedEventArgs e)
{
wclAPI.Unload();
wclServer.Dispose();
wclBluetoothDiscovery.Dispose();
wclAPI.Dispose();
}
private wcl.wclBluetoothRadio GetSelectedRadio()
{
wcl.wclBluetoothRadio Radio = null;
wcl.wclBluetoothRadios Radios = new wcl.wclBluetoothRadios();
if (wclBluetoothDiscovery.EnumRadios(Radios) == wcl.wclErrors.WCL_E_SUCCESS && Radios.Count > 0)
Radio = new wcl.wclBluetoothRadio(Radios[0]);
if (Radio == null)
MessageBox.Show("No Bluetooth API was found.");
return Radio;
}
private void btListen_Click(object sender, EventArgs e)
{
if (wclServer.State != wcl.wclServerState.ssClosed)
MessageBox.Show("Server is active");
else
{
wcl.wclBluetoothRadio Radio = GetSelectedRadio();
if (Radio != null)
{
wclAuthenticator.Radio = Radio;
int Res = wclAuthenticator.Open();
if (Res != wcl.wclErrors.WCL_E_SUCCESS)
{
MessageBox.Show("Unable open Authenticator: " + wcl.wclErrors.wclGetErrorMessage(Res));
Radio = null;
}
else
{
wclServer.Transport = wcl.wclServerTransport.stBluetooth;
wclServer.BluetoothParams.Radio = Radio;
wclServer.BluetoothParams.Authentication = true;
wclServer.BluetoothParams.Encryption = false;
wclServer.BluetoothParams.Service = wcl.wclUUIDs.SerialPortServiceClass_UUID;
wcl.wclErrors.wclShowError(wclServer.Listen());
}
}
}
}
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)
{
wcl.wclBluetoothRadio Radio = GetSelectedRadio();
wcl.wclBluetoothDevice Device = new wcl.wclBluetoothDevice();
Device.Address = wclServer.Address;
Device.Unpair(Radio);
listBox.Items.Add("Disconnected " + wclServer.Address + " " + wclServer.DeviceName);
}
private void btClose_Click(object sender, EventArgs e)
{
wclServer.Close();
}
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)
{
e.PIN = "1234";
}
private void wclAuthenticator_OnAuthenticationRequest(object sender, wcl.wclAuthenticationRequestEventArgs e)
{
e.Answer = e.Value;
e.Confirm = true;
}
}