我正在尝试启动并运行蓝牙套接字连接但由于某种原因我的客户端无法连接 .
更确切地说,当我尝试连接到流时,我遇到异常:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接主机无法响应而 Build 连接失败 .
我在网上找到的所有例子并没有真正解决我的问题,而且我目前还不确定问题的来源 . 扫描和配对工作正常 - 我看到有问题的蓝牙设备成功配对 .
我尝试连接首先设置客户端,然后调用连接
客户端蓝牙名称,地址和引脚已知:
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;
}
然后是异步连接:
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();
}
}
本地创建m_localBluetoothEndpoint和m_localBluetoothClient是这样的,尽管Endpoint或多或少是新的(在我使用BluetoothCLient之前没有参数):
m_localBluetoothEndpoint = new BluetoothEndPoint(BluetoothRadio.PrimaryRadio.LocalAddress, BluetoothService.SerialPort);
m_localBluetoothClient = new BluetoothClient(m_localBluetoothEndpoint);
我也尝试使用Listener,以防远程设备想要连接但是从不调用回调:
public void SetupListener()
{
var listener = new BluetoothListener(BluetoothService.SerialPort);
listener.Start();
listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallbackTwo, listener);
}
任何人都可以告诉我上面的连接方法是否有任何问题,以及如何弄清楚为什么我得到上面提到的异常?
这里抛出异常:
m_localBluetoothClient.EndConnect(result);
我还不明白的是,对remoteCLient的SupportedServices调用返回0 guids - 因此设备没有列出任何蓝牙服务 .
m_remoteBluetoothClient.InstalledServices()
谢谢