开发者

Bluetooth service problem

开发者 https://www.devze.com 2022-12-29 11:09 出处:网络
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

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 *)&开发者_如何学Go;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!


You have to create the SDP record and publish it with WSASetService before bind and wait for connections. Follow this tutorial, and you'll be able to receive connections.


"Discovery Failed" Means the phone sent out a broadcast message asking for a connection with the specified bluetooth MAC address, but nobody with that address responded.

Double check the Bluetooth MAC address of the remote device.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号