简体   繁体   中英

How to force a Windows Bluetooth Connect() call to timeout

Is there any way - Any way at all - to force the

BluetoothAuthenticateDevice( HWND hwndParent, HANDLE hRadio, BLUETOOTH_DEVICE_INFO * pbtbi, PWSTR pszPasskey, ULONG ulPasskeyLength);

and the

connect(SOCKET s, const struct sockaddr FAR * name, int namelen);

function calls to timeout?

I have tried setting SO_RCVTIMEO and SO_SNDTIMEO to different values on the connect() socket to see if they affect the length of the function call, to no avail - and there is some discussion about whether they affect connect() anyway in this question .

It appears the default timeout is about 5 seconds on connect() and can apparently be as slow as 30-45 seconds on AuthenticateDevice - i have seen the auth/connect fail process take 80 seconds. However, if it hasn't connected in the first 1-2 seconds, it isn't going to - and until this gives up, our GUI can't display anything meaningful to the end user either way. The extra few seconds do matter.

Is it possible to actually enforce a timeout or is the only way to start it in a thread and kill the thread if it takes too long?

Have you tried setting the socket as non-blocking before making the connect call?

long lFlag;
SOCKET btSocket;

   btSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
   lFlag = 1;
   ioctlsocket(btSocket, FIONBIO, &lFlag);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM