您的位置:首页 > 移动开发 > IOS开发

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

2015-08-24 17:18 351 查看
转自: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请到我的资源里下载

#import <UIKit/UIKit.h>
#import <BluetoothManager/BluetoothManager.h>
//#import <SpringBoard/SBWiFiManager.h>

@interface hylViewController : UIViewController{
BOOL isBlueToothOn;
BOOL isWlanOn;
}

@end


  

#import "hylViewController.h"

@implementation hylViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];

Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
isBlueToothOn = [btCont enabled] ;
UISwitch *blueTouthSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(150,50,0,0)];
[blueTouthSwitch addTarget:self action:@selector(BlueTouthSwitchAction:) forControlEvents:UIControlEventValueChanged];
blueTouthSwitch.on = isBlueToothOn;
[self.view addSubview:blueTouthSwitch];
[blueTouthSwitch release];
}

-(void)BlueTouthSwitchAction:(id)sender
{
#if TARGET_IPHONE_SIMULATOR
exit( EXIT_SUCCESS ) ;
#else
/* this works in iOS 4.2.3 */
Class BluetoothManager = objc_getClass( "BluetoothManager" );
id btCont = [BluetoothManager sharedInstance];
[self performSelector:@selector(toggle:) withObject:btCont afterDelay:1.0f];
#endif
}
#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
BOOL currentState = [btCont enabled] ;
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;
}
#endif
@end


  

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

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

首先:我们从SpringBoard中可以dump出SBTelephonyManager和SBStatusBarDataManager这两个 类,前者主要负责功能的开关,后者则是负责UI显示的。使用SBTelephonyManager的isInAirplaneMode方法可以得到当前的 飞行模式状态,setIsInAirplaneMode:这个方法来设置飞行模式。使用SBStatusBarDataManager的 airplaneModeChanged和_updateSignalStrengthItem来刷新UI显示状态。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: