【问题标题】:Ionic/Cordova and Arduino bluetoothIonic/Cordova 和 Arduino 蓝牙
【发布时间】:2015-11-30 01:21:55
【问题描述】:

我正在使用 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)

【问题讨论】:

  • 你的手机和你的arduino配对了吗?
  • 不是通过iPhone上的设置,因为手机无法检测到应用程序外的蓝牙,我认为当我在应用程序内按“连接”时,它会与设备配对,并且小红灯蓝牙停止闪烁。
  • 在应用程序上点击连接后,我会获得有关蓝牙模块的信息,例如 Uuid 和特征 - 这意味着设备已配对正确吗?

标签: cordova ionic-framework bluetooth arduino hm-10


【解决方案1】:

您必须在发送和读取数据之前发现服务和特征。使用 bluetoothle.discover

【讨论】:

  • 我正在使用示例应用程序:github.com/randdusing/BluetoothLE/tree/master/example/www 所以我打开了应用程序并做了:初始化 - 开始扫描 - 设备名称 - 连接 - 发现(你是对的) - ffe0 - ffe1 现在我有了两个描述符“uuid”:2901 和 2902,尝试写入两者,但没有任何反应...
  • 只是为了检查一下,我想要的只是在 arduino 和带蓝牙的计算机之间进行串行通信,我对此有点困惑......
  • 您的 Javascript 似乎正确。我认为问题可能来自您的 arduino 代码。你有没有做正确的设置。您确定您与特征的通信必须通过串行通信吗?你的 arduino 使用的是什么 BLE 芯片?
  • 我正在使用 HM10 芯片,我刚刚尝试了这个应用程序:itunes.apple.com/us/app/hm10-bluetooth-serial/id1030454675?mt=8,它运行良好,只需将设置更改为“无响应写入” - 我不确定什么样的写入我在我的javascript中使用,这可能是问题,因为“写响应”将不起作用 - 只是尝试从“$cordovaBluetoothLE.write(params).then(writeSuccess,写错误);"没有运气... - 也从 write 函数中删除了相同的代码.. 也没有运气...
  • 您正在使用 HM-10 和 arduino。看来您没有正确设置蓝牙。根据这个[链接](bellcode.wordpress.com/2012/01/02/…)。您可以启动蓝牙进行串行通信。
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2018-05-10
相关资源
最近更新 更多