将数据发送到蓝牙模块以供 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.centralManagerDidUpdateStateand 如果有,请使用 scanForPeripherals 开始扫描
  2. CBCentralManagerDelegate.didDiscover 如果这是你想要的外围设备,那么将你自己设置为它的委托
  3. CBPeripheralDelegate.peripheral:didDiscoverServices: 如果这是你想要的服务然后停止扫描 discoverCharacteristics:for: service
  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)
            }
        }
    }