我是iOS和BLE的新手。我一直在遵循一个教程,将数据连接并发送到Arduino上的BLE接收板。本教程是为不同的板设计的,我正在使用(BLE板是Adafruit nrf8001,如果这可能会有帮助的话)。
下面的代码设置UUID...
let BLEServiceUUID = CBUUID(string: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E") let PositionCharUUID = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E") let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"
这段代码将我的iOS设备连接到BLE板。
central.connectPeripheral(peripheral, options: nil)
然后,我在ViewController中设置了一个滑块,并希望将该滑块的值发送到BLE板。下面是我的writePosition函数:
func writePosition(position: UInt8)->NSData { let currentValue = NSUserDefaults.standardUserDefaults().floatForKey("sliderValue") var currentValueString=NSString(format: "%.5f", currentValue) var writeType:CBCharacteristicWriteType let newString:NSString = currentValueString let data = NSData(bytes: newString.UTF8String, length: newString.length) peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: writeType) return data }
请注意,我的arduino和我的应用程序都确认它们确实是连接的。但是,如果我在应用程序中更改滑块值,它不会将其值发送到BLE板。我做错了什么??
提前感谢!
发布于 2016-08-27 05:21:39
请注意,不能(通常)将数据写入BLE,速度超过35到50毫秒。最好使用计时器(在iOS中,计时器的最小刻度在50到100毫秒之间)来写入字符测试,而不是直接从滑块写入。因此,计时器轮询滑块的值并写入它。更高级的应用程序应该使用tx队列。
另外,您可能想看看这个答案:SWIFT - BLE communications
https://stackoverflow.com/questions/29292737
复制相似问题