📄 bluesoleilclient.cpp
字号:
else
{
dwResult = BT_StopSPPExService(g_sppexSvr[in].dwHanlde);
PrintError("BT_StopSPPExService",dwResult);
memset(&g_sppexSvr[in],0,sizeof(SPPEX_INFO));
}
}while(in != -1);
}
void SDK_BtSearchSPPExServices()
{
BLUETOOTH_DEVICE_INFO device;
device.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
memcpy(device.address, g_targetBdAddr, 6);
SPPEX_SERVICE_INFO sppex_svc_info[5];
memset(&sppex_svc_info, 0, 5*sizeof(SPPEX_SERVICE_INFO));
CHAR temp[256];
sppex_svc_info[0].dwSize = sizeof(SPPEX_SERVICE_INFO);
if(GetPrivateProfileString("SEARCH_SPP_EX_SERVICE", "GUID", NULL, temp, MAX_PATH, g_ini_path)==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[0].serviceClassUuid128.Data1));//0x00005002;
sscanf(temp+8,"%04x",&(sppex_svc_info[0].serviceClassUuid128.Data2));//0x0000;
sscanf(temp+8+4,"%04x",&(sppex_svc_info[0].serviceClassUuid128.Data3));//0x1000;
for(int i=0;i<8;i++)
sscanf(temp+8+4+4+i*2,"%02x",Data4+i);
memcpy(sppex_svc_info[0].serviceClassUuid128.Data4, Data4, 8);
DWORD dwResult;
DWORD dwLength = 5*sizeof(SPPEX_SERVICE_INFO);
dwResult = BT_SearchSPPExServices(&device,&dwLength,sppex_svc_info);
PrintError("BT_SearchSPPExServices",dwResult);
if(dwResult != BTSTATUS_SUCCESS)
{
return;
}
for(i=0;i<dwLength/sizeof(SPPEX_SERVICE_INFO);i++){
printf("SPPEx Service Info No.%d\n",i);
printf("SDAP Record Handle: %d\n", sppex_svc_info[i].dwSDAPRecordHanlde);
printf("Service Name: %s\n", sppex_svc_info[i].szServiceName);
printf("Service Channel: %02X\n", sppex_svc_info[i].ucServiceChannel);
printf("COM Port: %d\n", sppex_svc_info[i].ucComIndex);
}
}
void SDK_BtConnectSPPExService()
{
int nIndex=0;
while(nIndex<MAX_SPPEX && g_sppexClnt[nIndex].dwHanlde != NULL)
nIndex ++;
if(nIndex == MAX_SPPEX){
printf("No handle for SDK_BtConnectSPPExService\n");
return;
}
BLUETOOTH_DEVICE_INFO device;
device.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
memcpy(device.address, g_targetBdAddr, 6);
memset(&g_sppexClnt[nIndex], 0, sizeof(SPPEX_INFO));
CHAR temp[256];
g_sppexClnt[nIndex].svcInfo.dwSize = sizeof(SPPEX_SERVICE_INFO);
if(GetPrivateProfileString("CONNECT_SPP_EX_SERVICE", "GUID", NULL, temp, MAX_PATH, g_ini_path)==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",&(g_sppexClnt[nIndex].svcInfo.serviceClassUuid128.Data1));//0x00005002;
sscanf(temp+8,"%04x",&(g_sppexClnt[nIndex].svcInfo.serviceClassUuid128.Data2));//0x0000;
sscanf(temp+8+4,"%04x",&(g_sppexClnt[nIndex].svcInfo.serviceClassUuid128.Data3));//0x1000;
for(int i=0;i<8;i++)
sscanf(temp+8+4+4+i*2,"%02x",Data4+i);
memcpy(g_sppexClnt[nIndex].svcInfo.serviceClassUuid128.Data4, Data4, 8);
DWORD dwResult;
DWORD dwLength = sizeof(SPPEX_SERVICE_INFO);
dwResult = BT_ConnectSPPExService(&device,&g_sppexClnt[nIndex].svcInfo,&g_sppexClnt[nIndex].dwHanlde);
PrintError("BT_ConnectSPPExService",dwResult);
if(dwResult != BTSTATUS_SUCCESS){
memset(&g_sppexClnt[nIndex], 0, sizeof(SPPEX_INFO));
return;
}
printf("SPPEx Service Info:\n");
printf("SDAP Record Handle: %d\n", g_sppexClnt[nIndex].svcInfo.dwSDAPRecordHanlde);
printf("Service Name: %s\n", g_sppexClnt[nIndex].svcInfo.szServiceName);
printf("Service Channel: %02X\n", g_sppexClnt[nIndex].svcInfo.ucServiceChannel);
printf("COM Port: %d\n", g_sppexClnt[nIndex].svcInfo.ucComIndex);
g_sppexClnt[nIndex].bIsConnected = TRUE;
//Start I/O
int bDoClientIO = GetPrivateProfileInt("CONNECT_SPP_EX_SERVICE", "DO CLIENT IO", 1,g_ini_path);
if(bDoClientIO){
DWORD dwID;
CreateThread(NULL,0,SPPEXClientIOThread,(LPVOID)nIndex,0,&dwID);
}
}
void SDK_BtDisConnectSPPExService()
{
DWORD dwResult;
int in = -1;
printf("All the SPPEx connection on local device:\n");
for(int i = 0; i < MAX_SPPEX; i++)
{
if(g_sppexClnt[i].dwHanlde != 0)
{
printf("List NO: %d\n", i);
printf("SPPEx connection Handle:%d\n", g_sppexClnt[i].dwHanlde);
printf("***********************************************\n");
in = i;
}
}
if(in == -1)
{
printf("There is no connection on this local device!\n");
return;
}
do{
printf("Please Input the List NO that you want to disconnect:(-1 to Cancel)");
scanf("%d", &in);
if(in < 0 || in > 7)
continue;
else
{
dwResult = BT_DisconnectSPPExService(g_sppexClnt[in].dwHanlde);
PrintError("BT_DisconnectSPPExService",dwResult);
g_sppexClnt[i].bIsConnected = FALSE;
}
}while(in != -1);
}
typedef struct _DEV_LIST{
BLUETOOTH_DEVICE_INFO dev;
_DEV_LIST *pNext;
}DEV_LIST;
DEV_LIST *g_pDevList=NULL;
void SDK_InquiryDeviceInd(/*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_ini_path) == 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 SDK_GetDeviceClassDescription()
{
char temp[128];
//Get Device Class
if(GetPrivateProfileString("GET_DEVICE_CLASS_DESCRIPTION", "Device Class", NULL, temp, MAX_PATH, g_ini_path)==0){
printf("Get Device Class Failed!\n");
return;
}
BYTE classOfDevice[3];
if(Help_InputDeviceClass(temp, classOfDevice) == BTSTATUS_FAIL){
printf("Invalid Device Class!\n");
return;
}
DWORD dwResult;
DWORD dwLen =128;
dwResult = BT_GetDeviceClassDescription(DESC_DEVICE_TYPE,classOfDevice,&dwLen,(BYTE*)&temp);
PrintError("BT_GetDeviceClassDescription",dwResult);
if(dwResult == BTSTATUS_SUCCESS){
printf("Description:\n");
printf(temp);
printf("\n");
}
}
void SDK_BluetoothInd(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");
}
DWORD g_dwServerHandle;
void SDK_StartService()
{
GENERAL_SERVICE_INFO svc={0};
svc.dwSize = sizeof(GENERAL_SERVICE_INFO);
svc.wServiceClassUuid16 = CLS_SERIAL_PORT;
CHAR szTmp[64];
if(GetPrivateProfileString("START SERVICE", "Service Class", NULL, szTmp, 63, g_ini_path)==0){
printf("Get Service Class Failed!\n");
return;
}
if((svc.wServiceClassUuid16 = atoi(szTmp)) == 0){
printf("Invalid Service Class!\n");
return;
}
DWORD dwResult;
dwResult = BT_StartService(&svc,&g_dwServerHandle);
PrintError("BT_StartService",dwResult);
if(dwResult != BTSTATUS_SUCCESS)
g_dwServerHandle = NULL;
}
void SDK_StopService()
{
DWORD dwResult;
dwResult = BT_StopService(g_dwServerHandle);
PrintError("BT_StopService",dwResult);
g_dwServerHandle = NULL;
}
void SDK_GetSetConfiguration()
{
DWORD dwIndex = CONFIG_ASSIGN_COMPORT;
COM_PORT_ASSIGN_INFO comInfo={0};
comInfo.dwSize = sizeof(COM_PORT_ASSIGN_INFO);
comInfo.ucComPort = 4;
// //Get assignment info.
// DWORD dwResult = BT_GetConfiguration(dwIndex,(BYTE*)&comInfo);
// PrintError("BT_GetConfiguration",dwResult);
//
// //Set assignment
// memcpy(comInfo.devAddress, g_targetBdAddr,6);
// dwResult = BT_SetConfiguration(dwIndex,(BYTE*)&comInfo);
// PrintError("BT_SetConfiguration",dwResult);
//
// HANDLE hDun = OpenPort(4);
// if(hDun != INVALID_HANDLE_VALUE){
// printf("open port success");
// Sleep(5000);
// CloseHandle(hDun);
// }
//
// //Reset assignment
// memset(comInfo.devAddress,0,6);
// dwResult = BT_SetConfiguration(dwIndex,(BYTE*)&comInfo);
// PrintError("BT_SetConfiguration",dwResult);
dwIndex = CONFIG_LOCAL_ACCESSIBLITY;
LOCAL_ACCESSIBILITY acc;
DWORD dwResult = BT_GetConfiguration(dwIndex,(BYTE*)&acc);
PrintError("BT_GetConfiguration",dwResult);
printf("bConnectable=%d,wDiscoverableMode=%d,bPairable=%d\n",acc.bConnectable,acc.wDiscoverableMode,acc.bPairable);
acc.bConnectable = FALSE;
acc.wDiscoverableMode = LIMITED_DISCOVERABLE;
acc.bPairable = TRUE;
dwResult = BT_SetConfiguration(dwIndex,(BYTE*)&acc);
PrintError("BT_SetConfiguration",dwResult);
dwResult = BT_GetConfiguration(dwIndex,(BYTE*)&acc);
PrintError("BT_GetConfiguration",dwResult);
printf("bConnectable=%d,wDiscoverableMode=%d,bPairable=%d\n",acc.bConnectable,acc.wDiscoverableMode,acc.bPairable);
dwIndex = CONFIG_LOCAL_SECURITY;
LOCAL_SECURITY sec;
dwResult = BT_GetConfiguration(dwIndex,(BYTE*)&sec);
PrintError("BT_GetConfiguration",dwResult);
printf("ucSecurityMode=%d,bEncryptData=%d\n",sec.ucSecurityMode,sec.bEncryptData);
sec.ucSecurityMode = SECURITY_MODE_3;
sec.bEncryptData = FALSE;
dwResult = BT_SetConfiguration(dwIndex,(BYTE*)&sec);
PrintError("BT_SetConfiguration",dwResult);
dwResult = BT_GetConfiguration(dwIndex,(BYTE*)&sec);
PrintError("BT_GetConfiguration",dwResult);
printf("ucSecurityMode=%d,bEncryptData=%d\n",sec.ucSecurityMode,sec.bEncryptData);
}
void SDK_EnumConnections()
{
GENERAL_CONNECT_INFO connInfo[10]={0};
DWORD dwSize = 10*sizeof(GENERAL_CONNECT_INFO);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -