我需要创建一个自定义蓝牙服务,我必须使用 C++ 开发它。我阅读了很多示例,但没有成功发布具有自定义 UUID 的新服务。我需要指定一个 UUID 才能从 android 应用程序连接到服务。这就是我写的:
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;
当我打印使用 get getsockname 检索到的 SOCKADDR_BTH 结构时,我得到一个不是我的 UUID。此外,如果我使用从 getsockname 读取的 UUID 连接 Android 应用程序,则连接失败并出现以下异常:
java.io.IOException: Service discovery failed
你可以帮帮我吗??
谢谢!