I am programing in C++ (MVS 2005), using windows bluetooth stack.
I am working to a MFC application that use a bluetooth usb dongle attach to a PC, and I am trying to access a service on a remote device (mobile phone).
I already completed the device and service discovery phase. I've also successfully paired my PC with another phone.
Now I want to connect to one of the services that can see in my list (like OBEX File Transfer, or Headset Audio Gateway).
I've used Winsock for the discovery phase, and BluetoothAPIs for the pairing mechanism.
Now I want to use sockets to communicate with the service (OBEX File Transfer), but I don't know how I should send the file to the device. I read this article where it is said, that I should construct the hole packet sent (including the header), byte by byte.
Please trow some light on this, because I can't find any information on the internet.
I poste the code I wrote for this function:
// Sender_Bth.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// Link to ws2_32.lib
#include <winsock2.h>
#include <ws2bth.h>
#include <BluetoothAPIs.h>
#include <stdio.h>
#define DEFAULT_BUFLEN 512
typedef ULONGLONG bt_addr, *pbt_addr, BT_ADDR, *PBT_ADDR;
int main(int argc, char **argv)
{
WSADATA wsd;
SOCKET s;
SOCKADDR_BTH sab;
// Hard coded directly, got it from the receiver/server
BT_ADDR aSddr = 0X00249F68A192;//BalckBerry9000
int iResult;
// This should be const void * type for non-char data
char *sendbuf = "Test data from client...";
int recvbuflen = DEFAULT_BUFLEN;
// Change the type accordingly for non-char data
char recvbuf[DEFAULT_BUFLEN] = "";
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Unable to load Winsock! Error code is %d\n", WSAGetLastError());
return 1;
}
else
printf("WSAStartup() is OK, Winsock lib loaded!\n");
s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s == INVALID_SOCKET)
{
printf ("Socket creation failed, error %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
else
printf ("socket() looks fine!\n");
memset (&sab, 0, sizeof(sab));
sab.addressFamily = AF_BTH;
// Set the btAddr member to a BT_ADDR variable that
// contains the address of the target device. App
// can accept the device address as a string but must convert
// the address and store it in a variable of type BT_ADDR.
sab.btAddr = aSddr;
// If the service identifier is available, then set the
// serviceClassId member of SOCKADDR_BTH to the GUID of
// the RFCOMM-based service. In this case, the client
// performs an SDP query and then uses the resulting server channel.
// sab.serviceClassId = nguiD;
// Or If you want to use a hard-coded channel number, set the
// port member of SOCKADDR_BTH to the server channel number (1-31)
GUID serviceID;
//serviceID = OBEXFileTransferServiceClass_UUID;
//hardcoded because of an link error in the Winsock lib
serviceID.Data1 = 0x00001106;
serviceID.Data2 = 0x0000;
serviceID.Data3 = 0x1000;
serviceID.Data4[0] = 0x80;
serviceID.Data4[1] = 0x00;
serviceID.Data4[2] = 0x00;
serviceID.Data4[3] = 0x80;
serviceID.Data4[4] = 0x5F;
serviceID.Data4[5] = 0x9B;
serviceID.Data4[6] = 0x34;
serviceID.Data4[7] = 0xFB;
//sab.port = 1;
sab.serviceClassId = serviceID;
// Connect to the Bluetooth socket, created previously
if (connect (s, (SOCKADDR *)&sab, sizeof(sab)) == SOCKET_ERROR)
{
printf("connect() failed with error code %d\n", WSAGetLastError ());
closesocket(s);
WSACleanup();
return 1;
}
else
printf("connect() should be fine!\n");
//Copy from file
//Here I don't know how to send the file
char* buffer = new char[DEFAULT_BUFLEN];
int read_size = fread (buffer, sizeof(char), DEFAULT_BUFLEN, rFile);
// Send some data
iResult = send(s, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send() failed with error code %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return 1;
}
else
{
printf("send() is OK!\n");
printf("Bytes Sent: %d\n", iResult);
}
// shutdown the stream connection since no more data will be sent
iResult = shutdown(s, SD_SEND);
if (iResult == SOCKET_ERROR)
{
printf("shutdown() failed with error code %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return 1;
}
else
printf("shutdown() is working!\n");
// receive
// Receive until the peer closes the connection
do {
iResult = recv(s, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf(" %d Bytes received from sender\n", iResult);
else if (iResult == 0)
printf("Connection was closed by peer!\n");
else
printf("recv() failed with error code %d\n", WSAGetLastError());
} while(iResult > 0);
// Do all the cleanup
if(closesocket(s) == 0)
printf("closesocket() pretty fine!\n");
if(WSACleanup () == 0)
printf("WSACleanup() is OK!\n");
return 0;
}

New Topic/Question
Reply


MultiQuote

|