使用核心蓝牙ios将值写入外设并从外设读取相同的值

时间:2014-10-20 05:51:08

标签: ios swift core-bluetooth

我是ios的新手。我已经经历了很多方法并且开始了解读取和写入的方法并且也实现了但是我无法获得读取操作期间写入方法写入的相同值。我希望在read方法中读取的消息在读取方法期间被读取是否可能?当我这样做时,我收到空白消息,没有找到消息。请帮我解决。谢谢。  的

  /* Write data*/
        if WriteID.isEqual(CBUUID.UUIDWithString("D0F0-0B047047"))

            {
                var str:NSString = "heyaa..!!"
                data = str.dataUsingEncoding(NSUTF8StringEncoding)!
                peripheral.writeValue(data, forCharacteristic: arrCharacteristics!.objectAtIndex(1) as CBCharacteristic , type: CBCharacteristicWriteType.WithResponse)

            }

 /* read data  */

            if ReadID.isEqual(CBUUID.UUIDWithString("C88-A248-C4AB1"))
            {
      peripheral.readValueForCharacteristic(arrCharacteristics!.objectAtIndex(0) as CBCharacteristic)

     }

// did write method:
func peripheral(peripheral: CBPeripheral!, didWriteValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
        if characteristic.UUID.isEqual(CBUUID.UUIDWithString("D0F0-0B047047"))

        {     
         var datastring = NSString(data: data!, encoding: NSUTF8StringEncoding)
        println(datastring)
        var alert:UIAlertView = UIAlertView(title: "Message", message: datastring , delegate: self, cancelButtonTitle: "OK")
        alert.show()

  }

    }
// did update method

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
if characteristic.UUID.isEqual(CBUUID.UUIDWithString("C88-A248-C4AB1""))
        {
            var str:NSString = NSString(data: characteristic.value, encoding: NSUTF8StringEncoding)
            println("bbbbb--- \(str)")
            var alert:UIAlertView = UIAlertView(title: "Message", message: str, delegate: self, cancelButtonTitle: "OK")
            alert.show()
            IBlblmessage!.text = str
        }
}

0 个答案:

没有答案