查看 TWS 连接状态
读取 TWS 设备当前是否已连接。
前置条件
- 设备支持
DeviceTWSAPI。 - UI 需要区分设备类型与连接状态时,请使用
isTws。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
连接状态由 DeviceTWSAPI 提供。
- Swift
- Objective-C
/// The protocol for device True Wireless Stereo (TWS) related API.
protocol DeviceTWSAPI: DeviceAPI {
/// Indicates whether the TWS device is currently connected.
var isTwsConnected: Bool { get }
}/// The protocol for device True Wireless Stereo (TWS) related API.
@protocol AIBudsDeviceTWSAPI <AIBudsDeviceAPI>
/// Indicates whether the TWS device is currently connected.
@property(nonatomic, readonly) BOOL isTwsConnected;
@end属性
| 属性 | 类型 | 描述 |
|---|---|---|
isTwsConnected | Bool / BOOL | 当前 TWS 连接快照。 |
使用示例
- Swift
- Objective-C
import AIBuds
guard let device = device as? DeviceTWSAPI else {
print("TWS information is unavailable")
return
}
guard device.isTws else {
print("This is not a TWS device")
return
}
print(device.isTwsConnected ? "TWS connected" : "TWS disconnected")id<AIBudsDeviceTWSAPI> device = (id<AIBudsDeviceTWSAPI>)self.device;
if ([device conformsToProtocol:@protocol(AIBudsDeviceTWSAPI)] && device.isTws) {
NSLog(@"%@", device.isTwsConnected ? @"TWS connected" : @"TWS disconnected");
}错误处理
该属性不返回错误。请将其视为时间点快照,并单独处理协议不支持的情况。
最佳实践
- 监听变化:实时指示器应实现
didTwsConnectionStatusChanged。 - 不要用于连接命令:该协议不会发起配对或重连。
- 安全更新 UI:按需将代理触发的 UI 工作派发到主队列。
注意事项
false表示当前 TWS 连接未活动,本身不代表该设备不是 TWS 设备。- SDK Demo 直接显示该属性,并通过设备代理更新。