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

📄 btaudiogw.cpp

📁 The following file describes how to use the Bluetooth Headset Audio Gateway.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}

	EnterCriticalSection(&g_csData);

	if (g_dwAGState != AG_CONNECTED) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot send a headset command while not connected")));
		LeaveCriticalSection(&g_csData);
		dwRetVal = ERROR_NOT_CONNECTED;
		SetLastError(dwRetVal);
		goto exit;
	}

	dwRetVal = SendATCommand(pszCommand, cdwBuf);
	LeaveCriticalSection(&g_csData);

exit:
	return dwRetVal;
}



// Get the speaker volume.
extern "C" DWORD BthGetHeadsetSpkVolume(USHORT* pusVolume)
{
	DWORD dwRetVal = ERROR_SUCCESS;

	if (! pusVolume) {
		dwRetVal = ERROR_INVALID_PARAMETER;
		SetLastError(dwRetVal);
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Out parameter is NULL pointer in GetSpkVolume")));
		goto exit;
	}

	EnterCriticalSection(&g_csData);

	if (g_dwAGState != AG_CONNECTED) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot call GetSpkVolume before establishing an ACL connection")));
		LeaveCriticalSection(&g_csData);
		dwRetVal = ERROR_NOT_CONNECTED;
		SetLastError(dwRetVal);
		goto exit;
	}

	*pusVolume = g_usSpkVol;
	LeaveCriticalSection(&g_csData);

exit:
	return dwRetVal;
}



// Set the speaker volume.
extern "C" DWORD BthSetHeadsetSpkVolume(USHORT usVolume)
{
	DWORD 	dwRetVal = ERROR_SUCCESS;
	CHAR	szBuf[MAX_SEND_BUF];
	DWORD	cdwBuf;

	if (usVolume > MAX_VOL || usVolume < MIN_VOL) {
		dwRetVal = ERROR_INVALID_PARAMETER;
		SetLastError(dwRetVal);
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot set speaker volume to %d - out of range"), usVolume));
		goto exit;
	}

	EnterCriticalSection(&g_csData);

	if (g_dwAGState != AG_CONNECTED) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot call SetSpkVolume before establishing an ACL connection")));
		LeaveCriticalSection(&g_csData);
		dwRetVal = ERROR_NOT_CONNECTED;
		SetLastError(dwRetVal);
		goto exit;
	}
	
	g_usSpkVol = usVolume;
	
	sprintf(szBuf, AT_VGS, usVolume);
	cdwBuf = strlen(szBuf);
	
	dwRetVal = SendATCommand(szBuf, cdwBuf);
	if (dwRetVal != ERROR_SUCCESS) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error setting speaker volume %d"), GetLastError()));
	}

	LeaveCriticalSection(&g_csData);

exit:
	return dwRetVal;
}



// Get the microphone volume.
extern "C" DWORD BthGetHeadsetMicVolume(USHORT* pusVolume)
{
	DWORD dwRetVal = ERROR_SUCCESS;

	if (! pusVolume) {
		dwRetVal = ERROR_INVALID_PARAMETER;
		SetLastError(dwRetVal);
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Out parameter is NULL pointer in GetMicVolume")));
		goto exit;
	}

	EnterCriticalSection(&g_csData);

	if (g_dwAGState != AG_CONNECTED) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot call GetMicVolume before establishing an ACL connection")));
		LeaveCriticalSection(&g_csData);
		dwRetVal = ERROR_NOT_CONNECTED;
		SetLastError(dwRetVal);
		goto exit;
	}

	*pusVolume = g_usMicVol;
	LeaveCriticalSection(&g_csData);
	
exit:
	return dwRetVal;
}



// Set the microphone volume.
extern "C" DWORD BthSetHeadsetMicVolume(USHORT usVolume)
{
	DWORD 	dwRetVal = ERROR_SUCCESS;
	CHAR	szBuf[MAX_SEND_BUF];
	DWORD	cdwBuf;

	if (usVolume > MAX_VOL || usVolume < MIN_VOL) {
		dwRetVal = ERROR_INVALID_PARAMETER;
		SetLastError(dwRetVal);
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot set microphone volume to %d - out of range"), usVolume));
		goto exit;
	}
	
	EnterCriticalSection(&g_csData);

	if (g_dwAGState != AG_CONNECTED) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Cannot call SetMicVolume before establishing an ACL connection")));
		LeaveCriticalSection(&g_csData);
		dwRetVal = ERROR_NOT_CONNECTED;
		SetLastError(dwRetVal);
		goto exit;
	}
	
	g_usMicVol = usVolume;
	
	sprintf(szBuf, AT_VGM, usVolume);
	cdwBuf = strlen(szBuf);
		
	dwRetVal = SendATCommand(szBuf, cdwBuf);
	if (dwRetVal != ERROR_SUCCESS) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error setting microphone volume %d"), GetLastError()));
	}

	LeaveCriticalSection(&g_csData);

exit:
	return dwRetVal;
}



// Register Headset events.
extern "C" DWORD BthSetHeadsetEvents(HANDLE hButton, HANDLE hSpkVol, HANDLE hMicVol)
{
	DWORD dwRetVal = ERROR_SUCCESS;

	if ((hButton && (! ResetEvent(hButton))) || (hSpkVol && (! ResetEvent(hSpkVol))) || (hMicVol && (! ResetEvent(hMicVol)))) {
		dwRetVal = FALSE;
		goto exit;
	}

	EnterCriticalSection(&g_csData);

	g_hCloseEvent 	= hButton;
	g_hSpkVolEvent 	= hSpkVol;
	g_hMicVolEvent 	= hMicVol;

	LeaveCriticalSection(&g_csData);
	
exit:
	return dwRetVal;
}



