Monday, October 20, 2008

How to control bluetooth OBEX default service on/off?

To control the Infrared state (on/off) or bluetooth OBEX default service in a similar way as in the control pannel, and to get the current state of the infrared module use the code below:

Code:

//set irda service status
HRESULT OBEXIOCTL(DWORD dwIOCTL)
{
HANDLE hService;
BOOL fRet;
hService = CreateFile(TEXT("OBX0:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == hService)
{
CloseHandle(hService);
return FALSE;
}
fRet = DeviceIoControl(hService,dwIOCTL,0,0,0,0,NULL,0);
CloseHandle(hService);
return (0 == fRet)?E_FAIL:S_OK;
}

//query irda service status
DWORD OBEXQuery()
{
HANDLE hService;
BOOL fRet;
DWORD dwStatus;
hService = CreateFile(TEXT("OBX0:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == hService)
{
CloseHandle(hService);
return FALSE;
}
fRet = DeviceIoControl(hService,IOCTL_SERVICE_STATUS,0,0,&dwStatus,sizeof(DWORD),0, 0);
CloseHandle(hService);
if(0 == fRet)
return 0xFFFFFFFF;
else
return dwStatus;
}

bool GetIRDAState()
{
switch(OBEXQuery())
{
case SERVICE_STATE_SHUTTING_DOWN:
case SERVICE_STATE_UNLOADING:
case SERVICE_STATE_UNINITIALIZED:
case SERVICE_STATE_OFF:
return 0;
break;

case SERVICE_STATE_STARTING_UP:
case SERVICE_STATE_ON:
return 1;
break;
default:
break;
};
return 0;
}

No comments: