简体   繁体   English

在iOS Swift中写入BLE接收器

[英]Write to BLE Receiver in iOS Swift

I'm new to iOS and BLE. 我是iOS和BLE的新手。 I've been following a tutorial to connect and send data to a BLE receiver board on an Arduino. 我一直在关注连接并将数据发送到Arduino上的BLE接收器板的教程。 The tutorial was designed for a different board, and I'm using the (the BLE board is the Adafruit nrf8001 if that might help). 本教程是为不同的主板设计的,我正在使用(如果可能有帮助,BLE主板是Adafruit nrf8001)。

The following code sets up the UUIDs... 以下代码设置UUID ...

let BLEServiceUUID = CBUUID(string: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E") let PositionCharUUID = CBUUID(string: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E") let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"

And this code connects my iOS device to the BLE board. 此代码将我的iOS设备连接到BLE板。

central.connectPeripheral(peripheral, options: nil)

I then have a slider set up in my ViewController, and I want to send the value of that slider to the BLE board. 然后我在ViewController中设置了一个滑块,我想将该滑块的值发送到BLE板。 Here's my writePosition function: 这是我的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 }

Note that both my arduino AND my app confirm that they are indeed connected. 请注意,我的arduino和我的应用程序都确认它们确实已连接。 However, if I change the slider value in the app, it does not send its value to the BLE board. 但是,如果我更改应用程序中的滑块值,它不会将其值发送到BLE板。 What am I doing wrong?? 我究竟做错了什么??

Thanks in advance! 提前致谢!

Please note that writing data to a BLE can not be done (typically), faster than 35 to 50 ms. 请注意,无法(通常)将数据写入BLE,速度超过35到50 ms。 It's a good idea to use a timer (which in iOS has a minimum tick between 50 to 100 ms) to write to the charactestistic, instead of writing directly from your slider. 最好使用计时器(在iOS中最小刻度在50到100毫秒之间)写入字符,而不是直接从滑块写入。 So the timer polls the slider's value and writes it. 因此,计时器轮询滑块的值并写入它。 More advanced apps should use a tx queue. 更高级的应用程序应使用tx队列。

Also, you may want to take a look to this answer: SWIFT - BLE communications 此外,您可能想看看这个答案: SWIFT - BLE通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM