zoukankan      html  css  js  c++  java
  • iOS设备中WiFi、蓝牙和飞行模式的开启与关闭

    转自: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显示状态。

  • 相关阅读:
    linux命令大全
    linux几个常用命令
    linux基础入门命令自我总结——乱
    linux标准目录的结构——各个目录都是干啥的
    python基础知识9——模块2——常见内置模块
    python基础知识8——模块1——自定义模块和第三方开源模块
    手写数据结构-链表
    手写数据结构-基于动态数组的循环队列
    手写数据结构-基于动态数组的队列
    手写数据结构-基于动态数组的栈
  • 原文地址:https://www.cnblogs.com/killiancheung/p/4755326.html
Copyright © 2011-2022 走看看