iOS Swift 低功耗蓝牙 : Discovering Multiple Characteristics within a Single Service

标签 ios swift mobile bluetooth

所以我正在关注来自 http://www.raywenderlich.com/85900/arduino-tutorial-integrating-bluetooth-le-ios-swift 的这个 iOS 应用程序代码示例

下面的代码取自上面的链接,向服务注册了一个特征。您将如何为一项服务注册多个特征,以便可以使用 Swift 读取和写入它们?

/* Services & Characteristics UUIDs */
let BLEServiceUUID = CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let PositionCharUUID = CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")
let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"
.
.
.
.
.
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
  let uuidsForBTService: [CBUUID] = [PositionCharUUID]

  if (peripheral != self.peripheral) {
    // Wrong Peripheral
    return
  }

  if (error != nil) {
    return
  }

  if ((peripheral.services == nil) || (peripheral.services.count == 0)) {
    // No Services
    return
  }

  for service in peripheral.services {
    if service.UUID == BLEServiceUUID {
      peripheral.discoverCharacteristics(uuidsForBTService, forService: service as CBService)
    }
  }
}

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
  if (peripheral != self.peripheral) {
    // Wrong Peripheral
    return
  }

  if (error != nil) {
    return
  }

  for characteristic in service.characteristics {
    if characteristic.UUID == PositionCharUUID {
      self.positionCharacteristic = (characteristic as CBCharacteristic)
      peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)

      // Send notification that Bluetooth is connected and all required characteristics are discovered
      self.sendBTServiceNotificationWithIsBluetoothConnected(true)
    }
  }

提前致谢!

最佳答案

我认为这就像在 for 循环中添加更多 if 语句一样简单。像这样:

let characteristicOneUUID= CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let characteristicTwoUUID= CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")

for characteristic in service.characteristics {
    //Characteristic One
    if characteristic.UUID == characteristicOneUUID{
      peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
    }
     //Characteristic Two
    if characteristic.UUID == characteristicTwoUUID{
      peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
    }
  }

关于iOS Swift 低功耗蓝牙 : Discovering Multiple Characteristics within a Single Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786181/

相关文章:

iphone - toSharedViewController 不重用现有的 Controller

ios - KIF 测试包 XCTest.dylib 在使用 Swift 时未加载

java - 更改设备方向时,在 onCreate() 中更改 EditText 的值无效

objective-c - 无法访问 Pod 内的 Pod lib 依赖项,只能在客户端 App 内访问

javascript - XHTML Mobile 1.0 - W3C 验证因脚本而无效

android - 检查android中的噪音/声音

ios - Swift如何改变backIndicatorImage的tintColor

ios - 如何使用通向另一个表的表来为应用程序实现 MVC

ios - UIManagedDocument openWithCompletionHandler返回否

ios - 除非通过 xcode 启动,否则应用程序在加载屏幕后崩溃