iPhone/iOS获得基站信息[整理]

解决完了Android,下面应该就到了iPhone了。这个问题网上搜索的时候基本都是一个代码版本。
参考 iphone-wireless
当然,我们实际上有三种方法可以获取,目前我并未在真机上面测试,本文先做整理,后会放出iPhone上的测试版本代码。

iPhone上基站信息获取可以采用如下三种方法:

1 iphone-wireless方式获取。
2 串口发送AT指令获取。
3 模拟电话拨号 *3001#12345#* 获取。

第一种方法参考
获取CELLID,LAC等信息方法,感谢tiger723和shixuehuiab兄的代码。这里仅摘录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//CoreTelephony.m
#import "CoreTelephony.h"
#include <dlfcn.h>
#import <UIKit/UIKit.h>
#include <stdio.h>
#include <stdlib.h>

CFMachPortRef port ;
struct CTServerConnection  *sc =NULL ;
//struct CTServerConnection scc;
struct CellInfo cellinfo ;
int b ;
int t1 ;

@implementation CoreTelephony

int callback (CFNotificationCenterRef center ,  void  *observer , CFStringRef name ,  const  void  *object , CFDictionaryRef userInfo )
{
     printf ( "Callback calledn" ) ;
     return  0 ;
}

-  ( void ) getCellInfo
{
     int cellcount ;
     char * sdk_path  =  "/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony" ;

     // void * dlopen( const char * pathname, int mode );
     // 功能:以指定模式打开指定的动态连接库文件,并返回一个句柄给调用进程。 打开错误返回NULL,成功,返回库引用
     // RTLD_LAZY 暂缓决定,等有需要时再解出符号。这个参数使得未解析的symbol将在使用时去解析
     int * handle  =dlopen (sdk_path , RTLD_LAZY ) ;
     if  (handle  == NULL )  {
         return ;
     }

     // void* dlsym(void* handle,const char* symbol) 该函数在<dlfcn.h>文件中。将库中的一个函数绑定到预定义的函数地址(即获取到函数的指针)。handle是由dlopen打开动态链接库后返回的指针,symbol就是要求获取的函数的名称,函数返回值是void*,指向函数的地址,供调用使用。
     struct CTServerConnection  *  ( *CTServerConnectionCreate ) ( )  = dlsym (handle ,  "_CTServerConnectionCreate" ) ;
    sc =CTServerConnectionCreate (kCFAllocatorDefault , callback ,  &t1 ) ;

//    int (*CTServerConnectionGetPort)() = dlsym(handle, "_CTServerConnectionGetPort");
//    port=CFMachPortCreateWithPort(kCFAllocatorDefault, _CTServerConnectionGetPort(sc), NULL, NULL, NULL);

     void  ( *CTServerConnectionCellMonitorStart ) ( )  = dlsym (handle ,  "_CTServerConnectionCellMonitorStart" ) ;
    CTServerConnectionCellMonitorStart ( &t1 ,sc ) ;

     int *  ( *CTServerConnectionCellMonitorGetCellCount ) ( )  = dlsym (handle ,  "_CTServerConnectionCellMonitorGetCellCount" ) ;
    CTServerConnectionCellMonitorGetCellCount ( &t1 ,sc ,&cellcount ) ;
    NSLog (@ "cellcount:%d" ,cellcount ) ;

     void  ( *CTServerConnectionCellMonitorGetCellInfo ) ( )  = dlsym (handle ,  "_CTServerConnectionCellMonitorGetCellInfo" ) ;

     for ( int b = 0 ;b <cellcount ;b ++ )
     {
        NSAutoreleasePool * pool  =  [ [NSAutoreleasePool alloc ] init ] ;

         memset ( &cellinfo ,  0 ,  sizeof ( struct CellInfo ) ) ;
         int ts  =  0 ;

         /** 这个方法的问题出现在这里,3.0以前的版本是4个参数,运行后会崩溃,后来发现新版本中是5个参数,不过获取的结果不理想,只获取了5个结果:MNC,MCC,LAC,CELLID,PXLEVEL,其他4项四项返回-1*/
        CTServerConnectionCellMonitorGetCellInfo ( &t1 , sc , b ,  &ts ,  &cellinfo ) ;

         printf ( "Cell Site: %d, MNC: %d, " ,b ,cellinfo. servingmnc ) ;
         printf ( "LAC: %d, Cell ID: %d, Station: %d, " ,cellinfo. location , cellinfo. cellid , cellinfo. station ) ;
         printf ( "Freq: %d, RxLevel: %d, " , cellinfo. freq , cellinfo. rxlevel ) ;
         printf ( "C1: %d, C2: %dn" , cellinfo. c1 , cellinfo. c2 ) ;

         [pool release ] ;
        pool  = nil ;
     }

     // 使用dlclose()来卸载打开的库
    dlclose (handle ) ;
}

第二种方法同样参考自上篇文章:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "at.h"

int fd = InitConn ( 115200 ) ;
AT (fd ) ;

char buf [ 100 ]  =  "" ;

