NO

Author Topic: Bluetooth problems  (Read 5370 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Bluetooth problems
« on: August 15, 2018, 07:36:28 PM »
How well the Bluetooth technology works in Windows?

EDIT:
list cache:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#pragma comment (lib, "Ws2_32.lib")

typedef ULONGLONG bt_addr, *pbt_addr, BT_ADDR, *PBT_ADDR;

int printf(const char * format, ...);

int FindingBtDevices(void)
{
WSAQUERYSET wsaq;
HANDLE hLookup;
LPWSAQUERYSET pwsaResults;
DWORD dwSize;
BT_ADDR btAddr;
int nDev;
char buf[1024];

nDev = 0;
pwsaResults = (LPWSAQUERYSET)buf;
dwSize = sizeof(buf);
ZeroMemory(&wsaq, sizeof(wsaq));
wsaq.dwSize = sizeof(wsaq);

// namespace MUST be NS_BTH for bluetooth queries
wsaq.dwNameSpace = NS_BTH;
wsaq.lpcsaBuffer = NULL;

// initialize searching procedure
if (WSALookupServiceBegin(&wsaq, LUP_CONTAINERS, &hLookup) == SOCKET_ERROR)
{
printf("WSALookupServiceBegin() failed %d\n", WSAGetLastError());
return 0;
}
ZeroMemory(pwsaResults, sizeof(WSAQUERYSET));
pwsaResults->dwSize = sizeof(WSAQUERYSET);
pwsaResults->dwNameSpace = NS_BTH;
pwsaResults->lpBlob = NULL;
while (WSALookupServiceNext(hLookup, LUP_RETURN_NAME | LUP_RETURN_ADDR | LUP_RETURN_TYPE, &dwSize, pwsaResults) == 0)
{
btAddr = *(BT_ADDR*)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr->sa_data;
//if (!*(int*)pwsaResults->lpServiceClassId) { // only misc
printf("%s\taddr: %llXh %llXh\n", pwsaResults->lpszServiceInstanceName, btAddr, *pwsaResults->lpServiceClassId);
//}
nDev++;
}
if (WSALookupServiceEnd(hLookup) != 0)
printf("WSALookupServiceEnd(hLookup) failed with error code %ld\n", WSAGetLastError());
return nDev;
}

int main(void)
{
WSADATA wsd;
int nDev;

if (WSAStartup(MAKEWORD(2, 2), &wsd) == 0) {
nDev = FindingBtDevices();
if (!nDev)
printf("\nNo bluetooth devices in range found\n");
else
printf("\nFound %d bluetooth devices\n", nDev);
}
WSACleanup();
return 0;

}
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
//#include <ws2bth.h>
//#include <BluetoothAPIs.h>
#include <stdio.h>
#include "ws2bth.h"
#pragma comment (lib, "Ws2_32.lib")

TCHAR* DisplayWinSockError(DWORD ErrCode);

//const GUID RFCOMM_PROTOCOL_UUID = {0x00000003, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
//const GUID SerialPortServiceClass_UUID = {0x00001101, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};

SOCKADDR_BTH sabth = {AF_BTH
,0x000D18000001 //}; // bluetooth address 00:0D:18:00:00:01
,{0x00000003, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}
,BT_PORT_ANY
};

int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET s;
//SOCKADDR_BTH sabth;
if (!WSAStartup(MAKEWORD(2, 2), &wsd)) {
s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s != INVALID_SOCKET) {
if (connect (s, (SOCKADDR *)&sabth, sizeof(sabth)) != SOCKET_ERROR)
            {
printf("connect\n");
            } else {
DWORD ec = WSAGetLastError();
//printf("connect error %d\n", ec);
printf("connect error %d %s\n", ec, DisplayWinSockError(ec));
}
closesocket(s);
}
WSACleanup();
}
return 0;
}

