Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中通过蓝牙传输音频和数据?_Ios_Swift_Audio_Avfoundation_Core Bluetooth - Fatal编程技术网

Ios 如何在Swift中通过蓝牙传输音频和数据?

Ios 如何在Swift中通过蓝牙传输音频和数据?,ios,swift,audio,avfoundation,core-bluetooth,Ios,Swift,Audio,Avfoundation,Core Bluetooth,我正在为iOS开发一个应用程序,它需要连接到蓝牙设备。我已经能够使用CoreBluetooth和GATT配置文件找到并连接到我附近的设备。特征的书写和阅读也起到了作用。这些值最终将在滑块移动时写入。但是,现在我还需要能够将音频流传输到蓝牙设备 当我环顾四周寻找通过蓝牙传输的音频时,我注意到这可以通过允许通过蓝牙输出的AVFoundation来实现。但是,设备必须使用A2DP配置文件。但这会导致应用程序无法读取/写入特征吗 我得到了这段代码来读取、通知和写入特定的特性 func periphe

我正在为iOS开发一个应用程序,它需要连接到蓝牙设备。我已经能够使用CoreBluetooth和GATT配置文件找到并连接到我附近的设备。特征的书写和阅读也起到了作用。这些值最终将在滑块移动时写入。但是,现在我还需要能够将音频流传输到蓝牙设备

当我环顾四周寻找通过蓝牙传输的音频时,我注意到这可以通过允许通过蓝牙输出的AVFoundation来实现。但是,设备必须使用A2DP配置文件。但这会导致应用程序无法读取/写入特征吗

我得到了这段代码来读取、通知和写入特定的特性

  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService,
                  error: Error?) {
    guard let characteristics = service.characteristics else { return }
    for characteristic in characteristics {
      print(characteristic)
      if characteristic.properties.contains(.read) {
        print("\(characteristic.uuid): properties contains .read")
        if characteristic.uuid == batteryLevelCharacteristicCBUUID { peripheral.readValue(for: characteristic) }
      }
      if characteristic.properties.contains(.notify) {
        print("\(characteristic.uuid): properties contains .notify")
        if characteristic.uuid == batteryLevelCharacteristicCBUUID {
          peripheral.setNotifyValue(true, for: characteristic)
        }
      }
      if characteristic.properties.contains(.write) {
        print("\(characteristic.uuid): properties contains .write")
        if characteristic.uuid == writeTestCharacteristicCBUUID {
          print("Writing characteristic found! Trying to write now.")
          let string = "Hello World!"
          let data = string.data(using: .utf8)!
          peripheral.writeValue(data, for: characteristic, type: CBCharacteristicWriteType.withResponse)
        }
      }
    }
  }
这个问题似乎很相似,但有人提到这是不可能的。 然而,我在想。如果你看一个耳机,例如当你按下按钮时,必须有一些数据传输。这能用在我想做的事情上吗?但我似乎找不到任何关于这些信号的信息,也找不到如何在代码中使用这些信号的信息

所以我想要的是能够流式传输音频,也能发送小的值,比如百分比


如果你知道任何可能对我有帮助的信息,请告诉我。提前谢谢

您的设备中需要一个能够同时支持BLE和传统A2DP模式的蓝牙芯片组。耳机的按键通过AVRCP传送profile@Paulw11没有别的办法吗?如果我能使用普通的蓝牙芯片而不是双芯片,我会更喜欢。如果没有其他方法,大多数iPhone可以连接到这样的双芯片吗?我相信所有支持BLE(iPhone 4和更高版本)的iPhone都应该能够连接到双芯片组,因为双模式支持是外围设备的功能,而不是外部设备的功能central@Paulw11好的,谢谢。我将开始朝这个方向看,然后您的设备中将需要一个能够同时支持BLE和传统A2DP模式的蓝牙芯片组。耳机的按键通过AVRCP传送profile@Paulw11没有别的办法吗?如果我能使用普通的蓝牙芯片而不是双芯片,我会更喜欢。如果没有其他方法,大多数iPhone可以连接到这样的双芯片吗?我相信所有支持BLE(iPhone 4和更高版本)的iPhone都应该能够连接到双芯片组,因为双模式支持是外围设备的功能,而不是外部设备的功能central@Paulw11好的,谢谢。那我就开始朝那个方向看