检测蓝牙已连接 的情况下更加具体的获取一些当前连接设备的信息数据,具体实现方法如下:
#pragma mark - ****************************** 检测已连接的蓝牙设备
+ (NSMutableDictionary *)getDetectsConnectedBluetoothDevices {
AVAudioSessionPortDescription *portDescription = [YHUtility isBluetoothHeadsetConnectedEntity];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
if (portDescription) { // 已连接UID
NSLog(@"[蓝牙检测] - 状态 - 已连接:\n[类型] - %@\n[名称] - %@\n[UID] - %@", [portDescription portType], [portDescription portName], [portDescription UID]);
[dict setValue:[NSString convertNull:[portDescription portType]] forKey:@"type"]; // 连接方式
[dict setValue:[NSString convertNull:[portDescription portName]] forKey:@"name"]; // 设备名称
[dict setValue:@"active" forKey:@"state"]; // 连接状态(激活: active & 未激活: unactivated)
} else { // 未连接
[dict setValue:@"none" forKey:@"state"];
}
return dict;
}
#pragma mark - ****************************** 蓝牙耳机是否链接 - 实体
+ (AVAudioSessionPortDescription *)isBluetoothHeadsetConnectedEntity {
/*
蓝牙开启未链接耳机:
Speaker
单向保真音频协议(输出):
BluetoothA2DPOutput
...
双向保真音频协议(输入 & 输入):
BluetoothHFP - HFP(Hands-Free Profile)
BluetoothHSP - HSP(HeadSet Profile)
其它:
Receiver
*/
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription *routeDescription = [session currentRoute];
// NSLog(@"%@", routeDescription);
if (routeDescription) {
NSArray *outputs = [routeDescription outputs]; // 输入源
if (outputs && 0 < [outputs count]) {
AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
NSString *portType = [portDescription portType];
NSLog(@"PortType: %@", portType); // 蓝牙开启未链接: Speaker && 链接耳机: BluetoothA2DPOutput BluetoothHFP BluetoothHSP
if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"]) {
NSLog(@"[蓝牙检测] - 状态 - 输出 - %@", [portDescription portName]);
return portDescription;
} else if (portType && [portType isEqualToString:@"BluetoothHFP"] && [portType isEqualToString:@"BluetoothHSP"]) {
NSLog(@"[蓝牙检测] - 状态 - 输出 & 输入 - %@", [portDescription portName]);
return portDescription;
}
}
}
return nil;
}
以上便是此次分享的全部内容,希望能对大家有所帮助!
网友评论