Document directory
Preface:
There are two methods to implement the Mobile Phone Positioning Application: 1. locate the base station, obtain the base station information through the mobile phone, and then obtain the longitude and latitude based on the information; 2. GPS positioning is used to directly obtain latitude and longitude information. Advantages and disadvantages: 1. the positioning accuracy of the base station is low, and the error range is estimated to be less than 500 meters (this case is still in a big city. If it is a small city, the estimation error is greater). However, the positioning of the base station can be implemented everywhere, that is, the information of the base station will not be obtained because of buildings or other reasons. 2. GPS positioning accuracy is high, and the error range is estimated to be less than 10 meters. However, in an empty location, if there are many buildings around or indoors, GPS information cannot be obtained. Usually both methods are used for positioning, and the third WiFi positioning or cell positioning will be added. There are two ways to obtain cellid and lac in Windows Mobile: sending AT commands via serial port or using RIL. RIL (radio interface layer) is a library developed by Microsoft. Its program has inherent characteristics. In obtaining cellid, it is actually an encapsulation of the first method, the two are essentially the same. Note that once the serial port is enabled, it is difficult to close it unless you restart the machine (which may involve underlying interruptions). In addition, not all devices can obtain the cellid. This article uses the comport to obtain the cellid, which is not supported by all devices. Test Platform: vs2005 + WM 6.0 development language: C ++ defines the base station information structure:
typedef struct { char CountryCode[12]; char AreaCode[4]; char NetworkCode[4]; char CellID[4]; } TCREG_DATA;
Obtain base station information:
void Get_Cellid(void) { char m_sTemp[12] = {0}; strcat(m_sTemp,"COM"); for(int i = 9; i > 0; -- i) { char ch1; _itoa(i,&ch1,10); strcat(m_sTemp,&ch1); strcat(m_sTemp,":"); TCREG_DATA* pData = (TCREG_DATA*)GetCREG(m_sTemp); if(!pData) continue; char szNum1[8] = {0}; char szNum2[8] = {0}; strcpy(szNum1,pData->AreaCode); strcpy(szNum2,pData->CellID); int iLac = (int)strtol(szNum1,NULL,16); int iId = (int)strtol(szNum2,NULL,16); if (iLac && iId) { sprintf(m_sCell.LAC,"%06d", iLac ); sprintf(m_sCell.ID,"%06d", iId ); break; } } }
Get Serial Port:
char* GetCREG( char * comPort ) { HANDLE hCom; int bufpos; DCB dcb; COMMTIMEOUTS to; DWORD nWritten; DWORD event; DWORD nRead; static char outbuf[20], buf[256]; BYTE comdevcmd[2]= {0x84, 0x00}; WCHAR m_sCom[12] = {0}; mbstowcs(m_sCom,comPort,strlen(comPort)); hCom= CreateFile( m_sCom ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0); if (hCom==NULL || hCom==INVALID_HANDLE_VALUE) { hCom= NULL; return NULL; } if (!GetCommState(hCom, &dcb)) { return "ERROR:GetCommState Failed"; } dcb.BaudRate= CBR_115200; dcb.ByteSize= 8; dcb.fParity= false; dcb.StopBits= ONESTOPBIT; if (!SetCommState(hCom, &dcb)) { return "ERROR:SetCommState Failed"; } EscapeCommFunction(hCom, SETDTR); EscapeCommFunction(hCom, SETRTS); GetCommTimeouts(hCom, &to); to.ReadIntervalTimeout= 0; to.ReadTotalTimeoutConstant= 200; to.ReadTotalTimeoutMultiplier= 0; to.WriteTotalTimeoutConstant= 20000; to.WriteTotalTimeoutMultiplier= 0; SetCommTimeouts(hCom, &to); if (!SetCommMask(hCom, EV_RXCHAR)) { return "-8"; } DWORD rildevresult=0,nReturned=0; if (!DeviceIoControl (hCom,0xAAAA5679L, comdevcmd, sizeof(comdevcmd),0,0,0,0)) { return "-9"; } bufpos = 0; strcpy(outbuf,"AT+creg=2\r"); if (!WriteFile(hCom, outbuf, 10, &nWritten, NULL)) { return "-10"; } if (nWritten != 10) { return "-11"; } if (!WaitCommEvent(hCom, &event, NULL)) { return "-12"; } while(1) { if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL)) { return "-13"; } if (nRead == 0) break; bufpos += nRead; if (bufpos >= 256) break; } strcpy(outbuf,"AT+creg?\r"); if (!WriteFile(hCom, outbuf, 9, &nWritten, NULL)) { return "-14"; } if (nWritten != 9) { return "-15"; } if (!WaitCommEvent(hCom, &event, NULL)) { return "-16"; } while(1) { if (!ReadFile(hCom, buf+bufpos, 256 - bufpos, &nRead, NULL)) { return "-17"; } if (nRead == 0) break; bufpos += nRead; if (bufpos >= 256) break; } puts(buf); rildevresult = 0; if (!EscapeCommFunction(hCom, CLRDTR)) { return "-4"; } if (hCom!=NULL) { CloseHandle(hCom); hCom= NULL; } char* cregResponse = strpbrk( buf, "CREG\0" ); return cregResponse; }
Conclusion: mobile phones can be positioned by getting information from base stations, which is a good idea for mobile phones without GPS devices.