检测已连接的蓝牙设备 - iOS

检测蓝牙已连接 的情况下更加具体的获取一些当前连接设备的信息数据,具体实现方法如下:

#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;
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 可以通过 Core Bluetooth 框架来连接蓝牙设备。以下是连接蓝牙设备的基本步骤: 1. 导入 Core Bluetooth 框架。 2. 创建一个 Central Manager 对象,并实现它的代理方法。 3. 扫描周围的蓝牙设备,获取设备的 UUID。 4. 连接指定的蓝牙设备。 5. 执行设备的服务和特性,以便与设备通信。 以下是一个简单的示例代码: ``` import CoreBluetooth class ViewController: UIViewController, CBCentralManagerDelegate { var centralManager: CBCentralManager! override func viewDidLoad() { super.viewDidLoad() centralManager = CBCentralManager(delegate: self, queue: nil) } func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == .poweredOn { // 开始扫描蓝牙设备 centralManager.scanForPeripherals(withServices: nil, options: nil) } else { print("蓝牙未打开") } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { // 获取设备的 UUID,连接指定的蓝牙设备 if peripheral.identifier.uuidString == "指定设备的UUID" { centralManager.connect(peripheral, options: nil) } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { // 连接成功后,执行设备的服务和特性 peripheral.delegate = self peripheral.discoverServices(nil) } } extension ViewController: CBPeripheralDelegate { func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { if let services = peripheral.services { for service in services { peripheral.discoverCharacteristics(nil, for: service) } } } func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { if let characteristics = service.characteristics { for characteristic in characteristics { // 与设备进行读写操作 } } } } ``` 当然,具体的实现还需要根据你的蓝牙设备来进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值