/** 这里是发送的AT命令,如下面的命令,可以获取CELL的一些信息, 还有其他的一些信息,网上找找就有了,不过不是所有的命令都可以用哦,有些iphone没有留接口,有就用到一个命令AT+CCED=0,1,这个可以获取CELL的较详细的信息,但是IPHONE就没有留接口,没法用,悲剧*/
//下面是一些我找到的,可以用的命令
//获取小区信息
//SendStrCmd(fd, "AT+CREG=2;+CREG?rn");
//获取当前小区的信号强度
//SendStrCmd(fd, "AT+CSQrn");
//基带信息
//SendStrCmd(fd, "At+xgendatarn");
//获得IMSI。这命令用来读取或者识别SIM卡的IMSI(国际移动签署者标识)。在读取IMSI之前应该先输入PIN(如果需要PIN的话)。
//SendStrCmd(fd, "AT+CCIDrn");
//获得SIM卡的标识。这个命令使模块读取SIM卡上的EF-CCID文件
//SendStrCmd(fd, "AT+CCIDrn");
//读取信息中心号码
//SendStrCmd(fd, "AT+CSCA?rn");
//单元广播信息标识。
//SendStrCmd(fd, "AT+CSCB?rn");

SendStrCmd (fd ,  "AT+XCELLINFO=1rn" ) ;
while  ( 1 )
{
read (fd , buf ,  100 ) ;
printf ( "%s" , buf ) ;
memset (buf ,  0 ,  100 ) ;
} ;

作者补充了代码:
at

另外,网上对以上方法做出了一些补充:

1. cellinfo的定义如下,估计就不会内存泄露了。
struct CellInfo
{
int mcc;
int mnc;
int location;
int cellid;
int station;
int freq;
int rxlevel;
int c1;
int c2;
int c3[6];
};
2.用AT命令取的话iphone4上试一下/dev/dlci.spi-baseband.extra_0。
3.也可能需要创造一个/tmp/ttydebugenable文件,然后 再/dev/tty.debug。

这个老帖子,现在还有人问,我也凑下热闹

代码没有问题,可以取到基站以及信号,但信号数字要减去-130或-110至于多少,需要你在查询相关资料

上面的代码只可以取到2G,信号,有些童鞋在实测发现,代码运行正常count一直是0,那是应该开着3G呢,至于3G怎么取我也在研究

AT指令确实可以取到比较丰富的信息,但在IPHONE4上应该是13楼所提到的文件dlci.spi-baseband.extra_0,但必须赋予+x权限,所以只能在开发试验使用,最终用户很难对权限进行更改

最后就是函数的问题,如果你是4.0以上的话,没有必要dlsym库里的函数,直接应用公开的库CoreTelephon.framwork即可,但需要声明下符号名称.

3 第三种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BOOL tryDial ( )  {
    NSString * rawNumber  =  [ [NSUserDefaults standardUserDefaults ] stringForKey : @ "phonenumber_preference" ] ;
     if (rawNumber  &&  ! [rawNumber isEqualToString :@ "" ] )  {
        NSString * telNumber  = @ "" ;
         for  ( int i = 0 ; i < [rawNumber length ] ; i ++ )  {
            NSString * chr  =  [rawNumber substringWithRange :NSMakeRange (i ,  1 ) ] ;
             if (doesStringContain (@ "*3001#12345#*" , chr ) )  {
                telNumber  =  [telNumber stringByAppendingFormat :@ "%@" , chr ] ;
             }
         }
        telNumber  =  [NSString stringWithFormat :@ "tel:%@" , telNumber ] ;
         [ [UIApplication sharedApplication ] openURL : [ [NSURL alloc ] initWithString :telNumber ] ] ;
         return TRUE ;
     }
     return FALSE ;
}

参考文章:
1 http://apt.weiphone.com/index.php?action=packageRead&id=9429
2 http://www.cocoachina.com/bbs/read.php?tid=70132
3 http://stackoverflow.com/questions/8846174/app-crash-at-ctserverconnectioncellmonitorgetcellinfo
4 http://www.devdiv.com/iOS_iPhone-iPhone%E4%B8%AD%E5%8F%96%E5%BE%97%E5%9F%BA%E7%AB%99%E7%9A%84cellid-thread-30564-1-1.html
5 http://www.cocoachina.com/downloads/video/2010/0713/1854.html
6 http://code.google.com/p/iphone-wireless/source/browse/trunk/CellStumbler/main.c

-End-

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linphone-iphone demo ios 是一个为 iOS 设备开发的开源软件,它提供了一个优秀的 IP 电话通讯网络服务,该服务可以实现 VoIP 通讯,即语音和视频通话以及音频和视频传输等。 Linphone-iphone demo ios 具有音频或视频呼叫、文本聊天、通讯录、语音信箱、呼叫转移、呼叫保持、呼叫等待等新颖有用的功能。Linphone-iphone demo ios 还支持 SIP 协议、SIP/TLS、SIP/UDP、IMAP、POP3、SMTP、SIP/RTP、SRTP 和 ZRTP 等主流的通信协议,几乎可以适应各类网络环境。 Linphone-iphone demo ios 是一个易于使用和实现的VoIP解决方案,在架构设计上,它可以和其他 VoIP 应用程序进行集成,可以与许多 SIP 服务器进行互操作性测试,并与一些常见 VoIP 应用程序比较常见性和稳定性。 Linphone-iphone demo ios 的整体性能也十分突出。它具有鲜明的 SIP 通信体验,提供了语音质量、通话时延与 VoIP 协议调和的解决方案,以保证声音质量清晰,通话时延低,并且网络环境不会影响通话质量。Linphone-iphone demo ios 的接口和界面设计都采用了最新的 UI 设计和开发技术,效果极佳,让用户使用起来非常友好。 总之,Linphone-iphone demo ios 是一款可以为 iOS 用户提供高质量 VoIP 通信服务的开源软件,丰富的功能、通用的协议以及高质量的音频/视频通话体验,使得它成为 VoIP 领域中一种非常有价值的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值