// Save the state of audio gateway
DWORD SaveAGState(void)
{
	DWORD 	dwRetVal;
	DWORD 	dwDisp;
	DWORD 	dwData;
	HKEY	hKey;
	LPCWSTR	lpSubKey = _T("SOFTWARE\\Microsoft\\Bluetooth\\AudioGateway");

	EnterCriticalSection(&g_csData);

	dwRetVal = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, NULL, 0, NULL, NULL, &hKey, &dwDisp);
	if (dwRetVal != ERROR_SUCCESS) {
		goto exit;
	}
	
	dwData = g_usMicVol;
	dwRetVal = RegSetValueEx(hKey, _T("MicVolume"), 0, REG_DWORD, (PBYTE)&dwData, sizeof(dwData));
	if (dwRetVal != ERROR_SUCCESS) {
		RegCloseKey(hKey);
		goto exit;
	}

	dwData = g_usSpkVol;
	dwRetVal = RegSetValueEx(hKey, _T("SpkVolume"), 0, REG_DWORD, (PBYTE)&dwData, sizeof(dwData));
	if (dwRetVal != ERROR_SUCCESS) {
		RegCloseKey(hKey);
		goto exit;
	}

	RegCloseKey(hKey);

exit:
	LeaveCriticalSection(&g_csData);
	return dwRetVal;
}



// Load the previous state of the audio gateway
DWORD LoadAGState(void)
{
	DWORD 	dwRetVal;
	DWORD 	cdwBytes;
	DWORD 	dwData;
	HKEY	hKey;
	LPCWSTR	lpSubKey = _T("SOFTWARE\\Microsoft\\Bluetooth\\AudioGateway");

	EnterCriticalSection(&g_csData);

	// Initialize data in case it has
	// never been previously stored
	g_usSpkVol 		= 8;
	g_usMicVol 		= 8;
	g_fPcmMode		= 1;

	dwRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, 0, &hKey);
	if (dwRetVal != ERROR_SUCCESS) {
		goto exit;
	}

	cdwBytes = sizeof(DWORD);
	dwRetVal = RegQueryValueEx(hKey, _T("MapAudioToPcmMode"), 0, NULL, (PBYTE)&dwData, &cdwBytes);
	if (dwRetVal == ERROR_SUCCESS) {
		g_fPcmMode = (USHORT) dwData;
	}

	cdwBytes = sizeof(DWORD);
	dwRetVal = RegQueryValueEx(hKey, _T("MicVolume"), 0, NULL, (PBYTE)&dwData, &cdwBytes);
	if (dwRetVal != ERROR_SUCCESS) {
		RegCloseKey(hKey);
		goto exit;
	}
	g_usMicVol = (USHORT) dwData;

	cdwBytes = sizeof(DWORD);
	dwRetVal = RegQueryValueEx(hKey, _T("SpkVolume"), 0, NULL, (PBYTE)&dwData, &cdwBytes);
	if (dwRetVal != ERROR_SUCCESS) {
		RegCloseKey(hKey);
		goto exit;
	} 
	g_usSpkVol = (USHORT) dwData;
	
	RegCloseKey(hKey);

exit:
	LeaveCriticalSection(&g_csData);
	return dwRetVal;
}



// Initialize SCO connection
DWORD InitSco(void)
{
	DWORD dwRetVal = ERROR_SUCCESS;

	g_hSco = 0;
	
	dwRetVal = BthCreateSCOConnection (&g_btaddr, &g_hSco);
	if (dwRetVal != ERROR_SUCCESS) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error creating SCO connection: %d"), dwRetVal));
		goto exit;
	}

	DEBUGMSG(ZONE_OUTPUT, (_T("BTAUDIOGW: Created a SCO connection to the device")));
	
exit:
	return dwRetVal;
}



// Deinitialize SCO connection
DWORD DeinitSco(void)
{
	DWORD dwRetVal = ERROR_SUCCESS;

	if (g_hSco == 0) {
		dwRetVal = ERROR_NOT_CONNECTED;
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error deinitializing SCO: %d"), dwRetVal));
		goto exit;
	}

	dwRetVal = BthCloseConnection (g_hSco);
	if (dwRetVal != ERROR_SUCCESS) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error closing SCO connection: %d"), dwRetVal));
		goto exit;
	}

	DEBUGMSG(ZONE_OUTPUT, (_T("BTAUDIOGW: Disconnected SCO connection to the device")));
	
exit:
	g_hSco = 0;
	return dwRetVal;	
}



// Get the BD_ADDR of the headset
DWORD GetBDAddr(void)
{
	DWORD 	dwRetVal = ERROR_SUCCESS;
	DWORD	cdwBtAddr;
	HKEY	hk = NULL;

	dwRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Bluetooth\\ScoWav", 0, 0, &hk);
	if (dwRetVal != ERROR_SUCCESS) {
		goto exit;
    }

	cdwBtAddr = sizeof(BT_ADDR);
	dwRetVal = RegQueryValueEx(hk, L"AddressOut", NULL, NULL, (PBYTE)&g_btaddr, &cdwBtAddr);
	if (dwRetVal != ERROR_SUCCESS) {
		DEBUGMSG(ZONE_ERROR, (_T("BTAUDIOGW: Error getting BT_ADDR of the headset")));
		goto exit;
	}

exit:
	if (hk) {
		RegCloseKey(hk);
	}
	
	return dwRetVal;
}

⌨️ 快捷键说明

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