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,基站信号强度。

具体实现代码如下:

  1. package com.easipass.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.telephony.NeighboringCellInfo;  
  9. import android.telephony.TelephonyManager;  
  10. import android.telephony.cdma.CdmaCellLocation;  
  11. import android.telephony.gsm.GsmCellLocation;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14.   
  15. /** 
  16.  * 功能描述:通过手机信号获取基站信息 
  17.  * # 通过TelephonyManager 获取lac:mcc:mnc:cell-id 
  18.  * # MCC,Mobile Country Code,移动国家代码(中国的为460); 
  19.  * # MNC,Mobile Network Code,移动网络号码(中国移动为0,中国联通为1,中国电信为2);  
  20.  * # LAC,Location Area Code,位置区域码; 
  21.  * # CID,Cell Identity,基站编号; 
  22.  * # BSSS,Base station signal strength,基站信号强度。 
  23.  * @author android_ls 
  24.  */  
  25. public class GSMCellLocationActivity extends Activity {  
  26.   
  27.     private static final String TAG = "GSMCellLocationActivity";  
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.   
  34.         // 获取基站信息   
  35.         findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {  
  36.   
  37.             @Override  
  38.             public void onClick(View v) {  
  39.   
  40.                 TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  41.   
  42.                 // 返回值MCC + MNC   
  43.                 String operator = mTelephonyManager.getNetworkOperator();  
  44.                 int mcc = Integer.parseInt(operator.substring(03));  
  45.                 int mnc = Integer.parseInt(operator.substring(3));  
  46.   
  47.                 // 中国移动和中国联通获取LAC、CID的方式   
  48.                 GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  
  49.                 int lac = location.getLac();  
  50.                 int cellId = location.getCid();  
  51.   
  52.                 Log.i(TAG, " MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cellId);  
  53.   
  54.                 // 中国电信获取LAC、CID的方式   
  55.                 /*CdmaCellLocation location1 = (CdmaCellLocation) mTelephonyManager.getCellLocation(); 
  56.                 lac = location1.getNetworkId(); 
  57.                 cellId = location1.getBaseStationId(); 
  58.                 cellId /= 16;*/  
  59.                   
  60.                 // 获取邻区基站信息   
  61.                 List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();  
  62.                 StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");  
  63.                 for (NeighboringCellInfo info1 : infos) { // 根据邻区总数进行循环   
  64.                     sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC   
  65.                     sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID   
  66.                     sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度   
  67.                 }  
  68.   
  69.                 Log.i(TAG, " 获取邻区基站信息:" + sb.toString());  
  70.   
  71.             }  
  72.         });  
  73.   
  74.     }  
  75.   
  76. }  
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添加获取位置信息的权限:

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

点击“获取基站信息”的按钮后,Logcat的日志输出如下:

1、中国联通:


2、中国移动:

关于定位部分请查看: Android基站定位——单基站定位(二)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值