⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdkfunc.cpp

📁 蓝牙驱动代码,实现了蓝牙查找,蓝牙传输的一个程序段,用VC++打开可运行
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	device.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
	memcpy(device.address, g_targetBdAddr, 6);
	
	SPPEX_SERVICE_INFO	sppex_svc_info;
	memset(&sppex_svc_info, 0, sizeof(SPPEX_SERVICE_INFO));
	
	CHAR temp[256];
	sppex_svc_info.dwSize = sizeof(SPPEX_SERVICE_INFO);
	if(GetPrivateProfileString("CONNECT_SPP_EX_SERVICE", "GUID", NULL, temp, MAX_PATH, g_szIniPath)==0)
	{
		printf("Get GUID Failed!\n");
		return;
	}
	if(strlen(temp)!=strlen("000050020000100080000002ee000001")){
		printf("Invalid GUID");
		return;
	}
	
    //{00005002-0000-1000-8000-0002ee000001}
	BYTE Data4[8] = {0};
	
	sscanf(temp,"%08x",&(sppex_svc_info.serviceClassUuid128.Data1));//0x00005002;
	sscanf(temp+8,"%04x",&(sppex_svc_info.serviceClassUuid128.Data2));//0x0000;
	sscanf(temp+8+4,"%04x",&(sppex_svc_info.serviceClassUuid128.Data3));//0x1000;
	for(int i=0;i<8;i++)
		sscanf(temp+8+4+4+i*2,"%02x",Data4+i);
	memcpy(sppex_svc_info.serviceClassUuid128.Data4, Data4, 8);
	
	DWORD dwResult, dwHandle;
	dwResult = BT_ConnectSPPExService(&device,&sppex_svc_info,&dwHandle);
	PrintError("BT_SearchSPPExServices",dwResult);
	
	if(dwResult == BTSTATUS_SUCCESS)
	{
		printf("SPPEx Service Info No.%d\n",i);
		printf("SDAP Record Handle:	%d\n", sppex_svc_info.dwSDAPRecordHanlde);
		printf("Service Name:	%s\n", sppex_svc_info.szServiceName);
		printf("Service Channel:	%02X\n", sppex_svc_info.ucServiceChannel);
		printf("COM Port:	%d\n", sppex_svc_info.ucComIndex);
		
		//Start IO on the COM port
		int bDClientIO = GetPrivateProfileInt("SPP_COM_PORT", "DO Client IO", 0,g_szIniPath);
		if(bDClientIO)
			StartIoOnComport(dwHandle,sppex_svc_info.ucComIndex, TRUE);
		
	}
	
}

void SDK_BtDisConnectSPPExService()
{
	SDK_EnumConnections();
	
	printf("Please input the number of the connection. (-1 to return)\n");
	
	int nInput;
	scanf("%d",&nInput);
	if(nInput == -1 )
		return;
	
	if(nInput > MAX_CONNECT_COUNT || nInput<0 || g_connList[nInput].dwHandle ==0){
		printf("Invalid input.\n");
		return;
	}
	
	DWORD dwResult = BT_DisconnectSPPExService(g_connList[nInput].dwHandle);
	PrintError("BT_DisconnectService",dwResult);
	
	if(dwResult == BTSTATUS_SUCCESS){
		memset(&g_connList[nInput], 0 , sizeof(GENERAL_CONNECT_INFO));
	}
}

