我在ios中使用核心蓝牙框架并扫描和发现2个特征ID,但我希望他们的订单与我得到的不同,那么是否有可能以编程方式更改订单或任何内容?
代码
if (service.UUID.isEqual(CBUUID.UUIDWithString("F4F2-BC76-3206341A")))
{
println(service.characteristics.count)
for aChar:CBCharacteristic in service.characteristics as [CBCharacteristic]
{
println(aChar)
println(aChar.UUID)
/* Write data*/
if aChar.UUID.isEqual(CBUUID.UUIDWithString("D0F0AECD-6405-0B040047"))
{
var str:NSString = "heyaa..!!"
data = str.dataUsingEncoding(NSUTF8StringEncoding)!
peripheral.writeValue(data, forCharacteristic: aChar, type: CBCharacteristicWriteType.WithResponse)
println("Write performed")
}
/* read data */
if aChar.UUID.isEqual(CBUUID.UUIDWithString("C8853E-A248-C6F0B1"))
{
peripheral.readValueForCharacteristic(aChar)
println("Read performed")
}
}
}
在这个特性中,首先调用用于读取数据的ID,但我想首先调用写入数据特征ID,以便有任何解决方法。请帮帮我。提前谢谢。
答案 0 :(得分:0)
特征将以任意和未定义的顺序发现,但这不重要。
您应该存储对已发现特征的引用,以便您可以根据需要写入它们。
你应用应该经历不同的阶段
例如:
var writeCharacteristic : CBCharacteristic?
var readCharacteristic : CBCharacteristic?
optional func peripheral(_ peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!,error error: NSError!) {
if (service.UUID.isEqual(CBUUID.UUIDWithString("F4F2-BC76-3206341A")))
{
println(service.characteristics.count)
for aChar:CBCharacteristic in service.characteristics as [CBCharacteristic]
{
println(aChar)
println(aChar.UUID)
/* Write data*/
if aChar.UUID.isEqual(CBUUID.UUIDWithString("D0F0AECD-6405-0B040047"))
{
self.writeCharacteristic=aChar
}
else if aChar.UUID.isEqual(CBUUID.UUIDWithString("C8853E-A248-C6F0B1"))
{
self.readCharacteristic=aChar
}
}
}
if (self.writeCharacteristic != nil && self.readCharacteristic != nil) {
var str:NSString = "heyaa..!!"
data = str.dataUsingEncoding(NSUTF8StringEncoding)!
peripheral.writeValue(data, forCharacteristic: self.writeCharacteristic!, type: CBCharacteristicWriteType.WithResponse)
println("Write performed")
}
}
optional func peripheral(_ peripheral: CBPeripheral!,didWriteValueForCharacteristic characteristic: CBCharacteristic!,error error: NSError!) {
peripheral.readValueForCharacteristic(self.readCharacteristic!)
println("Read performed")
}