1. 程式人生 > >Android基站定位——通過手機訊號獲取基站資訊

Android基站定位——通過手機訊號獲取基站資訊

基站定位原理:通過手機訊號獲取基站資訊,然後呼叫第三方公開的根據基站資訊查詢基站的經緯度值,想要具體地址資訊的再根據經緯度值獲取具體的地址資訊。

一、通過手機訊號獲取基站資訊

 通過TelephonyManager 獲取lac:mcc:mnc:cell-id(基站資訊)的解釋:

 MCC,Mobile Country Code,移動國家程式碼(中國的為460);

 MNC,Mobile Network Code,行動網路號碼(中國移動為0,中國聯通為1,中國電信為2); 

 LAC,Location Area Code,位置區域碼;

 CID,Cell Identity,基站編號;

 BSSS,Base station signal strength,基站訊號強度。

具體實現程式碼如下:

[java]  

package com.easipass.test;  

import java.util.List;  

import android.app.Activity;  

import android.content.Context;  

import android.os.Bundle;  

import android.telephony.NeighboringCellInfo;  

import android.telephony.TelephonyManager;  

import android.telephony.cdma.CdmaCellLocation;  

import android.telephony.gsm.GsmCellLocation;  

import android.util.Log;  

import android.view.View;  

/** 

 * 功能描述:通過手機訊號獲取基站資訊 

 * # 通過TelephonyManager 獲取lac:mcc:mnc:cell-id 

 * # MCC,Mobile Country Code,移動國家程式碼(中國的為460); 

 * # MNC,Mobile Network Code,行動網路號碼(中國移動為0,中國聯通為1,中國電信為2);  

 * # LAC,Location Area Code,位置區域碼; 

 * # CID,Cell Identity,基站編號; 

 * # BSSS,Base station signal strength,基站訊號強度。 

 * @author android_ls 

 */  

public class GSMCellLocationActivity extends Activity {  

    private static final String TAG = "GSMCellLocationActivity";  

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

        // 獲取基站資訊  

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {  

            @Override  

            public void onClick(View v) {  

                TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  

                // 返回值MCC + MNC  

                String operator = mTelephonyManager.getNetworkOperator();  

                int mcc = Integer.parseInt(operator.substring(0, 3));  

                int mnc = Integer.parseInt(operator.substring(3));  

                // 中國移動和中國聯通獲取LAC、CID的方式  

                GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  

                int lac = location.getLac();  

                int cellId = location.getCid();  

                Log.i(TAG, " MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cellId);  

                // 中國電信獲取LAC、CID的方式  

                /*CdmaCellLocation location1 = (CdmaCellLocation) mTelephonyManager.getCellLocation(); 

                lac = location1.getNetworkId(); 

                cellId = location1.getBaseStationId(); 

                cellId /= 16;*/  

                // 獲取鄰區基站資訊  

                List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();  

                StringBuffer sb = new StringBuffer("總數 : " + infos.size() + "\n");  

                for (NeighboringCellInfo info1 : infos) { // 根據鄰區總數進行迴圈  

                    sb.append(" LAC : " + info1.getLac()); // 取出當前鄰區的LAC  

                    sb.append(" CID : " + info1.getCid()); // 取出當前鄰區的CID  

                    sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 獲取鄰區基站訊號強度  

                }  

                Log.i(TAG, " 獲取鄰區基站資訊:" + sb.toString());  

            }  

        });  

    }  

}  

AndroidManifest.xml新增獲取位置資訊的許可權:

[html]  www.2cto.com

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  

點選“獲取基站資訊”的按鈕後,Logcat的日誌輸出如下:

1、中國聯通:

2、中國移動: