繁体   English   中英

将数据发送到蓝牙模块以供arduino rom iOS swift 3读取

[英]sending data to bluetooth module to be read by arduino rom iOS swift 3

如果我想将数据发送到已连接至Arduino的蓝牙模块,则需要特别注意哪些代码行。

我想向蓝牙模块发送数字“ 75”,Arduino会读取它

谢谢

蓝牙LE很长而且很麻烦,介于两者之间。 写入数据的最小路径是:

  1. 确保您具有蓝牙许可:CBCentralManagerDelegate.centralManagerDidUpdateState,如果是,则开始使用scanForPeripherals进行扫描
  2. CBCentralManagerDelegate.didDiscover如果这是您想要的外围设备,则将自己设置为其代理
  3. CBPeripheralDelegate.peripheral:didDiscoverServices:如果这是您想要的服务,请停止扫描并发现功能:
  4. CBPeripheralDelegate.peripheral:didDiscoverCharacteristicsFor:service如果特性数组中的一个特性是您想要的,则:

     func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { guard let characteristics = service.characteristics else { return } for characteristic in characteristics { if characteristic.uuid == CBUUID(string: characteristicIdentifier) { let value: UInt8 = 75 let data = Data(bytes: [value]) peripheral.writeValue(data, for: characteristic, type: .withResponse) } } } 

暂无
暂无

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

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