Bluetooth service problem

Posted by hara on Stack Overflow See other posts from Stack Overflow or by hara
Published on 2010-05-08T20:33:11Z Indexed on 2010/05/08 20:38 UTC
Read the original article Hit count: 287

hi

I need to create a custom bluetooth service and I have to develop it using c++. I read a lot of examples but I didn't success in publishing a new service with a custom UUID. I need to specify a UUID in order to be able to connect to the service from an android app. This is what i wrote:

GUID service_UUID = { /* 00000003-0000-1000-8000-00805F9B34FB */
    0x00000003,
    0x0000,
    0x1000,
    {0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}
};
    SOCKET s, s2;
SOCKADDR_BTH sab
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
    return 1;
printf("installing a new service\n");

s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s == INVALID_SOCKET)
{
    printf ("Socket creation failed, error %d\n", WSAGetLastError());
    return 1;
}

memset (&sab, 0, sizeof(sab));
sab.addressFamily  = AF_BTH;
sab.port = BT_PORT_ANY;
sab.serviceClassId = service_UUID;


if (0 != bind(s, (SOCKADDR *) &sab, sizeof(sab)))
{
    printf ("bind() failed with error code %d\n", WSAGetLastError());
    closesocket (s);
    return 1;
}
int result=sizeof(sab);
getsockname(s,(SOCKADDR *) &sab, &result );
printSOCKADDR_BTH(sab);

if(listen (s, 5) == 0)
    printf("listen() is OK! Listening for connection... :)\n");
else
    printf("listen() failed with error code %d\n", WSAGetLastError());

printf("waiting connection");

for ( ; ; )
{
    int ilen = sizeof(sab2);
    s2 = accept (s, (SOCKADDR *)&sab2, &ilen);
    printf ("accepted");
}
if(closesocket(s) == 0)
    printf("closesocket() pretty fine!\n");
if(WSACleanup () == 0)
    printf("WSACleanup() is OK!\n");
return 0;

When i print the SOCKADDR_BTH structure retrieved with get getsockname i get an UUID that is not the mine. Furthermore if i use the UUID read from getsockname to connect the Android application the connection fails with this exception:

java.io.IOException: Service discovery failed

Could you help me??

Thanks!

© Stack Overflow or respective owner

Related posts about bluetooth

Related posts about winsock2