如何检测 iPhone 状态栏中蓝牙图标的状态?

发布于 2024-10-07 19:11:34 字数 150 浏览 4 评论 0原文

正如问题所述,有没有办法查询状态栏的当前图标以及它们正在使用哪个 png?如果我可以这样做,我就可以测试蓝牙是否打开以及是否连接了设备。我想指出,这与使用 EAAccessory 不同,后者是不同的连接。在本例中,我只是尝试确定是否连接了耳机,而不管 EAAccessory 状态如何。

As the question states, is there a way to query the status bar for its current icons, and which png they're using? If I could do so, I'd be able to test if BlueTooth was on, and if it had a device connected. I want to note, this is different than using the EAAccessory, which is a different connection. In this case, I'm just trying to establish if there's a headset connected, regardless of EAAccessory state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-10-14 19:11:34

您可以采用私有 API 路线,但这很可能会让您的应用程序被拒绝。我之前曾有过一些私有 API 偷偷溜过 Apple,但在更新中被拒绝,所以请注意这一点。

CoreBluetooth

与普遍看法相反,这是可能的。您可以使用CoreBluetooth 库。我确信,如果您的应用程序没有展示蓝牙外围设备的用途,它可能会被拒绝。

但是,以下是我在使用蓝牙外围设备的应用程序之一中检测蓝牙状态的方法。请注意,这不是确定蓝牙状态的最佳方法,但可能对某些人有用。

- (BOOL)isBluetoothEnabled
{
    if ( ! _centralManager ) {
           _centralManager = [[CBCentralManager alloc]
                              initWithDelegate:self
                              queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                              options:@{
                                        CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]
                                        }];
    }
    switch ( _centralManager.state )
    {
        case CBCentralManagerStateUnknown: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateResetting: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnsupported: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnauthorized: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOff: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOn: {
            _bluetoothEnabled = YES;
            break;
        }
        default: { // Unknown
            _bluetoothEnabled = NO;
            break;
        }
    }
    return _bluetoothEnabled;
}

You can go the Private API route but this will most likely get your app rejected. I have had some Private API sneak past Apple before, but rejected in an update, so be mindful of that.

CoreBluetooth

Contrary to popular belief, this is possible. You can use the CoreBluetooth library. I'm sure that if your app does not demonstrate a use for Bluetooth peripherals, it could be rejected.

However, here is how I detect the Bluetooth state in one of my apps that does use Bluetooth peripherals. Note that this is not the best way to determine the Bluetooth state, but may work for someone.

- (BOOL)isBluetoothEnabled
{
    if ( ! _centralManager ) {
           _centralManager = [[CBCentralManager alloc]
                              initWithDelegate:self
                              queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                              options:@{
                                        CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]
                                        }];
    }
    switch ( _centralManager.state )
    {
        case CBCentralManagerStateUnknown: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateResetting: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnsupported: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStateUnauthorized: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOff: {
            _bluetoothEnabled = NO;
            break;
        }
        case CBCentralManagerStatePoweredOn: {
            _bluetoothEnabled = YES;
            break;
        }
        default: { // Unknown
            _bluetoothEnabled = NO;
            break;
        }
    }
    return _bluetoothEnabled;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文