我已经尝试过多次将数据从iPhone发送到hm-10模块,但是我无法弄清楚我的快速代码是否错误,或者是我的arduino代码不正确,这是我的快速代码,因为我认为这是有问题的代码:
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else { return }
for characteristic in characteristics {
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
mainCharacteristic = characteristic
mainCentralManager.stopScan()
updateUI()
guard let peripheral = mainPeripheral, let characteristic = mainCharacteristic else { print("problem")
return }
var data1 = 5
var data2 = 7
var data = Data(buffer: UnsafeBufferPointer(start: &data1, count: 1))
data.append(Data(buffer: UnsafeBufferPointer(start: &data2, count: 1)))
peripheral.writeValue(data, for: characteristic, type: .withoutResponse)
print("data sent")
break
}
}
}
这是我的arduino代码,以防万一:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9);
void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
delay(100);
}
void loop()
{
if (mySerial.available()>0){
Serial.print(mySerial.read());
Serial.print("\n");
}
}
谢谢