iOS设备中WiFi、蓝牙和飞行模式的开启与关闭

来源:互联网 发布:软件测试的发展前途 编辑:程序博客网 时间:2024/04/20 08:57

iOS设备中WiFi、蓝牙和飞行模式的开启与关闭

分类: ios开发 1086人阅读 评论(0) 收藏 举报
iosnulliphone测试hookclass

转自:http://www.cnblogs.com/OtionSky/archive/2011/11/08/iOS_WiFi_Controller.html

今天写了一段有关在iPhone程序中开关WiFi型号的代码,经测试运行良好。

我想不用我多说大家都应该知道以上的功能只能在越狱的设备中实现!

好了,闲话稍少叙,进入正题:

1.首先要在SpringBoard启动之后,我们要执行hook动作:

  NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
      if ([identifier isEqualToString:@"com.apple.springboard"]) {
          Class $SpringBoard = (objc_getClass("SpringBoard"));
          _SpringBoard$applicationDidFinishLaunching$ = MSHookMessage($SpringBoard, @selector(applicationDidFinishLaunching:), &$SpringBoard$applicationDidFinishLaunching$);
      }

2. 然后实现我们的HOOK函数,这里我们仅仅是注册了两个消息:

  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
                                    &NotificationReceivedCallback, CFSTR("turnOffWiFi"), NULL, 
                                    CFNotificationSuspensionBehaviorCoalesce);
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL,
                                    &NotificationReceivedCallback, CFSTR("turnOnWiFi"), NULL,
                                    CFNotificationSuspensionBehaviorCoalesce);

3. 最后我们要响应这两个信号,也就是实现我们在第二步中指明的那个回调方法(NotificationReceivedCallback)

static void NotificationReceivedCallback(CFNotificationCenterRef center, 
                                         void *observer, CFStringRef name, 
                                         const void *object, CFDictionaryRef 
                                         userInfo) 
{
    BOOL offOrOn = YES;
    if ([(NSString *)name isEqualToString:@"turnOffWiFi"]) {
        offOrOn = NO;
    } else if ([(NSString *)name isEqualToString:@"turnOnWiFi"]) {
        offOrOn = YES;
    }
    [[objc_getClass("SBWiFiManager") sharedInstance] setWiFiEnabled:offOrOn];
}

也就是这一步正真的对WiFi信号进行开关操作。好了我们的后台程序(dynamicLibrary)已经编写完成了。

然后在我们的前台程序中我们找个事件来发送我们在后台注册的那两个消息,例如一个Button和一个BOOL值来完成这功能:

 BOOL turnOff = YES;
    if (turnOn) {
                CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);
    } else {
                CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("turnOffWiFi"), NULL, NULL, 0);

    }

测试设备:iPhone 3GS

系统:iOS 4.3.3

设备状态:已越狱

测试结果:Perfect!

 

注1:使用相同的方法我们可以启动和关闭蓝牙:

首先,我们要从BluetoothManager.framework这个私有库中dump出BluetoothManager这个类;

然后,我们就可以调用这个类的setPowered:方法启动和关闭蓝牙了(参数:YES为启动、NO为关闭)。

带有.h文件的BluetoothManager.framework请到我的资源里下载

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>  
  2. #import <BluetoothManager/BluetoothManager.h>  
  3. //#import <SpringBoard/SBWiFiManager.h>  
  4.   
  5. @interface hylViewController : UIViewController{  
  6.     BOOL isBlueToothOn;  
  7.     BOOL isWlanOn;  
  8. }  
  9.   
  10. @end  

[cpp] view plaincopyprint?
  1. #import "hylViewController.h"  
  2.   
  3. @implementation hylViewController  
  4. #pragma mark - View lifecycle  
  5. - (void)viewDidLoad  
  6. {  
  7.     [super viewDidLoad];  
  8.   
  9. Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;  
  10.     id btCont = [BluetoothManager sharedInstance] ;  
  11.     isBlueToothOn = [btCont enabled] ;  
  12. UISwitch *blueTouthSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(150,50,0,0)];  
  13.     [blueTouthSwitch addTarget:self action:@selector(BlueTouthSwitchAction:) forControlEvents:UIControlEventValueChanged];  
  14.     blueTouthSwitch.on = isBlueToothOn;  
  15.     [self.view addSubview:blueTouthSwitch];  
  16.     [blueTouthSwitch release];  
  17. }  
  18.   
  19. -(void)BlueTouthSwitchAction:(id)sender  
  20. {  
  21.     #if TARGET_IPHONE_SIMULATOR  
  22.         exit( EXIT_SUCCESS ) ;  
  23.     #else  
  24.         /* this works in iOS 4.2.3 */  
  25.         Class BluetoothManager = objc_getClass( "BluetoothManager" );  
  26.         id btCont = [BluetoothManager sharedInstance];  
  27.         [self performSelector:@selector(toggle:) withObject:btCont afterDelay:1.0f];  
  28.     #endif  
  29. }  
  30. #if TARGET_IPHONE_SIMULATOR  
  31. #else  
  32. - (void)toggle:(id)btCont  
  33. {  
  34.         BOOL currentState = [btCont enabled] ;  
  35.         [btCont setEnabled:!currentState] ;  
  36.         [btCont setPowered:!currentState] ;  
  37. }  
  38. #endif  
  39. @end  



经过我的测试是可以正常使用的。

注2:我们同样可以对飞行模式作开启和关闭操作:

首先:我们从SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager这两个 类,前者主要负责功能的开关,后者则是负责UI显示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到当前的 飞行模式状态,setIsInAirplaneMode:这个方法来设置飞行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem来刷新UI显示状态。

0 0