哪个是快速BLE获得CBServices和CBC特性的最佳方法?

时间:2017-06-21 13:05:34

标签: ios swift3 bluetooth-lowenergy

有时候连接时cbcharacteristic值为null。请帮助解决swift 3中的这类问题。最佳结构安排所有服务及其特点。

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
{ 
    if let er = error{
        print(er)
        return
    }

    if let services = peripheral.services as [CBService]!{
        for service in services{

            peripheral.discoverCharacteristics(nil, for: service)
        }
    }
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?){

    if let arraychar = service.characteristics as [CBCharacteristic]!{

        for getCharacteristic in arraychar{    
        arrayValue.append(String(data: dd, encoding: String.Encoding.ascii)!)
            }
        }

1 个答案:

答案 0 :(得分:0)

   func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    self.dictSerivesCharacteistic.setObject(service.characteristics!, forKey: service.uuid.uuidString as NSCopying)


    for newChar: CBCharacteristic in service.characteristics!{

        peripheral.readValue(for: newChar)

        if newChar.properties.rawValue == 0x10 || newChar.properties.rawValue == 0x8C{

            self.arrayReadWriteChar.append(newChar)

            peripheral.setNotifyValue(true, for: newChar)
        }
        else if newChar.properties.rawValue == 0x12{

            peripheral.setNotifyValue(true, for: newChar)
        }
    }