参考链接:
http://www.devdiv.com/iOS_iPhone ... read-30564-1-1.html
//CoreTelephony.h
#import <Foundation/Foundation.h>
struct CTServerConnection
{
int a;
int b;
CFMachPortRef myport;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
};
struct CellInfo
{
int servingmnc;
int network;
int location;
int cellid;
int station;
int freq;
int rxlevel;
// int freq;
int c1;
int c2;
};
@interface CoreTelephony : NSObject
- (void) getCellInfo;
int callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
@end
//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 called\n");
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: %d\n", cellinfo.c1, cellinfo.c2);
[pool release];
pool = nil;
}
// 使用dlclose()来卸载打开的库
dlclose(handle);
}
要注意以下两点:
1.dlsym()方法只是获取到了所需函数的指针,需要执行相应函数才能取得所需的值;
2.方法的执行顺序应该为:打开动态库、创建服务、执行服务(先取得信息数再循环取得信息)、关闭动态库。
http://www.devdiv.com/iOS_iPhone ... read-30564-1-1.html
//CoreTelephony.h
#import <Foundation/Foundation.h>
struct CTServerConnection
{
int a;
int b;
CFMachPortRef myport;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
};
struct CellInfo
{
int servingmnc;
int network;
int location;
int cellid;
int station;
int freq;
int rxlevel;
// int freq;
int c1;
int c2;
};
@interface CoreTelephony : NSObject
- (void) getCellInfo;
int callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
@end
//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 called\n");
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: %d\n", cellinfo.c1, cellinfo.c2);
[pool release];
pool = nil;
}
// 使用dlclose()来卸载打开的库
dlclose(handle);
}
要注意以下两点:
1.dlsym()方法只是获取到了所需函数的指针,需要执行相应函数才能取得所需的值;
2.方法的执行顺序应该为:打开动态库、创建服务、执行服务(先取得信息数再循环取得信息)、关闭动态库。