-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bose.mm
65 lines (55 loc) · 2.65 KB
/
Bose.mm
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
63
64
65
#import "Bose.h"
@implementation BoseController {
dispatch_source_t closeSessionTimer;
}
+(BoseController *)sharedInstance {
static BoseController *boseController = nil;
if (boseController == nil) {
boseController = [BoseController new];
}
return boseController;
}
-(void)setCurrentBluetoothListeningMode:(NSString *)listeningMode forAccessory:(EAAccessory *)accessory v2: (BOOL) v2 {
[[SessionController sharedController] setupControllerForAccessory:accessory withProtocolString: v2 ? @"com.bose.bmap2" : @"com.bose.bmap"];
[[SessionController sharedController] openSession];
// create a new dispatch queue
dispatch_queue_t queue = dispatch_queue_create("com.semvis.sonitus.queue", NULL);
dispatch_async(queue, ^{
[[[SessionController sharedController] writeDataCondition] lock];
while (![[SessionController sharedController] hasSpaceAvailable] && [[SessionController sharedController] writeDataLength] != 0){
[[[SessionController sharedController] writeDataCondition] wait];
}
[[[SessionController sharedController] writeDataCondition] unlock];
bool isOff = [listeningMode isEqual:@"AVOutputDeviceBluetoothListeningModeNormal"];
bool isNC = [listeningMode isEqual:@"AVOutputDeviceBluetoothListeningModeActiveNoiseCancellation"];
char command[] = {0x01,
static_cast<char>(v2 ? 0x05 : 0x06),
0x02,
static_cast<char>(v2 ? 0x02 : 0x01),
static_cast<char>(isNC ? (v2 ? 0x00 : 0x01) : isOff ? (v2? 0x00 : 0x00) : (v2 ? 0x0a : 0x03)),
static_cast<char>(v2 ? (isOff? 0x00 : 0x01) : NULL)
};
[[SessionController sharedController] writeData:[NSData dataWithBytes:command length:sizeof(command) - (v2? 0 : 1)]];
[[[SessionController sharedController] writeDataCondition] lock];
while (![[SessionController sharedController] hasSpaceAvailable] && [[SessionController sharedController] writeDataLength] != 0){
[[[SessionController sharedController] writeDataCondition] wait];
}
[[[SessionController sharedController] writeDataCondition] unlock];
if (closeSessionTimer != nil){
dispatch_source_cancel(closeSessionTimer);
closeSessionTimer = nil;
}
closeSessionTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
if (closeSessionTimer) {
dispatch_source_set_timer(closeSessionTimer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 10 ), DISPATCH_TIME_FOREVER, (1ull * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(closeSessionTimer, ^{
[[SessionController sharedController] closeSession];
});
dispatch_resume(closeSessionTimer);
}
});
}
-(NSString *)getCurrentListeningModeOfAccessory: (EAAccessory *)accessory {
return nil;
}
@end