单击按钮时将数据写入特征 - Swift
Write Data to Characteristic on Button Click - Swift
我已成功将 ios 应用程序连接到我的 BLE hm-10 设备并向其发送一个字符串特征。但是我不知道如何在使用@IBAction 函数按下按钮后发送字符串。
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)
}
当我将此代码放入底部显示的 @IBAction 函数时,它不起作用,因为 "characteristic" 是一个未解析的标识符。有没有办法linkthis到外围功能?我对 Swift 中的编码比较陌生,因此非常感谢任何帮助。谢谢
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)
}
}
我已成功将 ios 应用程序连接到我的 BLE hm-10 设备并向其发送一个字符串特征。但是我不知道如何在使用@IBAction 函数按下按钮后发送字符串。
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)
}
当我将此代码放入底部显示的 @IBAction 函数时,它不起作用,因为 "characteristic" 是一个未解析的标识符。有没有办法linkthis到外围功能?我对 Swift 中的编码比较陌生,因此非常感谢任何帮助。谢谢
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)
}
}