TCHAR *DisplayWinSockError(DWORD ErrCode)
{
LPSTR pszName = NULL; // http://www.sockets.com/err_lst1.htm#WSANO_DATA

switch (ErrCode) // http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx
{
case 10022: pszName = "WSAEINVAL Invalid argument";break;
/* case 10035: pszName = "WSAEWOULDBLOCK";break;
case 10036: pszName = "WSAEINPROGRESS";break;
case 10037: pszName = "WSAEALREADY ";break;
case 10038: pszName = "WSAENOTSOCK       ";break;
case 10039: pszName = "WSAEDESTADDRREQ   ";break;
case 10040: pszName = "WSAEMSGSIZE       ";break;
case 10041: pszName = "WSAEPROTOTYPE     ";break;
case 10042: pszName = "WSAENOPROTOOPT    ";break;
case 10043: pszName = "WSAEPROTONOSUPPORT";break;
case 10044: pszName = "WSAESOCKTNOSUPPORT";break;
case 10045: pszName = "WSAEOPNOTSUPP     ";break;
case 10046: pszName = "WSAEPFNOSUPPORT   ";break;
case 10047: pszName = "WSAEAFNOSUPPORT   ";break;
case 10048: pszName = "WSAEADDRINUSE     ";break;*/
case 10049: pszName = "WSAEADDRNOTAVAIL Can't assign requested address";break;
case 10050: pszName = "WSAENETDOWN Network is down";break;
case 10051: pszName = "WSAENETUNREACH Network is unreachable";break;
/* case 10052: pszName = "WSAENETRESET      ";break;
case 10053: pszName = "WSAECONNABORTED   ";break;
case 10054: pszName = "WSAECONNRESET     ";break;
case 10055: pszName = "WSAENOBUFS        ";break;
case 10056: pszName = "WSAEISCONN        ";break;
case 10057: pszName = "WSAENOTCONN       ";break;
case 10058: pszName = "WSAESHUTDOWN      ";break;
case 10059: pszName = "WSAETOOMANYREFS   ";break;*/
case 10060: pszName = "WSAETIMEDOUT Connection timed out";break;
/* case 10061: pszName = "WSAECONNREFUSED   ";break;
case 10062: pszName = "WSAELOOP          ";break;
case 10063: pszName = "WSAENAMETOOLONG   ";break;
case 10064: pszName = "WSAEHOSTDOWN      ";break;
case 10065: pszName = "WSAEHOSTUNREACH   ";break;
case 10066: pszName = "WSAENOTEMPTY      ";break;
case 10067: pszName = "WSAEPROCLIM       ";break;
case 10068: pszName = "WSAEUSERS         ";break;
case 10069: pszName = "WSAEDQUOT         ";break;
case 10070: pszName = "WSAESTALE         ";break;
case 10071: pszName = "WSAEREMOTE        ";break;
case 10091: pszName = "WSASYSNOTREADY    ";break;
case 10092: pszName = "WSAVERNOTSUPPORTED";break;
case 10093: pszName = "WSANOTINITIALISED ";break;
case 11001: pszName = "WSAHOST_NOT_FOUND ";break;
case 11002: pszName = "WSATRY_AGAIN      ";break;
case 11003: pszName = "WSANO_RECOVERY    ";break;
case 11004: pszName = "WSANO_DATA        ";break;*/
}
return pszName;
}
Code: [Select]
#ifndef _WS2BTH_H
#define _WS2BTH_H
#pragma once

#define BT_PORT_ANY        ((ULONG)-1)
#define AF_BTH 32
#define BTH_MAX_NAME_SIZE          (248)
#define BTHPROTO_RFCOMM  0x0003

typedef ULONGLONG BTH_ADDR, *PBTH_ADDR;

#include <pshpack1.h>
typedef struct _SOCKADDR_BTH {
  USHORT   addressFamily;
  BTH_ADDR btAddr;
  GUID     serviceClassId;
  ULONG    port;
} SOCKADDR_BTH, *PSOCKADDR_BTH;
#include <poppack.h>

#endif // _WS2BTH_H

EDIT:
Pairing seems to be a problem ?
COM-ports ?
With devmgmt.msc admin can remove nonworking COM ports.

EDIT: 2018-08-17
Uwe's tool for COM assignments.

Bluetooth in Windows 7

TIPS: If you have Windows 8, copy bthprops.cpl from a Windows 7 PC, as that one looks like a normal program.
« Last Edit: August 20, 2018, 09:39:46 AM by TimoVJL »
May the source be with you

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Bluetooth problems
« Reply #1 on: August 15, 2018, 10:01:54 PM »
Hello Timo,

I havn't worked with it in quite a while and only on Windows 7.  I used Java blue cove  and windows 32 feet .NET libraries for applications.  One thing I learned after the fact is that the application on a device should broadcast a service.  Other devices running the application look for the service and then they can connect and send messages.

32Feet.NET library wraps the Windows SDK.  You could use a reflection tool to examine the SDK calls and see how they handle / avoid the errors in the versions targeting the latest Windows OS and apply similar code patterns in your application.

That's the approach I usually take when I cannot find much in forums concerning something I want to do.

HTH

DMAC
No one cares how much you know,
until they know how much you care.