
BLEUnlock 是一款轻量级的 macOS 菜单栏实用工具。它通过检测您 iPhone、Apple Watch 或其他蓝牙低功耗(BLE)设备的接近状态,来自动锁定或解锁您的 Mac 屏幕。无需 iPhone 配套应用,利用您已有的设备即可实现智能化的安全防护。
打开终端,执行以下命令:
brew install bleunlock.zip 文件。BLEUnlock.app 拖拽到“应用程序”文件夹中。首次运行 BLEUnlock 需要进行以下设置:
以下是项目中的部分核心代码,展示了其基础工作原理。
1. 低层级屏幕与系统控制 (**lowlevel.c**)
此文件包含了控制显示器睡眠/唤醒以及立即锁定屏幕的底层 C 函数。
#include "lowlevel.h"
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOKitLib.h>
// 唤醒显示器
void wakeDisplay(void)
{
static IOPMAssertionID assertionID;
// 声明一个用户活动,防止系统进入睡眠,并唤醒显示器
IOPMAssertionDeclareUserActivity(CFSTR("BLEUnlock"), kIOPMUserActiveLocal, &assertionID);
}
// 使显示器进入睡眠
void sleepDisplay(void)
{
// 通过 IORegistry 访问显示器管理服务
io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
if (reg) {
// 设置属性请求显示器空闲(睡眠)
IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
IOObjectRelease(reg); // 释放资源
}
}2. 低层级函数头文件 (**lowlevel.h**)
定义了可供 Swift 代码调用的 C 函数接口。
#ifndef lowlevel_h
#define lowlevel_h
#include <stdbool.h>
void sleepDisplay(void); // 睡眠显示器
void wakeDisplay(void); // 唤醒显示器
int SACLockScreenImmediate(void); // 立即锁定屏幕(函数实现在其他文件)
#endif /* lowlevel_h */3. 媒体远程控制接口 (**MediaRemote.h**)
定义了用于控制 macOS 全局媒体播放(如音乐、视频)的私有框架函数。
#include <CoreFoundation/CoreFoundation.h>
// 媒体命令枚举
typedef enum {
MRCommandPlay, // 播放命令
MRCommandPause, // 暂停命令
} MRCommand;
// 获取当前播放状态的回调块类型
typedef void (^MRMediaRemoteGetNowPlayingApplicationIsPlayingCompletion)(Boolean isPlaying);
// 函数声明:获取是否正在播放
void MRMediaRemoteGetNowPlayingApplicationIsPlaying(dispatch_queue_t queue, MRMediaRemoteGetNowPlayingApplicationIsPlayingCompletion completion);
// 函数声明:发送播放/暂停命令
Boolean MRMediaRemoteSendCommand(MRCommand command, id userInfo);4. 启动器代理 (**AppDelegate.m**)
此代码属于应用的“启动器”组件,用于确保主应用只有一个实例运行。
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// 获取当前 bundle identifier (例如 com.example.BLEUnlock.Launcher)
NSString *id = [[NSBundle mainBundle] bundleIdentifier];
// 推导出主应用的 bundle identifier (去掉 .Launcher)
NSString *mainId = [id stringByReplacingOccurrencesOfString:@".Launcher" withString:@""];
// 检查主应用是否已经在运行
if ([NSRunningApplication runningApplicationsWithBundleIdentifier:mainId].count > 0) {
[NSApp terminate:self]; // 如果已运行,则终止启动器
}
// 构建主应用的路径 (向上回退四级目录)
NSString *path = [[NSBundle mainBundle] bundlePath];
NSMutableArray *components = [NSMutableArray arrayWithArray:[path pathComponents]];
[components removeLastObject]; // 移除 macOS/
[components removeLastObject]; // 移除 Contents/
[components removeLastObject]; // 移除 _CodeSignature 或类似目录
[components removeLastObject]; // 移除 BLEUnlock Launcher.app
NSString *mainPath = [NSString pathWithComponents:components]; // 得到 BLEUnlock.app 的路径
// 启动主应用程序
[[NSWorkspace sharedWorkspace] launchApplication:mainPath];
// 终止启动器自身
[NSApp terminate:self];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// 终止前的清理工作(如有)
}
@endkC6Ey/YuBG2vd8EVZfHu7L/x107n3ZMNPCO+hvaEEDo=
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。