Bluetooth client connection to 32feet.NET not working all the time
I am trying to connect to a bluetooth connection, but for some reason my client is not connecting.
More precisely, I get an exception when I try to connect to the stream: The connection attempt failed because the related party did not respond properly after a while or the connection failed because the connected host was unable to respond.
All the examples I found on the internet didn't actually solve my problem, and I'm currently not entirely sure where this problem comes from. Scanning and pairing are working fine - I can see that the corresponding bluetooth device is successfully paired.
I am trying to connect through the first client setup and then call connect
The name, address and contact name of the Bluetooth client are known:
public bool SetClient(String clientName, String btAddress, String pin)
{
bool retVal = false;
m_remoteBluetoothClient = new BluetoothDeviceInfo(BluetoothAddress.Parse(btAddress));
m_localBluetoothClient.SetPin(pin);
if (m_remoteBluetoothClient.Authenticated)
{
//m_localBluetoothClient.Authenticate = true;
retVal = true;
}
else
{
if (BluetoothSecurity.PairRequest(m_remoteBluetoothClient.DeviceAddress, pin))
{
retVal = true;
}
}
return retVal;
}
Then an asynchronous connection:
private void ClientConnectThread()
{
m_localBluetoothClient.BeginConnect(m_remoteBluetoothClient.DeviceAddress, BluetoothService.SerialPort, Connect, m_localBluetoothClient);
}
private void Connect(IAsyncResult result)
{
if (result.IsCompleted)
{
m_localBluetoothClient.EndConnect(result);
mBtStream = m_localBluetoothClient.GetStream();
}
}
The locales m_localBluetoothEndpoint and m_localBluetoothClient are created this way, although the endpoint is more or less new (before I used BluetoothCLient without a parameter):
m_localBluetoothEndpoint = new BluetoothEndPoint(BluetoothRadio.PrimaryRadio.LocalAddress, BluetoothService.SerialPort);
m_localBluetoothClient = new BluetoothClient(m_localBluetoothEndpoint);
I also tried to work with the Listener in case remote devices want to connect, but the callback is never called:
public void SetupListener()
{
var listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();
listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallbackTwo, listener);
}
Can anyone tell me if there is something wrong with my connection and how can I figure out why I got the exception mentioned above?
The exception gets here:
m_localBluetoothClient.EndConnect(result);
What I also don't understand is that the SupportedServices call to remoteCLient returned 0 commands - so the device didn't list any bluetooth services.
m_remoteBluetoothClient.InstalledServices()
thank
source to share
No one has answered this question yet
Check out similar questions: