Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add ConnectBluetoothDevice action #333

Closed
wants to merge 1 commit into from

1 participant

@clundie

I use this action to connect to a Bluetooth speaker that does not automatically connect.

@clundie

Looks like my speaker disconnects itself automatically after one minute. I'm not sure why. It doesn't do that when I manually connect it through the Bluetooth status item. (OS X Mavericks)

@dustinrue dustinrue closed this in 9830e21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 2, 2014
  1. @clundie
This page is out of date. Refresh to see the latest.
View
6 ControlPlane.xcodeproj/project.pbxproj
@@ -23,6 +23,7 @@
7CA6847717947E2200563601 /* LaptopLidEvidenceSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 7CA6847617947E2200563601 /* LaptopLidEvidenceSource.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
7CFF034917246B770098990F /* SearchDomainRule.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7CFF034B17246B770098990F /* SearchDomainRule.xib */; };
7CFF035A172471DD0098990F /* ServerAddressRule.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7CFF035C172471DD0098990F /* ServerAddressRule.xib */; };
+ 835B9DBA1913046400E646B0 /* ConnectBluetoothDeviceAction.m in Sources */ = {isa = PBXBuildFile; fileRef = 835B9DB91913046400E646B0 /* ConnectBluetoothDeviceAction.m */; };
8D01634F0C029913004AD8DB /* DNDArrayController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D01634D0C029913004AD8DB /* DNDArrayController.m */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
@@ -365,6 +366,8 @@
7CFF0360172472980098990F /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "pt-PT"; path = "pt-PT.lproj/ServerAddressRule.xib"; sourceTree = "<group>"; };
7CFF0361172472990098990F /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "pt-BR"; path = "pt-BR.lproj/ServerAddressRule.xib"; sourceTree = "<group>"; };
7CFF03621724729A0098990F /* it */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = it; path = it.lproj/ServerAddressRule.xib; sourceTree = "<group>"; };
+ 835B9DB81913046400E646B0 /* ConnectBluetoothDeviceAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ConnectBluetoothDeviceAction.h; path = Source/ConnectBluetoothDeviceAction.h; sourceTree = "<group>"; };
+ 835B9DB91913046400E646B0 /* ConnectBluetoothDeviceAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ConnectBluetoothDeviceAction.m; path = Source/ConnectBluetoothDeviceAction.m; sourceTree = "<group>"; };
8D01634C0C029913004AD8DB /* DNDArrayController.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DNDArrayController.h; path = Source/DNDArrayController.h; sourceTree = "<group>"; };
8D01634D0C029913004AD8DB /* DNDArrayController.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = DNDArrayController.m; path = Source/DNDArrayController.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
@@ -1148,6 +1151,8 @@
DD8274ED14114D620098FA6E /* System Actions */ = {
isa = PBXGroup;
children = (
+ 835B9DB81913046400E646B0 /* ConnectBluetoothDeviceAction.h */,
+ 835B9DB91913046400E646B0 /* ConnectBluetoothDeviceAction.m */,
8DF5A45E0BC239B100CBEB4B /* DefaultPrinterAction.h */,
8DF5A45F0BC239B100CBEB4B /* DefaultPrinterAction.m */,
8DA2F4120CE8630200F92D4D /* DesktopBackgroundAction.h */,
@@ -1525,6 +1530,7 @@
8D3BFF3F0C61BE4500B9B910 /* AboutPanel.m in Sources */,
8DB1EF240C6C60A300586F84 /* MailIMAPServerAction.m in Sources */,
8D1493F70C824B6D00D29FE8 /* BonjourEvidenceSource.m in Sources */,
+ 835B9DBA1913046400E646B0 /* ConnectBluetoothDeviceAction.m in Sources */,
8DFA46170CC33E46003EAC16 /* QuitApplicationAction.m in Sources */,
8D3C85150CDD8546009DBCD9 /* ScreenSaverStartAction.m in Sources */,
8DA2F4150CE8630200F92D4D /* DesktopBackgroundAction.m in Sources */,
View
BIN  Resources/en.lproj/Localizable.strings
Binary file not shown
View
3  Source/Action.m
@@ -221,6 +221,7 @@ - (NSArray *)executeAppleScriptReturningListOfStrings:(NSString *)script
#pragma mark -
+#import "ConnectBluetoothDeviceAction.h"
#import "DefaultBrowserAction.h"
#import "DefaultPrinterAction.h"
#import "DesktopBackgroundAction.h"
@@ -285,6 +286,7 @@ - (id)init
Gestalt(gestaltSystemVersionMinor, &minor);
classes = [[NSMutableArray alloc] initWithObjects:
+ [ConnectBluetoothDeviceAction class],
[DefaultBrowserAction class],
[DefaultPrinterAction class],
[DesktopBackgroundAction class],
@@ -369,6 +371,7 @@ - (id)init
if (NO) {
// Purely for the benefit of 'genstrings'
+ NSLocalizedString(@"ConnectBluetoothDevice", @"Action type");
NSLocalizedString(@"DefaultBrowser", @"Action type");
NSLocalizedString(@"DefaultPrinter", @"Action type");
NSLocalizedString(@"DesktopBackground", @"Action type");
View
14 Source/ConnectBluetoothDeviceAction.h
@@ -0,0 +1,14 @@
+//
+// ConnectBluetoothDeviceAction.h
+// ControlPlane
+//
+// Created by Chris Lundie on 1/May/2014.
+//
+
+@class ConnectBluetoothDeviceAction;
+
+#import "Action.h"
+
+@interface ConnectBluetoothDeviceAction : Action <ActionWithLimitedOptions>
+
+@end
View
121 Source/ConnectBluetoothDeviceAction.m
@@ -0,0 +1,121 @@
+//
+// ConnectBluetoothDeviceAction.m
+// ControlPlane
+//
+// Created by Chris Lundie on 1/May/2014.
+//
+
+#import "ConnectBluetoothDeviceAction.h"
+#import "ToggleBluetoothAction.h"
+#import <IOBluetooth/IOBluetooth.h>
+
+@interface ConnectBluetoothDeviceAction ()
+
+@property (copy) NSString *deviceAddressString;
+
+@end
+
+@implementation ConnectBluetoothDeviceAction
+
+- (instancetype)initWithOption:(NSString *)option
+{
+ self = [super init];
+ if (self) {
+ self.deviceAddressString = option;
+ }
+ return self;
+}
+
+- (instancetype)init
+{
+ return [self initWithOption:@""];
+}
+
+- (instancetype)initWithDictionary:(NSDictionary *)dict
+{
+ return [self initWithOption:dict[@"parameter"]];
+}
+
+- (void)dealloc
+{
+ self.deviceAddressString = nil;
+ [super dealloc];
+}
+
+- (NSMutableDictionary *)dictionary
+{
+ NSMutableDictionary *dict = [super dictionary];
+ dict[@"parameter"] = [[self.deviceAddressString copy] autorelease];
+ return dict;
+}
+
+- (NSString *)description
+{
+ NSString *format = NSLocalizedString(
+ @"Connecting to Bluetooth device '%@'.", @"");
+ return [NSString stringWithFormat:format, self.deviceAddressString];
+}
+
+- (BOOL)execute:(NSString **)errorString
+{
+ ToggleBluetoothAction *toggleAction =
+ [[[ToggleBluetoothAction alloc] initWithOption:@YES] autorelease];
+ NSString *error = nil;
+ BOOL didToggle = [toggleAction execute:&error];
+ if (!didToggle) {
+ NSLog(@"%s Aborting because Bluetooth could not be turned on",
+ __PRETTY_FUNCTION__);
+ *errorString = [[error copy] autorelease];
+ return NO;
+ }
+ IOBluetoothDevice *device =
+ [IOBluetoothDevice deviceWithAddressString:self.deviceAddressString];
+ IOReturn ioReturn = [device openConnection];
+ if (!device || (ioReturn != kIOReturnSuccess)) {
+ NSLog(@"%1$s Failed to connect to bluetooth device %2$@, return code %3$d",
+ __PRETTY_FUNCTION__, self.deviceAddressString, (int)ioReturn);
+ }
+ return device && (ioReturn == kIOReturnSuccess);
+}
+
++ (NSString *)helpText
+{
+ return NSLocalizedString(
+ @"The parameter for ConnectBluetoothDevice actions is the address of the"
+ @" device.",
+ @"");
+}
+
++ (NSString *)creationHelpText
+{
+ return NSLocalizedString(@"Connecting to Bluetooth device", @"");
+}
+
++ (NSArray *)limitedOptions
+{
+ NSArray *devices = [IOBluetoothDevice pairedDevices];
+ NSMutableArray *options = [NSMutableArray array];
+ for (IOBluetoothDevice *device in devices) {
+ NSString *deviceName = device.nameOrAddress;
+ NSString *deviceAddress = device.addressString;
+ if (deviceName && deviceAddress) {
+ [options addObject:@{
+ @"option": [[deviceAddress copy] autorelease],
+ @"description": [[deviceName copy] autorelease],
+ }];
+ }
+ }
+ return options;
+}
+
++ (NSString *)friendlyName
+{
+ return NSLocalizedString(@"Connect Bluetooth Device", @"");
+}
+
++ (NSString *)menuCategory
+{
+ return NSLocalizedString(@"Networking", @"");
+}
+
+@end
Something went wrong with that request. Please try again.