typedef struct _DEV_LIST{
	BLUETOOTH_DEVICE_INFO dev;
	_DEV_LIST *pNext;
}DEV_LIST;
DEV_LIST *g_pDevList=NULL;
void CBK_InquiryDeviceReport(/*in*/PBLUETOOTH_DEVICE_INFO pDevInfo)
{
	BYTE zeroBuff[32]={0};
	
	DEV_LIST* pNode;
	
	printf("Inquiry Ind:\n");
	
    if( memcmp(pDevInfo->address,zeroBuff,DEVICE_ADDRESS_LENGTH) == 0 &&
		memcmp(pDevInfo->classOfDevice,zeroBuff,DEVICE_CLASS_LENGTH) == 0)
	{
        //This is the end of the inquiry
		printf("Inquiry completes. BlueSoleil might now continue to query device names\n\n");
		while(g_pDevList){
			pNode = g_pDevList;
			g_pDevList = g_pDevList->pNext;
			free(pNode);
		}
		g_pDevList = NULL;
		return;
	}
	pNode = g_pDevList;
	while(pNode){
		if(memcmp(pNode->dev.address,pDevInfo->address,DEVICE_ADDRESS_LENGTH)==0){
			printf("duplicated dev report.\n");
			return;
		}
		pNode = pNode->pNext;
	}
	pNode = (DEV_LIST*)malloc(sizeof(DEV_LIST));
	memcpy(&(pNode->dev),pDevInfo,sizeof(BLUETOOTH_DEVICE_INFO));
	pNode->pNext = g_pDevList;
	g_pDevList = pNode;
	
	
	printf("Address:%02X:%02X:%02X:%02X:%02X:%02X\n",
		pDevInfo->address[5],
		pDevInfo->address[4],
		pDevInfo->address[3],
		pDevInfo->address[2],
		pDevInfo->address[1],
		pDevInfo->address[0]);
	
	//If get device name afresh
	if(GetPrivateProfileInt("BT_INQUIRY_DEVICE", "Get Fresh Name On Inquiry", 1, g_szIniPath) == 1){
		DWORD dwMask = 0;
		BLUETOOTH_DEVICE_INFO_EX devInfo = {0};
		memcpy(&devInfo.address, pDevInfo->address, DEVICE_ADDRESS_LENGTH);
		devInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO_EX);
		devInfo.szName[0]='\0';
		dwMask = MASK_DEVICE_NAME;
		
		DWORD dwResult;
		dwResult = BT_SetRemoteDeviceInfo(dwMask, &devInfo);
		PrintError("BT_SetRemoteDeviceInfo",dwResult);
		dwResult = BT_GetRemoteDeviceInfo(dwMask, &devInfo);
		PrintError("BT_GetRemoteDeviceInfo",dwResult);
		
		printf("Name:%s \n", devInfo.szName);	
	}
	else
		printf("Name:%s \n", pDevInfo->szName);	
	
	
    printf("Class:%02x:%02x:%02x ---- \n",
		pDevInfo->classOfDevice[2],
		pDevInfo->classOfDevice[1],
		pDevInfo->classOfDevice[0]);
	
	PrintDeviceClassInfo(pDevInfo->classOfDevice);
	
	printf("Pair Status: %d \n\n",pDevInfo->bPaired);
	
}

void SDK_StartBluetooth()
{
	DWORD dwResult;
	dwResult = BT_StartBluetooth();
	PrintError("SDK_StartBluetooth",dwResult);
}
void SDK_StopBluetooth()
{
	DWORD dwResult;
	dwResult = BT_StopBluetooth(TRUE);
	PrintError("BT_StopBluetooth",dwResult);
}
void SDK_CancelInquiry()
{
	DWORD dwResult;
	dwResult = BT_CancelInquiry();
	PrintError("BT_CancelInquiry",dwResult);
}

void CBK_BluetoothStatus(UCHAR ucStatus)
{
	if(ucStatus == STATUS_BLUETOOTH_STARTED)
		printf("Bluetooth is started\n");
	else if(ucStatus == STATUS_BLUETOOTH_STOPED){
		printf("Bluetooth is stoped\n");
	}
	else
		printf("Invalid parameter in Bluetooth ind\n");
	
}

