Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jabra support #21

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Jabra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import <CoreBluetooth/CoreBluetooth.h>
#import <ExternalAccessory/ExternalAccessory.h>
#import <Foundation/Foundation.h>
#include "spawn.h"

@interface JabraController : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>
+(JabraController *)sharedInstance;
-(void)setCurrentBluetoothListeningMode:(NSString *)listeningMode;
-(NSString *)getCurrentListeningMode;
-(void)setListeningMode:(NSString *)listeningMode forPeripheral:(CBPeripheral *)peripheral forCharacteristic:(CBCharacteristic *)characteristic initializeConnection: (BOOL)initialize;

@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheral *peripheral;
@property (nonatomic, strong) CBCharacteristic *foundCharacteristic;
@property (nonatomic, strong) NSString *shouldChangeTolisteningMode;
@property (nonatomic, strong) dispatch_queue_t centralQueue;
@end
111 changes: 111 additions & 0 deletions Jabra.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#import "Jabra.h"

@implementation JabraController {}

+(JabraController *)sharedInstance {
static JabraController *jabraController = nil;
if (jabraController == nil) {
jabraController = [JabraController new];
}

return jabraController;
}

-(void)setCurrentBluetoothListeningMode:(NSString *)listeningMode {
pid_t pid;
const char *args[] = {"killall", "-9", "JabraMoments", NULL};
posix_spawn(&pid, "/usr/bin/killall", NULL, NULL, (char *const *)args, NULL);

self.shouldChangeTolisteningMode = listeningMode;
if (self.centralManager == nil) {
self.centralQueue = dispatch_queue_create("com.semvis.sonitus", DISPATCH_QUEUE_SERIAL);
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:self.centralQueue];
}
if (self.peripheral != nil && self.foundCharacteristic != nil) {
if (self.peripheral.state == CBPeripheralStateConnected) {
[self setListeningMode:listeningMode forPeripheral:self.peripheral forCharacteristic:self.foundCharacteristic initializeConnection:NO];
} else {
[self.centralManager connectPeripheral:self.peripheral options:nil];
}
} else {
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FEFF"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}];
}
}

-(void)setListeningMode:(NSString *)listeningMode forPeripheral:(CBPeripheral *)peripheral forCharacteristic:(CBCharacteristic *)characteristic initializeConnection:(BOOL)initialize {
bool isOff = [listeningMode isEqual:@"AVOutputDeviceBluetoothListeningModeNormal"];
bool isNC = [listeningMode isEqual:@"AVOutputDeviceBluetoothListeningModeActiveNoiseCancellation"];
bool isTP = [listeningMode isEqual:@"AVOutputDeviceBluetoothListeningModeAudioTransparency"];

if (isTP || isNC || isOff){
self.shouldChangeTolisteningMode = nil;

Byte command[] = {0x0d, 0x09, 0x00, 0x88, 0x13, 0xbe, 0x01, static_cast<Byte>(isOff ? 0x01 : isNC ? 0x04 : 0x02)};
[peripheral writeValue:[NSData dataWithBytes:command length:sizeof(command)] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];

Byte cap[] = {0x0d, 0x09, 0x00, 0x47, 0x13, 0xbe, 0x01};
[peripheral writeValue:[NSData dataWithBytes:cap length:sizeof(cap)] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];

if (isTP || isNC){
cap[6] = static_cast<Byte> (isTP ? 0x02 : 0x03);
[peripheral writeValue:[NSData dataWithBytes:cap length:sizeof(cap)] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}

if (isNC || isOff) {
Byte ack[] = {0x0d, 0x09, 0x00, 0x46, 0x13, 0x76};
for (int i = 0; i < 2; i++) {
[peripheral writeValue:[NSData dataWithBytes:ack length:sizeof(ack)] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}
}
}
}

-(NSString *)getCurrentListeningMode {
return nil;
}

-(void)centralManagerDidUpdateState:(CBCentralManager *)central {}


-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
self.peripheral = peripheral;
self.peripheral.delegate = self;
if (peripheral.state != CBPeripheralStateConnected) {
[self.centralManager connectPeripheral:self.peripheral options:nil];
} else {
if (self.shouldChangeTolisteningMode != nil) {
if (self.foundCharacteristic != nil) {
[self setListeningMode:self.shouldChangeTolisteningMode forPeripheral:peripheral forCharacteristic:self.foundCharacteristic initializeConnection:NO];
} else {
[self.peripheral discoverServices:nil];
}
}
}
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) return;

for (CBService *service in peripheral.services) {
peripheral.delegate = self;
[peripheral discoverCharacteristics:nil forService:service];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) return;

for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"8B5B80C0-AC6D-11E4-BAEF-0002A5D5C51B"]]) {
self.foundCharacteristic = characteristic;
if (self.shouldChangeTolisteningMode != nil) {
[self.centralManager stopScan];
[self setListeningMode:self.shouldChangeTolisteningMode forPeripheral:peripheral forCharacteristic:self.foundCharacteristic initializeConnection:YES];
}
break;
}
}
}
@end
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ THEOS_DEVICE_IP = 192.168.2.15
include $(THEOS)/makefiles/common.mk
ARCHS = arm64 arm64e
TWEAK_NAME = Sonitus
$(TWEAK_NAME)_FILES = Tweak.xm SessionController.mm Sony.mm Bose.mm Soundcore.mm Sennheiser.mm
$(TWEAK_NAME)_FILES = Tweak.xm SessionController.mm Sony.mm Bose.mm Soundcore.mm Sennheiser.mm Jabra.mm
SUBPROJECTS += Preferences
$(TWEAK_NAME)_CFLAGS = -fobjc-arc -std=c++17
$(TWEAK_NAME)_FRAMEWORKS = Foundation ExternalAccessory CoreBluetooth
Expand Down
1 change: 1 addition & 0 deletions Tweak.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Foundation/Foundation.h>
#import "Sony.h"
#import "Bose.h"
#import "Jabra.h"
#import "Soundcore.h"
#import "Sennheiser.h"
#import <Cephei/HBPreferences.h>
Expand Down
1 change: 1 addition & 0 deletions Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ NSString *previous = @"AVOutputDeviceBluetoothListeningModeNormal";
}
}
[[SoundcoreController sharedInstance] setCurrentBluetoothListeningMode:arg1];
[[JabraController sharedInstance] setCurrentBluetoothListeningMode:arg1];
return YES;
}
return %orig;
Expand Down
2 changes: 1 addition & 1 deletion control
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: com.semvis.sonitus
Name: Sonitus
Version: 1.0.5
Version: 1.0.6
Architecture: iphoneos-arm
Description: Sonitus, bring the noise cancellation control of your headphones to the built-in iOS menu.
Maintainer: Sem Visscher
Expand Down
2 changes: 1 addition & 1 deletion rootless.patch
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ index 29931eb..b5e4adb 100644
-ARCHS = arm64 arm64e
+ARCHS = arm64e
TWEAK_NAME = Sonitus
$(TWEAK_NAME)_FILES = Tweak.xm SessionController.mm Sony.mm Bose.mm Soundcore.mm Sennheiser.mm
$(TWEAK_NAME)_FILES = Tweak.xm SessionController.mm Sony.mm Bose.mm Soundcore.mm Sennheiser.mm Jabra.mm
SUBPROJECTS += Preferences

diff --git a/Preferences/Makefile b/Preferences/Makefile
Expand Down