简体   繁体   English

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

[英]How to change order of charateristics ID discovered while scanning peripheral using core bleutooth ios

I am using core bluetooth framework in ios and scanning and discovering 2 charateristics ID but i want their order to be different then what I am getting so is it possible to change their order or anything programmatically? 我在ios中使用核心蓝牙框架并扫描并发现2个charateristics ID,但我希望它们的顺序与我所得到的不同,因此可以通过编程方式更改其顺序或其他内容吗?

code

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

In this characteristics ID for reading data is getting called first but i want to call write data characteristics ID first so is there any way to resolve. 在此特性ID中,用于读取数据的ID首先被调用,但是我想首先调用写入数据特性ID,因此有什么方法可以解决。 Please help me out. 请帮帮我。 Thank you in advance. 先感谢您。

The characteristics will be discovered in an arbitrary and undefined order, but that shouldn't matter. 将以任意且未定义的顺序发现特征,但这无关紧要。

You should store a reference to the discovered characteristics so that you can write to them as required. 您应该存储对发现的特征的引用,以便可以根据需要对其进行写入。

You app should go through distinct phases 您的应用程序应经历不同的阶段

  1. Scan for and discover peripherals advertising specific service(s) 扫描并发现宣传特定服务的外围设备
  2. Connect to the target peripheral 连接到目标外围设备
  3. Discover the characteristics of the service 发现服务的特征
  4. Read/Write data as required - repeat this step as required 根据需要读取/写入数据-根据需要重复此步骤
  5. Disconnect 断开

For example: 例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS Core Bluetooth:在外围设备上未发现广告服务 - iOS Core Bluetooth: Advertised service is not discovered on peripheral 在iOS中如何在不使用硬件地址进行扫描的情况下连接到外围设备 - In iOS how to connect to Peripheral without scanning using the harware address 如何使用Core Bluetooth在iOS中设置BLE外围设备的名称 - How to set the name of BLE peripheral in iOS using Core Bluetooth iOS 13 Core 蓝牙保存的外围服务在被发现后显示为零 - iOS 13 Core Bluetooth saved peripheral services showing nil after already being discovered 扫描提供特定服务的外围设备,但未在发现的外围设备上找到服务 - Scanning for Peripherals offering specific services, But doesn't find Services on discovered peripheral 我们可以使用蓝牙重新连接断开的外围设备而无需再次扫描ios - can we reconnect disconnected peripheral using bluetooth without scanning again ios CBCentralManager:如何确定管理器正在扫描哪个外围设备 - CBCentralManager: how to determine which peripheral is the manager scanning for 当后台或手机中的应用处于锁定状态时,iOS BLE外围设备扫描 - iOS BLE peripheral scanning when app in background or phone is in lock state 如何检测外围设备何时停止广告日期,以便我可以删除该外围设备表单已发现设备列表? - How to detect when a peripheral stops advertisement Date so that i can remove that peripheral form discovered device list? Xamarin iOS 蓝牙外设扫描永远看不到任何外设 - Xamarin iOS Bluetooth peripheral scanning never sees any peripherals
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM