📄 bth_mobile_conn.cpp
字号:
#include "stdafx.h"
#include "BTH_Mobile_Conn.h"
#include <winsock2.h>
#include <ws2bth.h>
#include <initguid.h>
#include <stdio.h>
#include <BluetoothAPIs.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "irprops.lib")
DEFINE_GUID(g_guidServiceClass,0xa9705ad0,0x9edf,0x11db,0xb6,0x06,0x08,0x00,0x20,0x0c,0x9a,0x66);
//a9705ad0-9edf-11db-b606-0800200c9a66
BTH_Mobile_Conn::BTH_Mobile_Conn(){
intDeviceCount = 0;
sockBTH = INVALID_SOCKET;
BluetooothIsEnabled = false;
Disconnected = true;
//SockAddrBthLocal = {0};
//this->SetLocalSocketAddress();
}
const char* BTH_Mobile_Conn::toChar(const wchar_t* nString){
static char* string = 0;
static size_t stringLen = 0;
size_t i, len=wcslen(nString);
if(len>stringLen)
{
stringLen = len;
delete string;
string = new char[stringLen+1];
}
for(i=0; i<len; i++)
string[i] = (char)nString[i];
string[len] = 0;
return string;
}
wchar_t BTH_Mobile_Conn::ToWChar(const char* nInfo, ...){
char infoString[256];
va_list marker;
va_start(marker, nInfo);
// vsprintf(infoString,nInfo,marker);
int i,lenI=(int)strlen(infoString);
wchar_t wInfo[256];
for(i=0; i<lenI; i++)
wInfo[i] = infoString[i];
wInfo[lenI] = 0;
return *wInfo;
//::MessageBoxW(NULL, wInfo, wTitle, MB_OK|MB_SETFOREGROUND|MB_TOPMOST);
}
int BTH_Mobile_Conn::toNativeBTHAddr(const WCHAR **pp, BTH_ADDR *pba){
int i;
while (**pp == ' ')
++*pp;
for (i = 0 ; i < 4 ; ++i, ++*pp) {
if (! iswxdigit (**pp))
return FALSE;
int c = **pp;
if (c >= 'a')
c = c - 'a' + 0xa;
else if (c >= 'A')
c = c - 'A' + 0xa;
else c = c - '0';
if ((c < 0) || (c > 16))
return FALSE;
*pba = *pba * 16 + c;
}
for (i = 0 ; i < 8 ; ++i, ++*pp) {
if (! iswxdigit (**pp))
return FALSE;
int c = **pp;
if (c >= 'a')
c = c - 'a' + 0xa;
else if (c >= 'A')
c = c - 'A' + 0xa;
else c = c - '0';
if ((c < 0) || (c > 16))
return FALSE;
*pba = *pba * 16 + c;
}
if ((**pp != ' ') && (**pp != '\0'))
return FALSE;
return TRUE;
}
void BTH_Mobile_Conn::convertCharToTCHAR(char *c, TCHAR* t){
int i = 0;
while (c[i] != '\0'){
t[i] = (TCHAR) c[i];
i++;
}
t[i] = (TCHAR) c[i];
}
void BTH_Mobile_Conn::convertTCHARToChar(char *c, TCHAR* t){
int i = 0;
while (t[i] != '\0'){
c[i] = (char) t[i];
i++;
}
c[i] = (char) t[i];
}
int BTH_Mobile_Conn::InitialiseBluetooth(void){
if(!BluetoothIsDiscoverable(NULL)){
if(!::BluetoothEnableDiscovery(NULL,TRUE)){
return 0;
}
this->BluetooothIsEnabled = true;
}
return 1;
}
void BTH_Mobile_Conn::TerminateBluetooth(void){
if(this->BluetooothIsEnabled){
BluetoothEnableDiscovery(NULL,FALSE);
this->BluetooothIsEnabled = false;
}
}
int BTH_Mobile_Conn::InitialiseWinsock(void){
int intReturn;
WSADATA wsd;
if (WSAStartup (MAKEWORD(2,2), &wsd)){
intReturn=0;
}
else{
intReturn=1;
}
return intReturn;
}
void BTH_Mobile_Conn::TerminateWinsock(void){
::WSACleanup();
}
int BTH_Mobile_Conn::InitialiseSocket(void){
this->sockBTH = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if(this->sockBTH == INVALID_SOCKET) {
return 0;
}
SOCKADDR_BTH tempSockAddr_BTH={0};
this->SetLocalSocketAddress(tempSockAddr_BTH);
this->SockAddrBthLocal.addressFamily = AF_BTH;
this->SockAddrBthLocal.port = BT_PORT_ANY;
if((bind(this->sockBTH,(struct sockaddr *)&this->SockAddrBthLocal,sizeof(SOCKADDR_BTH))) == SOCKET_ERROR){
printf("%d",WSAGetLastError());
return 1;
}
return 2;
}
void BTH_Mobile_Conn::TerminateSocket(void){
if(this->sockBTH != INVALID_SOCKET){
closesocket(this->sockBTH);
this->sockBTH = INVALID_SOCKET;
}
}
int BTH_Mobile_Conn::RegisterBTHService(void){
LPCSADDR_INFO lpcsAddressInfo =NULL;
WSAQUERYSET wsaQuerySet = {0};
if((lpcsAddressInfo =(LPCSADDR_INFO)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CSADDR_INFO)))==NULL){
return 0;
}
lpcsAddressInfo[0].LocalAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
lpcsAddressInfo[0].LocalAddr.lpSockaddr = (LPSOCKADDR)&this->SockAddrBthLocal;
lpcsAddressInfo[0].RemoteAddr.iSockaddrLength = sizeof(SOCKADDR_BTH);
lpcsAddressInfo[0].RemoteAddr.lpSockaddr = (LPSOCKADDR)&this->SockAddrBthLocal;
lpcsAddressInfo[0].iSocketType = SOCK_STREAM;
lpcsAddressInfo[0].iProtocol = BTHPROTO_RFCOMM;
ZeroMemory(&wsaQuerySet,sizeof(WSAQUERYSET));
wsaQuerySet.dwSize = sizeof(WSAQUERYSET);
wsaQuerySet.lpServiceClassId = (LPGUID) &g_guidServiceClass;
wsaQuerySet.lpszServiceInstanceName = (LPWSTR)"BlueTooth Remote Service";
wsaQuerySet.lpszComment = (LPWSTR)"BlueTooth Remote Service";
wsaQuerySet.dwNameSpace = NS_BTH;
wsaQuerySet.dwNumberOfCsAddrs = 1;
wsaQuerySet.lpcsaBuffer = lpcsAddressInfo;
if(::WSASetService(&wsaQuerySet,RNRSERVICE_REGISTER, 0) == SOCKET_ERROR){
return 1;
}
return 2;
}
char* BTH_Mobile_Conn::MakeConnection(int intDeviceNum){
char* strReturnMsg="";
unsigned int intChannel = 0;
SOCKADDR_BTH tempSockAddr;
memset(&tempSockAddr,0,sizeof(tempSockAddr));
tempSockAddr.addressFamily = AF_BTH;
tempSockAddr.btAddr = this->deviceAddrList[intDeviceNum];
for(int count=0; count<=30; count++)
{
intChannel = count;
tempSockAddr.port = intChannel & 0xff;
//wprintf (L"Connecting to %04x%08x 0x%02x\n", GET_NAP(serverAddrBt), GET_SAP(serverAddrBt), channel & 0xff);
strReturnMsg="";
if (connect (this->sockBTH, (SOCKADDR *)&tempSockAddr, sizeof(tempSockAddr))) {
strReturnMsg="Connect failed";
}
else{
strReturnMsg="Connected on channel";
break;
}
}
if(intChannel>=30){
strReturnMsg="Could not connect(Channel>=30)";
return strReturnMsg;
}
return strReturnMsg;
}
int BTH_Mobile_Conn::ListenMsg(void){
if(SOCKET_ERROR == listen(this->sockBTH,SOMAXCONN)){
return 0;
}
return 1;
}
int BTH_Mobile_Conn::AcceptMsg(void){
SOCKET tempSock = INVALID_SOCKET;
tempSock = accept(this->sockBTH,NULL,NULL);
this->SetClientSocket(tempSock);
Disconnected = false;
return 1;
}
char* BTH_Mobile_Conn::RecieveMsg(void){
int intLen=0;
char* strReturnMsg="";
char* strRecvBuffer = new char[50];
if(INVALID_SOCKET == this->GetClientSocket()){
Disconnected = true;
return "ClientSocket error";
}
intLen = recv(this->GetClientSocket(),strRecvBuffer,50,0);
if(intLen == SOCKET_ERROR){
Disconnected = true;
return "ClientSocket error";
}
if(intLen>0){
strRecvBuffer[intLen]=0;
strReturnMsg=strRecvBuffer;
}
Disconnected = false;
return strReturnMsg;
}
int BTH_Mobile_Conn::SendMsg(char* strInput){
int intByteSend=0;
int intReturn = -1;
if(INVALID_SOCKET == this->GetClientSocket()){
Disconnected = true;
return 0;
}
intByteSend =send(this->GetClientSocket(),strInput,50,0);
if( SOCKET_ERROR == intByteSend)
{
Disconnected = true;
intReturn = 1;
return intReturn;
}else{
Disconnected = false;
intReturn = 2;
}
if(intByteSend !=strlen(strInput)){
intReturn = 3;
}else{
intReturn = 4;
}
return intReturn;
}
char* BTH_Mobile_Conn::CloseSocket(void){
char* strReturnMsg="";
closesocket(this->sockBTH);
return strReturnMsg;
}
char* BTH_Mobile_Conn::IsDeviceVaild(int intDeviceNum){
char* strReturnMsg;
const WCHAR* tempBTH_AddrStr=NULL;
const char* tempBTH_Name="UNKNOWN";
BTH_ADDR tempBTH_Addr;
if(intDeviceNum<-1){
strReturnMsg="Invalid Device Num";
}
tempBTH_Name=this->toChar((wchar_t*)this->deviceNameList[intDeviceNum]);
tempBTH_Addr=this->deviceAddrList[intDeviceNum];
if(!this->toNativeBTHAddr(&tempBTH_AddrStr,&tempBTH_Addr)){
strReturnMsg="Invalid server address";
}
return strReturnMsg;
}
int BTH_Mobile_Conn::GetDeviceCount(void){
return this->intDeviceCount;
}
SOCKADDR_BTH BTH_Mobile_Conn::GetLocalSocketAddress(void){
return this->SockAddrBthLocal;
}
void BTH_Mobile_Conn::SetLocalSocketAddress(SOCKADDR_BTH temp){
this->SockAddrBthLocal = temp;
}
char* BTH_Mobile_Conn::GetServerName(int intDeviceNum){
//char* strServerName = (char*)toChar((wchar_t*)this->deviceNameList[intDeviceNum]);
//return strServerName;
return this->devList[intDeviceNum];
}
SOCKET BTH_Mobile_Conn::GetClientSocket(void){
return this->ClientSocket;
}
void BTH_Mobile_Conn::SetClientSocket(SOCKET tempClientSock){
this->ClientSocket = tempClientSock;
}
bool BTH_Mobile_Conn::IsDisconnect(void){
return Disconnected;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -