2017-07-14 94 views
0

我想让两个运行在不同设备上的程序通过蓝牙与CoreBluetooth进行通信。我可以找到并连接来自经理的外围设备,并且可以浏览连接的外围设备中的服务,但是当我尝试并尝试发现特征时,出现错误The specified UUID is not allowed for this operation.,并且如预期的那样,该服务的特征为零。UUID不允许在外设didDiscoverCharacteristicsfor服务

这是什么意思?我试图通过指定目标的UUID而没有发现特征,都显示此错误。

这是打印错误的功能。

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 
    print(error.localizedDescription)//prints "The specified UUID is not allowed for this operation." 
    if service.characteristics != nil { 
     for characteristic in service.characteristics! { 
      if characteristic.uuid == CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1") { 
       print("characteristic found") 
       peripheral.readValue(for: characteristic) 
      } 
     } 
    } 
} 

这是我寻找外围设备的地方。

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    if peripheral.services != nil { 
     for service in peripheral.services! { 
      if service.uuid == CBUUID(string: "dc495108-adce-4915-942d-bfc19cea923f") { 
       peripheral.discoverCharacteristics(nil, for: service) 
      } 
     } 
    } 
} 

这是我如何在其他设备上添加服务特性。

service = CBMutableService(type: CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f"), primary: true) 
characteristic = CBMutableCharacteristic(type: CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1"), properties: .write, value: nil, permissions: .writeable) 
service.characteristics = [characteristic] 

我尝试了许多不同的属性和权限组合(包括.read/.readable),我得到了同样的错误。

+0

您试图读取您已设置为只写因此当您尝试获得错误的特性并阅读它。 – Paulw11

+0

有道理,但我如何使它可读写?我也将属性更改为.read和.read权限,并且它不会更改任何内容。 – C1FR1

回答

0

您正试图读取您设置为只写的特性的值,因此Core Bluetooth会给出错误;对的读操作对于指定的特性无效。

如果你希望你的特点是可读可写的,你需要指定此:

service = CBMutableService(type: CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f"), primary: true) 
let characteristic = CBMutableCharacteristic(type: CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1"), properties: [.write, .read], value: nil, permissions: [.writeable, .readable]) 
service.characteristics = [characteristic] 
+0

我已经试过这个,没有帮助。 – C1FR1

+0

你从哪里得到错误打印?我刚刚创建了一个使用你的代码的测试应用程序,它工作正常。这里是我的两个视图控制器 - 第一个是中心,第二个是外围设备https://gist.github.com/paulw11/5d734957ddd575f9d00c758926984e76 – Paulw11

+0

错误来自'didDiscoverCharacteristicsFor服务'功能,我将编辑问题以显示哪里。我查看了您的测试应用程序,并且使用了另一种搜索外设的方式(可能还有更好的方法),所以我将其复制并解决了问题。一旦连接,中央运行'didDiscoverCharacteristicsFor'函数几次,有时会有错误,如果没有,特性将显示为零。那么它会再运行一次,并在for语句中崩溃,说'NSArray元素无法匹配Swift数组元素类型。 – C1FR1