我正在使用 Ionic 构建一个应用程序,并且我正在尝试使用bluetoothLE从 arduino 向我的应用程序发送一个简单的字符串。
我正在使用带有 HM-10 蓝牙适配器的 Arduino,我设法连接和断开设备,但无法理解如何发送或接收数据。
只是为了测试它,我一直在尝试 write 方法,并在 arduino 上有一个简单的脚本,通过串行通信寻找任何类型的数据,但没有成功。
这是我正在使用的方法:
$rootScope.writeH =function(address, serviceUuid, characteristicUuid, value) {
var string = "H";
var bytes = bluetoothle.stringToBytes(string);
var encodedString = bluetoothle.bytesToEncodedString(encodedString);
var params = {address:address, serviceUuid:serviceUuid, characteristicUuid:characteristicUuid, value:encodedString};
console.log("Write : " + JSON.stringify(params));
$cordovaBluetoothLE.write(params).then(writeSuccess, writeError);
}
这就是我调用函数的方式:
<div class="col">
<a class="button button-balanced button-small" ng-click="writeH(selectedDevice.address, selectedService.uuid, selectedCharacteristic.uuid)">Write H</a>
</div>
我正在使用运行 iOS 9 的 iPhone 6。
这是我的 Arduino 草图,此时我只是想获取任何数据:
char val; // variable to receive data from the serial port
int ledpin = 8; // LED connected to pin 48 (on-board LED)
void setup() {
pinMode(ledpin, OUTPUT); // pin 8 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 9600bps
}
void loop() {
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
if( val == NULL ) // if any NO data was received
{
digitalWrite(ledpin, LOW); // LED is OFF
} else {
digitalWrite(ledpin, HIGH); // otherwise turn it ON
delay(2000);
}
delay(100); // wait 100ms for next reading
}
编辑:
我正在使用 HM10 芯片,我刚刚尝试了这个应用程序:http: //itunes.apple.com/us/app/hm10-bluetooth-serial/id1030454675 ?mt=8它运行良好,只需要更改设置“写而不响应” - 我不确定我在我的 javascript 中使用什么样的写,这可能是问题,因为“写有响应”将不起作用。
编辑2:
刚刚从蓝牙cordova插件检查了iOS的源文件,写入类型设置为无响应,所以不是......
//Write Type
NSString *const writeTypeNoResponse = @"noResponse";
控制台日志:
[Log] Initialize : {"request":true} (index.js, line 58)
[Log] Initialize Success : {"status":"enabled"} (index.js, line 64)
[Log] Enabled (index.js, line 68)
[Log] Start Scan : {"serviceUuids":[],"allowDuplicates":false} (index.js, line 129)
[Log] Start Scan Success : {"status":"scanStarted"} (index.js, line 135)
[Log] Scan Started (index.js, line 145)
[Log] Start Scan Success : {"status":"scanResult","advertisement":"zgAG1R1JwPI=","rssi":-54,"name":"Avea_6C73","address":"4BED6ECD-A5A9-6625-61A6-D693B02EAC78"} (index.js, line 135)
[Log] Scan Result (index.js, line 139)
[Log] Start Scan Success : {"status":"scanResult","advertisement":"SE30uF7wPYo=","rssi":-82,"name":"HMSoft","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 135)
[Log] Scan Result (index.js, line 139)
[Log] Connect : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 302)
[Log] Connect Success : {"name":"HMSoft","address":"07929248-D56A-1FDB-9036-D012FBC4F10B","status":"connecting"} (index.js, line 308)
[Log] Connecting (index.js, line 316)
[Log] Connect Success : {"name":"HMSoft","address":"07929248-D56A-1FDB-9036-D012FBC4F10B","status":"connected"} (index.js, line 308)
[Log] Connected (index.js, line 312)
[Log] Discover : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 417)
[Log] Discover Success : {"status":"discovered","services":[{"characteristics":[{"descriptors":[{"descriptorUuid":"2902"},{"descriptorUuid":"2901"}],"properties":{"writeWithoutResponse":true,"read":true,"notify":true},"characteristicUuid":"ffe1"}],"serviceUuid":"ffe0"}],"name":"HMSoft","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 423)
[Log] Discovered (index.js, line 427)
[Log] Write Descriptor : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B","characteristicUuid":"ffe1","descriptorUuid":"2901","value":""} (index.js, line 879)
[Log] Read Descriptor Error : {"message":"Service not found","name":"HMSoft","error":"service","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 868)
[Log] Write Descriptor : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B","characteristicUuid":"ffe1","descriptorUuid":"2902","value":""} (index.js, line 879)
[Log] Read Descriptor Error : {"message":"Service not found","name":"HMSoft","error":"service","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 868)
[Log] Subscribe : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B","serviceUuid":"ffe0","characteristicUuid":"ffe1"} (index.js, line 757)
[Log] Subscribe Success : {"status":"subscribed","characteristicUuid":"ffe1","name":"HMSoft","serviceUuid":"ffe0","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 763)
[Log] Subscribed (index.js, line 771)
[Log] Write Descriptor : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B","characteristicUuid":"ffe1","descriptorUuid":"2901","value":""} (index.js, line 879)
[Log] Read Descriptor Error : {"message":"Service not found","name":"HMSoft","error":"service","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 868)
[Log] Write : {"address":"07929248-D56A-1FDB-9036-D012FBC4F10B","serviceUuid":"ffe0","characteristicUuid":"ffe1","value":""} (index.js, line 823)
[Log] Write Error : {"message":"Write value not found","name":"HMSoft","error":"write","address":"07929248-D56A-1FDB-9036-D012FBC4F10B"} (index.js, line 843)