如何为BLE创建写入特性

问题描述 投票:0回答:1

嗨,我正在尝试为我的应用程序创建写入特性

我的特色创作代码就是这个

 // Start with the Read CBMutableCharacteristic
self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]
                                                                  properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyRead                                                                       value:nil
                                                                 permissions:CBAttributePermissionsReadable];

self.writeCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:WRITE_CHARACTERISTIC_UUID]
                                                                 properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyWriteWithoutResponse                                                                       value:nil
                                                                permissions:CBAttributePermissionsWriteable];
// Then the service
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]
                                                                    primary:YES];

// Add the characteristic to the service
transferService.characteristics = @[self.transferCharacteristic, self.writeCharacteristic];

// And add it to the peripheral manager
[self.peripheralManager addService:transferService];

我已经确认添加服务时存在两个特征。虽然当我扫描这个外围设备时它只显示一个特征。扫描码:

[self.centralManager scanForPeripheralsWithServices:nil
                                            options:nil];

我检查了各种链接,发现我的写作特征是正确的,任何人都可以指出我在这里做错了什么,为什么我的服务只显示一个特征?

注意:这是iPhone到iPhone的应用程序。 Lightblue应用程序显示两个具有准确属性的特征

编辑:

特征提取:

[peripheral discoverCharacteristics:nil forService:service];
ios objective-c bluetooth-lowenergy core-bluetooth cbcentralmanager
1个回答
0
投票

我可以快速给你一个代码来完成你的工作,你必须把它转换成目标c。

在我们将数据写入外设之前,我们需要知道如何编写数据。我们需要注意两种类型的CBC特性写入类型。 CBC特性写入类型可以是.withResponse或.withoutResponse。 .withResponse属性类型从外设获取响应以指示写入是否成功。 .withoutResponse不会从外围设备发回任何响应。要写入一个特性,我们需要用实例NSData写一个值,我们将通过调用writeValue(for:,type:CBCharacteristicWriteType.withResponse)方法来实现:

blePeripheral.writeValue(data!, for: txCharacteristic, type: CBCharacteristicWriteType.withResponse)

现在,您将能够使用可写属性写入特征。

 func writeValue(data: String){
  let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue)
    if let blePeripheral = blePeripheral{
       if let txCharacteristic = txCharacteristic {
          blePeripheral.writeValue(valueString!, for: txCharacteristic, type: CBCharacteristicWriteType.withResponse)
        }
    }
}

在上面的writeValue(data)函数中,我们将输出字符串格式化为NSData,然后检查是否设置了BLE外设和txCharacteristic变量。如果是,我们然后调用writeValue()函数,如前所述。因为我们使用CBCharacteristicWriteType.withResponse类型编写了值,所以我们可以通过在委托中实现以下函数来通知响应:

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
    guard error == nil else {
        print("Error discovering services: error")
      return
    }
     print("Message sent")
}
© www.soinside.com 2019 - 2024. All rights reserved.