如何改变使用核心蓝牙ios扫描外围设备时发现的特征顺序

时间:2014-10-18 06:08:11

标签: ios swift core-bluetooth

我在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,以便有任何解决方法。请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

特征将以任意和未定义的顺序发现,但这不重要。

您应该存储对已发现特征的引用,以便您可以根据需要写入它们。

你应用应该经历不同的阶段

  1. 扫描并发现广告特定服务的外围设备
  2. 连接目标外设
  3. 了解服务的特征
  4. 根据需要读取/写入数据 - 根据需要重复此步骤
  5. 断开
  6. 例如:

        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")
        }