出于测试的目的,我试图通过我作为客户端制作的应用程序连接到我的手机,但我遗漏了一些东西。在配对设备之后,程序应该打开一个运行client.BeginConnect的新线程,但它只能运行“启动连接线程...”。
BluetoothDeviceInfo deviceInfo;
private void listBox1_DoubleClick(object sender, EventArgs e)
{
deviceInfo = devices.ElementAt(listBox1.SelectedIndex);
updateUI(deviceInfo.DeviceName + " was selected. Attempting to connect.");
if (pairDevice())
{
updateUI("Device paired.");
updateUI("Starting connect thread...");
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
}
else
{
updateUI("Pairing failed.");
}
}
private void ClientConnectThread()
{
updateUI("Attempting connect.");
client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(BluetoothClientConnectCallback), client);
}
我尝试重用之前用来扫描设备的线程,并将BeginConnect粘贴到那里,但这只会导致程序崩溃。我不确定它会显示什么错误,因为我是在我的PC上对它进行编程,但只能使用.exe文件在另一台笔记本电脑上测试该程序。
发布于 2018-06-01 05:23:30
您已经创建了一个线程,但您还没有要求它启动:
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
bluetoothClientThread.Start(); // <--- this starts the thread
当然,您还会遇到另一个问题,因为您调用BeginConnect (异步的),然后函数结束(线程也是如此)。
https://stackoverflow.com/questions/50632471
复制相似问题