![](/img/trans.png)
[英]iOS Core Bluetooth: Advertised service is not discovered on peripheral
[英]How to change order of charateristics ID discovered while scanning peripheral using core bleutooth ios
我在ios中使用核心藍牙框架並掃描並發現2個charateristics 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首先被調用,但是我想首先調用寫入數據特性ID,因此有什么方法可以解決。 請幫幫我。 先感謝您。
將以任意且未定義的順序發現特征,但這無關緊要。
您應該存儲對發現的特征的引用,以便可以根據需要對其進行寫入。
您的應用程序應經歷不同的階段
例如:
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.