mydew Napisano Kwiecień 3, 2010 Zgłoś Share Napisano Kwiecień 3, 2010 Program znajduje urządzenia bluetooth (to działa). Chciałbym jeszcze zainicjować z takim urządzeniem połączenie i wysłać np. jakieś dane tekstowe. Ktoś wie, w jaki sposób to zrobić ? Funkcja connect zwraca mi 10049 (nieprawidłowy adres w zapytaniu). Tworzę w VS 2008 EE, Windows XP. // BT.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <winsock2.h> #include <ws2bth.h> #include <iostream> #pragma comment(lib, "ws2_32.lib") using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // setup windows sockets WORD wVersionRequested ; WSADATA wsaData; //structure contains information about the Windows Sockets implementation wVersionRequested = MAKEWORD(2,0); if(WSAStartup(wVersionRequested, &wsaData) != 0 ) //function initiates use of the Winsock DLL by a process { fprintf(stderr , "uh oh... windows sockets barfed\n"); printf("Starting error"); ExitProcess(2); } // prepare the inquiry data structure DWORD qs_len = sizeof(WSAQUERYSET); WSAQUERYSET *qs = (WSAQUERYSET*) malloc(qs_len); /*structure provides relevant information about a given service, including service class ID, service name, applicable namespace identifier and protocol information, as well as a set of transport addresses at which the service listens.*/ ZeroMemory(qs , qs_len); qs->dwSize = sizeof(WSAQUERYSET ) ; qs->dwNameSpace = NS_BTH ; DWORD flags = LUP_CONTAINERS; flags |= LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_ADDR; HANDLE h; // start the device inquiry if(SOCKET_ERROR == WSALookupServiceBegin(qs , flags, &h) ) /*function initiates a client query that is constrained by the information contained within a WSAQUERYSET structure*/ { printf("Any device found"); ExitProcess(2); } // iterate through the inquiry results bool done = false ; while(!done) { if(NO_ERROR == WSALookupServiceNext(h , flags, &qs_len , qs) ) //function is called after obtaining a handle from a previous call to WSALookupServiceBegin in order to retrieve the requested service information { LPWSTR *buf=(LPWSTR*) malloc(sizeof(LPWSTR)); SOCKADDR_BTH *sa = (SOCKADDR_BTH* )qs->lpcsaBuffer->RemoteAddr.lpSockaddr; //structure is used in conjunction with Bluetooth socket operations, defined by address family AF_BTH //sa->addressFamily = AF_BTH; //sa->serviceClassId = SerialPortServiceClass_UUID; //sa->btAddr = 0; //sa->port = BT_PORT_ANY; BTH_ADDR result = sa->btAddr; DWORD bufsize = sizeof(buf); WSAAddressToString(qs->lpcsaBuffer->RemoteAddr.lpSockaddr, sizeof(SOCKADDR_BTH), NULL, *buf, &bufsize);//function converts all components of a sockaddr structure into a human-readable string representation of the address //printf("found: %s - %s\n", buf, qs->lpszServiceInstanceName); cout << "Found: " << qs->lpszServiceInstanceName << " : " << sa->btAddr << "\n"; //Connect to device: SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); //if(connect(s, qs->lpcsaBuffer->RemoteAddr.lpSockaddr, qs->lpcsaBuffer->RemoteAddr.iSockaddrLength) == SOCKET_ERROR) if(connect(s, (LPSOCKADDR)&sa, sizeof(SOCKADDR_BTH)) == SOCKET_ERROR) { printf("Failed to connect, error: "); cout << WSAGetLastError() << "\n"; WSACleanup(); ExitProcess(2); } printf("Connected\n"); } else { int error = WSAGetLastError(); if(error == WSAEFAULT) { free(qs); qs = (WSAQUERYSET* ) malloc(qs_len); } else if(error == WSA_E_NO_MORE) { printf("inquiry complete\n"); done = true; } else { printf("uh oh. error code %d\n" , error); done = true; } } } WSALookupServiceEnd(h); //function is called to free the handle after previous calls to WSALookupServiceBegin and WSALookupServiceNext free(qs); WSACleanup(); return 0; } Link do komentarza Udostępnij na innych stronach More sharing options...
Polecane posty
Zarchiwizowany
Ten temat jest archiwizowany i nie można dodawać nowych odpowiedzi.