2016-08-24 3 views
0

나는 BLE hm-10 장치에 ios 응용 프로그램을 연결하고 그 특성을 문자열로 전송했습니다. 그러나 @IBAction 함수를 사용하여 단추를 누르면 문자열을 보내는 방법을 알아낼 수 없습니다. I 그것이 작동하지 않는 아래쪽에 도시 @IBAction 기능이 코드를 넣어버튼에 대한 데이터 쓰기 - 클릭 Swift

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?) 
{ 
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)") 
    _peripheral = peripheral 
    _characteristics = service.characteristics 
    let string = "off" 

    let data = string.dataUsingEncoding(NSUTF8StringEncoding) 


    for characteristic in service.characteristics as [CBCharacteristic]! 
    { 

     if(characteristic.UUID.UUIDString == "FFE1") 
     { 
      print("sending data") 
      peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse) 
     } 
    } 

} 

@IBAction func onButtonTouch(sender: UIButton) 
{ 
    let string = "on" 

    let data = string.dataUsingEncoding(NSUTF8StringEncoding) 

    _peripheral.writeValue(data!, forCharacteristic: _characteristics, type: CBCharacteristicWriteType.WithoutResponse) 

} 

"특성"미해결 식별자이다. 주변 장치 기능에 연결하는 방법이 있습니까? 나는 Swift에서 코딩하기가 비교적 쉽다. 그래서 어떤 도움이 정말 감사 할 것이다. 감사.

+0

특성을 저장해야합니다. 'onButtonTouch (보낸 사람 :)'에 선언되지 않았습니다! – Larme

+0

주변 변수로'peripheral'와'characteristic'을 만들 수없는 이유는 무엇입니까? – D4ttatraya

+0

@Larme 빠른 답변 감사드립니다. 그런 초보자처럼 들리 겠지만 정말이 일을 어떻게 설명 할 수 있겠습니까? 고맙습니다! – DanB

답변

0
class viewController: UIViewController { 

    var _peripheral: CBPeripheral? 
    var _characteristics: [CBCharacteristic]? 

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error:NSError?) 
    { 
    print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)") 
    _peripheral = peripheral 
    _characteristics = service.characteristics 
    let string = "off" 

    let data = string.dataUsingEncoding(NSUTF8StringEncoding) 


     for characteristic in service.characteristics as [CBCharacteristic]! 
     { 

      if(characteristic.UUID.UUIDString == "FFE1") 
      { 
       print("sending data") 
       peripheral.writeValue(data!, forCharacteristic: characteristic,type: CBCharacteristicWriteType.WithoutResponse) 
      } 
     } 

    } 

    @IBAction func onButtonTouch(sender: UIButton) 
    { 
    let string = "on" 

    let data = string.dataUsingEncoding(NSUTF8StringEncoding) 

    let characteristic = //get right on out of _characteristics! array 
    _peripheral!.writeValue(data!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse) 

    } 
} 
+0

이렇게하면 고유 식별자 문제가 해결됩니다. 'viewController 클래스에 initializers가 없습니다.'라는 viewController와 CharacteristicsDataType/Class에 관한 오류가 발생합니다. "한 줄에 연속적으로 선언해야한다." 고통 스럽군요. – DanB

+0

@ DanB 어떤 오류가 있습니까? 'view controller' 예를 들어 언급 한 바 있습니다. 코드가 포함 된 전체 클래스를 보여줍니다. – D4ttatraya

+0

@DanB 당신은'CharacteristicsDataType/Class'를'[CBCharacteristic]'의 실제 데이터 타입으로 대체해야합니다. 업데이트 된 답변 확인 – D4ttatraya

관련 문제