void SDK_EnumConnections()
{
	
	DWORD dwSize = MAX_CONNECT_COUNT*sizeof(GENERAL_CONNECT_INFO);
	memset(g_connList, 0, dwSize);
	
	DWORD dwResult = BT_EnumConnections(&dwSize,g_connList);
	PrintError("BT_EnumConnections",dwResult);
	
	for(DWORD i = 0; i < (dwSize/sizeof(GENERAL_CONNECT_INFO)); i++){
		printf("#%d handle=%d bIsOut=%d Service ID=0x%x bd=%02X:%02X:%02X:%02X:%02X:%02X\n",
			i,g_connList[i].dwHandle,g_connList[i].bIsOutgoing,g_connList[i].wServiceClass,
			g_connList[i].remoteBdAddr[5],
			g_connList[i].remoteBdAddr[4],
			g_connList[i].remoteBdAddr[3],
			g_connList[i].remoteBdAddr[2],
			g_connList[i].remoteBdAddr[1],
			g_connList[i].remoteBdAddr[0]);
	}
}


void PostRobustConnect(DWORD dwHanlde, WORD wSvcId, BYTE* pParam)
{
	switch(wSvcId)
	{
	case CLS_SERIAL_PORT:
	case CLS_DIALUP_NET:
		{
			if(pParam){
				BOOL bIsOutGoing;
				SPP_CONNECT_INFO sppConnInfo ={0};
				DWORD dwLen = sizeof(SPP_CONNECT_INFO);
				BYTE bdAddr[6]={0};
				WORD wClass;
				
				if(BT_GetConnectInfo(dwHanlde,&bIsOutGoing,&wClass,bdAddr,&dwLen,(BYTE*)&sppConnInfo) == BTSTATUS_SUCCESS){
					printf("Com Port = %d\n",sppConnInfo.ucComPort);
					printf("Remote Address:	%02X:%02X:%02X:%02X:%02X:%02X\n",
						bdAddr[5],
						bdAddr[4],
						bdAddr[3],
						bdAddr[2], 
						bdAddr[1], 
						bdAddr[0]);
				}
			}
		}
		break;
	default:;
	}
}
void TS_ConnectRobustness()
{
	int i;
	WORD wSvcCls = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST","Service ID", 0, g_szIniPath);
	int nGapConn2Disc = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Gap between Connect and Disconnect", 5000, g_szIniPath);
	int nGapDisc2Conn = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Gap between Disconnect and Connect", 4000, g_szIniPath);
	int bStopFlag;
	int bLog = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Log Enabled", 0, g_szIniPath);
	int bDoPostConnect = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Do Post Connect", 0, g_szIniPath);
	
	
	BLUETOOTH_DEVICE_INFO devInfo={0};
	devInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
	memcpy(devInfo.address, g_targetBdAddr, 6);
	
	GENERAL_SERVICE_INFO svcInfo = {0};
	svcInfo.dwSize = sizeof(GENERAL_SERVICE_INFO);
	svcInfo.wServiceClassUuid16 = wSvcCls;
	
	BYTE abyConnParam[512];
	DWORD dwParamLen = 512;
	
	if(!ConfigConnectParams(wSvcCls,abyConnParam,&dwParamLen)){
		printf("Failed to config parameters.\n");
		return;
	}
	
	DWORD dwResult;
	DWORD dwHandle;
	for(i=0;;i++){
		
		g_dwLastError = 0;
		printf("#%d Connect attempt started.\n", i);
		if(bLog)
			WriteLog("#%d Connect attempt started.", i);
		dwResult = BT_ConnectService(&devInfo, &svcInfo,dwParamLen==0 ? NULL : abyConnParam, &dwHandle);
		PrintError("BT_ConnectService",dwResult);
		
		if(dwResult == BTSTATUS_SUCCESS){
			if(bLog)
				WriteLog("#%d Connect succeeded.",i);
			
			PostRobustConnect(dwHandle, wSvcCls, dwParamLen==0 ? NULL : abyConnParam);
			
			if(nGapConn2Disc>0)
				Sleep(nGapConn2Disc);
			
			
			g_dwLastError = 0;
			printf("#%d Disconnect attempt started\n",i);
			if(bLog)
				WriteLog("#%d Disconnect attempt started.",i); 
			dwResult = BT_DisconnectService(dwHandle);
			PrintError("DisconnectService",dwResult);
			if(dwResult == BTSTATUS_SUCCESS){
				if(bLog)
					WriteLog("#%d Disconnect succeeded.",i);
			}
			else{
				if(bLog)
					WriteLog("Disconnect failed! return:%d, last error:0x%x\n",dwResult,g_dwLastError); 
			}
			
			if(nGapDisc2Conn>0)
				Sleep(nGapDisc2Conn);
		}
		else{
			if(bLog)
				WriteLog("%d Connect failed. Return:%d, Last Error:0x%x.\n",dwResult,g_dwLastError); 
		}
		
		bStopFlag = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Stop Flag", 0, g_szIniPath);
		if(bStopFlag)
			break;
		
	}
	
}


