Is it possible to “disconnect” a Bluetooth device in Cocoa / ObjC?

I plugged in IOBluetoothDevice in my Mac / Cocoa application and would like to “unlock” it programmatically. That is, I would like to remove the device from the left pane of the Bluetooth section in the system settings.

I saw [IOBluetoothDevice removeFromFavorites] , but it just removes the heart icon next to the device’s Favorites attribute - the device is still displayed in the left pane.

Is this possible through Cocoa?

Bluetooth Section of System Preferences

In the above image, I want to programmatically remove the "Apple Mighty Mouse" from the left pane.

+8
objective-c cocoa bluetooth
source share
1 answer

Paired devices are part of the system settings.

You can find the file with the Bluetooth settings in /Library/Preferences , its name is com.apple.Bluetooth.plist .

com.apple.Bluetooth.plist

However, you cannot edit the file directly. You must use the SCPreferences class from System Configuration .

Please note that the API for accessing / changing system settings is rather low.

EDIT: The following code works if it is run in superuser mode. I myself am not a Mac OS developer, but it can be started using AuthorizationRef and run in user mode (the user will confirm access to the system configuration).

 SCPreferencesRef prefs = SCPreferencesCreate(kCFAllocatorDefault, CFSTR("Test"), CFSTR("/Library/Preferences/com.apple.Bluetooth.plist")); const CFStringRef PAIRED_DEVICES_KEY = CFSTR("PairedDevices"); NSArray *pairedDevices = (__bridge NSArray *) SCPreferencesGetValue(prefs, PAIRED_DEVICES_KEY); NSLog(@"Paired devices: %@", pairedDevices); NSString *deviceToRemove = @"e4-32-cb-da-ca-2f"; NSMutableArray *newPairedDevices = [pairedDevices mutableCopy]; [newPairedDevices removeObject:deviceToRemove]; Boolean valueSet = SCPreferencesSetValue(prefs, PAIRED_DEVICES_KEY, (__bridge CFPropertyListRef) [NSArray arrayWithArray:newPairedDevices]); NSLog(@"Value set: %@", (valueSet) ? @"YES" : @"NO"); if (!valueSet) { NSLog(@"Error: %@", SCCopyLastError()); } Boolean saved = SCPreferencesCommitChanges(prefs); if (!saved) { NSLog(@"Error: %@", SCCopyLastError()); } NSLog(@"Saved: %@", (saved) ? @"YES" : @"NO"); CFRelease(prefs); 
+9
source share

All Articles