有没有办法在 iOS 中以编程方式打开和关闭蓝牙和/或 WiFi?

发布于 2024-10-08 21:58:28 字数 491 浏览 2 评论 0原文

我正在寻找一种简单的方法来在 iOS 4.x 设备(iPhone 和 iPad)上切换蓝牙和 WiFi 的打开和关闭状态。

当我在不同的位置和使用场景之间移动时,我会不断地切换这些功能,现在需要多次点击并访问“设置”应用程序。我希望创建一个简单的应用程序,它位于 Springboard 上,我只需点击它,它就会关闭 wifi(如果它已打开),反之亦然,然后立即退出。与用于切换蓝牙状态的应用程序类似。

我拥有开发人员 SDK,并且熟悉 Xcode 和 iOS 开发,因此很乐意编写创建应用程序所需的代码。我只是不知道哪个 API(无论是否私有)具有简单切换这些设施状态所需的功能。

因为这是一个非常个人化的问题,我无意尝试销售该应用程序或将其放在应用程序商店中,因此遵守有关 API 使用的应用程序指南不是问题。我不想做的是越狱设备,因为我想保留核心软件。

任何人都可以向我指出一些示例代码或有关实现此目标的更多信息,因为我的 Google-fu 让我失望,如果有 4.x 设备的信息,我就是找不到它。

I am looking for an easy way to toggle both bluetooth and wifi between on and off states on iOS 4.x devices (iPhone and iPad).

I am constantly toggling these functions as I move between different locations and usage scenarios, and right now it takes multiple taps and visits to the Settings App. I am looking to create a simple App, that lives on Springboard, that I can just tap and it will turn off the wifi if it's on, and vice versa, then immediately quit. Similarly with an App for toggling bluetooth’s state.

I have the developer SDK, and am comfortable in Xcode and with iOS development, so am happy to write the required code to create the App. I am just at a loss as to which API, private or not, has the required functionality to simply toggle the state of these facilities.

Because this is scratching a very personal itch, I have no intent to try and sell the App or get it up on the App store, so conforming with App guidelines on API usage is a non-issue. What I don’t want to do is jailbreak the devices, as I want to keep the core software as shipped.

Can anyone point me at some sample code or more info on achieving this goal, as my Google-fu is letting me down, and if the information is out there for 4.x devices I just can’t find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2024-10-15 21:58:28

感谢 Matt Farrugia(Twitter 上的@mattfarrugia),我一直在寻找的答案是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

#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:0.1f] ;
#endif
    return YES ;
}

#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
    BOOL currentState = [btCont enabled] ;
    [btCont setEnabled:!currentState] ;
    [btCont setPowered:!currentState] ;
    exit( EXIT_SUCCESS ) ;
}
#endif

您还需要链接到 Gamekit 框架,但只需将此代码添加到新的 Xcode 项目并在设备上运行即可。这样做会创建一个一键式应用程序,用于打开和关闭蓝牙。

Thanks to Matt Farrugia (@mattfarrugia on Twitter) the answer I was looking for was:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

#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:0.1f] ;
#endif
    return YES ;
}

#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
    BOOL currentState = [btCont enabled] ;
    [btCont setEnabled:!currentState] ;
    [btCont setPowered:!currentState] ;
    exit( EXIT_SUCCESS ) ;
}
#endif

You need to link against the Gamekit framework as well, but simply add in this code to a new Xcode project and run on the device. Doing so creates a 1-tap App that toggles Bluetooth on and off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文