调用私有方法开启关闭蓝牙

先导入IOBluetooth.framework

然后调用我写的方法

MTBluetooth.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@interface MTBluetooth : NSObject

/**
* 蓝牙是否可用
*
* @return
*/
+ (BOOL)isAvaliable;

/**
* 是否开启
*
* @return <#return value description#>
*/
+ (BOOL)isPower;

/**
* 开启蓝牙
*/
+ (void)openBluetooth;

/**
* 关闭蓝牙
*/
+ (void)closeBluetooth;

@end

MTBluetooth.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#import "MTBluetooth.h"


// private methods
int IOBluetoothPreferencesAvailable();

int IOBluetoothPreferenceGetControllerPowerState();
void IOBluetoothPreferenceSetControllerPowerState(int state);

int IOBluetoothPreferenceGetDiscoverableState();
void IOBluetoothPreferenceSetDiscoverableState(int state);

// dry
int BTSetParamState(int state, int (*getter)(), void (*setter)(int)) {
if (state == getter()) {
return EXIT_SUCCESS;
} else {
setter(state);

usleep(1000000); // Just wait, checking getter even in 10 seconds gives old result
return EXIT_SUCCESS;
}
}

// short names
typedef int (*getterFunc)();
typedef int (*setterFunc)(int);

#define BTAvaliable IOBluetoothPreferencesAvailable

#define BTPowerState IOBluetoothPreferenceGetControllerPowerState
int BTSetPowerState(int state) {
return BTSetParamState(state, BTPowerState, IOBluetoothPreferenceSetControllerPowerState);
}

#define BTDiscoverableState IOBluetoothPreferenceGetDiscoverableState
int BTSetDiscoverableState(int state) {
return BTSetParamState(state, BTDiscoverableState, IOBluetoothPreferenceSetDiscoverableState);
}

@implementation MTBluetooth

+ (BOOL)isAvaliable {
return BTAvaliable();
}

+ (BOOL)isPower {
if ([self isAvaliable]) {
return BTPowerState() == 1;
}
return NO;
}

+ (void)openBluetooth {
BTSetPowerState(1);
}

+ (void)closeBluetooth {
BTSetPowerState(0);
}

@end
坚持原创技术分享,您的支持将鼓励我继续创作!