檢測藍(lán)牙已連接 的情況下更加具體的獲取一些當(dāng)前連接設(shè)備的信息數(shù)據(jù),具體實現(xiàn)方法如下:
#pragma mark - ****************************** 檢測已連接的藍(lán)牙設(shè)備
+ (NSMutableDictionary *)getDetectsConnectedBluetoothDevices {
AVAudioSessionPortDescription *portDescription = [YHUtility isBluetoothHeadsetConnectedEntity];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
if (portDescription) { // 已連接UID
NSLog(@"[藍(lán)牙檢測] - 狀態(tài) - 已連接:\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"]; // 設(shè)備名稱
[dict setValue:@"active" forKey:@"state"]; // 連接狀態(tài)(激活: active & 未激活: unactivated)
} else { // 未連接
[dict setValue:@"none" forKey:@"state"];
}
return dict;
}
#pragma mark - ****************************** 藍(lán)牙耳機是否鏈接 - 實體
+ (AVAudioSessionPortDescription *)isBluetoothHeadsetConnectedEntity {
/*
藍(lán)牙開啟未鏈接耳機:
Speaker
單向保真音頻協(xié)議(輸出):
BluetoothA2DPOutput
...
雙向保真音頻協(xié)議(輸入 & 輸入):
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); // 藍(lán)牙開啟未鏈接: Speaker && 鏈接耳機: BluetoothA2DPOutput BluetoothHFP BluetoothHSP
if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"]) {
NSLog(@"[藍(lán)牙檢測] - 狀態(tài) - 輸出 - %@", [portDescription portName]);
return portDescription;
} else if (portType && [portType isEqualToString:@"BluetoothHFP"] && [portType isEqualToString:@"BluetoothHSP"]) {
NSLog(@"[藍(lán)牙檢測] - 狀態(tài) - 輸出 & 輸入 - %@", [portDescription portName]);
return portDescription;
}
}
}
return nil;
}
以上便是此次分享的全部內(nèi)容街氢,希望能對大家有所幫助!