void TS_PanIpAllocation()
{
	
	CConnectCheck pingChecker;
	
	int bStopFlag;
	
	DWORD dwConnHandle;
	
	BLUETOOTH_DEVICE_INFO devInfo={0};
	devInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
	memcpy(devInfo.address, g_targetBdAddr, 6);
	GENERAL_SERVICE_INFO svcInfo = {0};
	svcInfo.dwSize = sizeof(GENERAL_SERVICE_INFO);
	svcInfo.wServiceClassUuid16 = CLS_PAN_GN;
		
	DWORD dwTotalTime = 0, dwErr;
	int nTotal = 0;
	DWORD dwLastTick;
	
	for(int i = 0;; i++)
	{
		WriteLog("------------------------------------",i);
		WriteLog("#%d BT_ConnectService attempt started...",i);
		dwLastTick = GetTickCount();
		dwErr = BT_ConnectService(
			&devInfo,
			&svcInfo,
			NULL,
			&dwConnHandle);		
		if(dwErr != BTSTATUS_SUCCESS){
			WriteLog("#%d BT_ConnectService attempt failed.",i);
		}
		else
		{
			WriteLog("#%d BT_ConnectService attempt succeeded.",i);
			Sleep(5000);
			if(0)/*Fail to get IP*/
			{
				WriteLog("Failed to recv a valid ip address or did not recv a change in IP address notification.");
			}
			else
			{
				// Recvd IP address increase the total time and disconnect PAN connection
				DWORD dwTimeTaken = GetTickCount() - dwLastTick;
				dwTotalTime += dwTimeTaken;
				nTotal ++;
				
				WriteLog("PAN IP received. time taken=%d milliseconds",dwTimeTaken);
				
				//Ping
				int nDoPing;
				char szTargetIP[64];
				nDoPing = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Do Ping", 1, g_szIniPath);
				if(nDoPing == 1){
					if(GetPrivateProfileString("CONNECT_ROBUSTNESS_TEST","Do Ping Target","",szTargetIP,63,g_szIniPath)>0){
						
						if(pingChecker.Ping(NULL,szTargetIP,0))
							WriteLog("Ping %s succeeded.",szTargetIP);
						else
							WriteLog("Ping %s failed.",szTargetIP);
					}
				}
				
				// disconnect PAN
				WriteLog("#%d BT_DisconnectService attempt started.",i);
				DWORD dwErr = BT_DisconnectService(dwConnHandle);
				if(dwErr != BTSTATUS_SUCCESS)
				{
					WriteLog("#%d BT_DisconnectService attempt failed.",i);
					WriteLog(" Failed to disconnect from PAN Service...step through put a break point here");
				}
				else
				{
					WriteLog("#%d BT_DisconnectService attempt succeeded.",i);
					dwConnHandle = 0;
				}
			}
		}
		
		bStopFlag = GetPrivateProfileInt("CONNECT_ROBUSTNESS_TEST", "Stop Flag", 0, g_szIniPath);
		if(bStopFlag)
			break;
		
	}
	
	
	WriteLog("Total %d attempts, %d succeed. ", i+1, nTotal);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -