objective-c 在iphone sdk中以编程方式打开蓝牙?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1743610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:30:06  来源:igfitidea点击:

Programmatically turn on bluetooth in the iphone sdk?

iphoneobjective-cbluetoothipod-touch

提问by johnathon

I have seen a lot of questions about this but no one actually gives a real answer (frameworks to import, actual code etc). They only say with a private api and that will get your app rejected from the app store.

我已经看到很多关于此的问题,但实际上没有人给出真正的答案(要导入的框架、实际代码等)。他们只说带有私人 api,这将使您的应用程序被应用程序商店拒绝。

I am aware that use of a private api will get my app rejected by I was wondering how to do it for personal use. (iPhone SDK 3.1.2, iPod touch 2g)

我知道使用私有 api 会使我的应用程序被拒绝,因为我想知道如何将其用于个人用途。(iPhone SDK 3.1.2,iPod touch 2g)

回答by memmons

I've been looking into this as well. You need to include the bluetoothmanager framework and header file in your project. It shouldbe in

我也一直在研究这个。您需要在项目中包含 bluetoothmanager 框架和头文件。它应该

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library/PrivateFrameworks/BluetoothManager.framework/

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library/PrivateFrameworks/BluetoothManager.framework/

If the header file is not there, you'll need to grab a .h file that was generated from the library and include it in your project. I googled to find it; Here is one here:

如果头文件不存在,您需要获取从库生成的 .h 文件并将其包含在您的项目中。我用谷歌搜索找到它;这里有一个:

http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk/include/BluetoothManager/

http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk/include/BluetoothManager/

Once that is added to your project, your import should look like this if the header file was already in the framework:

一旦将其添加到您的项目中,如果头文件已经在框架中,您的导入应该如下所示:

#import <BluetoothManager/BluetoothManager.h>

Or this if you added your own BluetoothManager.h file to your project:

或者,如果您将自己的 BluetoothManager.h 文件添加到您的项目中:

#import "BluetoothManager.h

To toggle the bluetooth here is the code:

在这里切换蓝牙是代码:

BluetoothManager *manager = [BluetoothManager sharedInstance];
[manager setEnabled:![manager enabled]];    

I have built a utility to do this myself and it does work. Note, if all you want to do is create a utility to toggle the bluetooth and exit, without any UI, create a new project in XCode and use the Window-based Application template. Add the code to the didFinishLaunchingWithOptions method and replace [window makeKeyAndVisible]with exit(0).

我已经构建了一个实用程序来自己执行此操作,并且确实有效。请注意,如果您只想创建一个实用程序来切换蓝牙并退出,而无需任何 UI,请在 XCode 中创建一个新项目并使用基于窗口的应用程序模板。将代码添加到 didFinishLaunchingWithOptions 方法并替换[window makeKeyAndVisible]exit(0).

回答by WrightsCS

You need to make sure that binaries and headerfiles are BOTH in the PrivateFrameworks folders under:

您需要确保二进制文件和头文件都在以下 PrivateFrameworks 文件夹中:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/PrivateFrameworks

This will allow you to import PrivateFrameworks such as BluetoothManager.framework into your app and not get errors. You can find how to get the headers online. This works for 3.1.2 +cause Im writing an app right now that works perfectly on my device as well as Sim.

这将允许您将诸如 BluetoothManager.framework 之类的 PrivateFrameworks 导入您的应用程序而不会出错。您可以找到如何在线获取标题。这适用于 3.1.2 + 因为我现在正在编写一个可以在我的设备和 Sim 上完美运行的应用程序。

If your gonna test in the simulator, use the following:

如果您要在模拟器中进行测试,请使用以下命令:

#if TARGET_IPHONE_SIMULATOR
        //This is where simulator code goes that use private frameworks
#else
        /* this works in iOS 4.2.1 */
        Class BluetoothManager = objc_getClass("BluetoothManager");
        id btCont = [BluetoothManager sharedInstance];
        [btCont setPowered:YES];
#endif

回答by Raj

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

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

}
#endif

the above method will work in iOS 4.2.3

上述方法适用于 iOS 4.2.3

回答by radhoo

To get the BluetoothManager private api working you need to do the following: 1. get the 4 header files indicated by Harkonian and adding them to your SDK files (adding the header files to your project) 2. add the Framework to your project (adding the binary files to your project) 3. create a variable, for working with the BluetoothManager service Example: btManager = [BluetoothManager sharedInstance]; 4. use the BluetoothManager's methods, you can see them all in BluetoothManager.h

要使 BluetoothManager 私有 api 正常工作,您需要执行以下操作: 1. 获取 Harkonian 指示的 4 个头文件并将它们添加到您的 SDK 文件中(将头文件添加到您的项目中) 2. 将框架添加到您的项目中(添加将二进制文件添加到您的项目中) 3. 创建一个变量,用于使用 BluetoothManager 服务 示例:btManager = [BluetoothManager sharedInstance]; 4.使用BluetoothManager的方法,可以在BluetoothManager.h中看到

I put together a complete sample that is available here: http://www.pocketmagic.net/?p=2827

我整理了一个完整的样本,可以在这里找到:http: //www.pocketmagic.net/?p=2827

Hope this helps, Radu

希望这会有所帮助,拉杜

回答by vualoaithu

With Iphone 5s
[btCont setEnabled:!currentState] ; [btCont setPowered:!currentState] ;
not running

使用 Iphone 5s
[btCont setEnabled:!currentState] ; [btCont setPowered:!currentState] ;
没有运行

回答by Epsilon Prime

I believe the solution is to make a system call to launchctl since that's the daemon responsible for starting/stopping system services.

我相信解决方案是对 launchctl 进行系统调用,因为这是负责启动/停止系统服务的守护进程。