[设计过程分享] 体感手套 #4 简单蓝牙连接手机程序测试通过

北方   2017-9-21 13:37 楼主
1. 在这个连接的过程,需要使用控制手机VR的连接方式,所以需要连接手机。这里采用cordova plugin的方式编程,测试简单串口连接OK。 具体硬件是和上贴一样,计算机USB-ttl连接HC-06模块。 2. 其中要先定义mac的数值,并通过扫描获得MAC地址,这样在后面可以选择正确的模块连接,如果模块多,就先扫描,然后把获得的MAC赋值后连接 var app = { macAddress: "98:D3:32:30:8F:C4", // get your mac address from bluetoothSerial.list chars: "", } 连接成功的截图如下: Screenshot_2017-09-21-12-23-45.png 3. 事实上,这样的连接其实并不可靠,不是每次都可以顺利连接,需要多测试几次才能够。不过这个是蓝牙模块的性能问题,连接测试功能是通过了 4. 软件如下,
  1. var app = {
  2. macAddress: "98:D3:32:30:8F:C4", // get your mac address from bluetoothSerial.list
  3. chars: "", //macAddress: "98:D3:32:10:97:2C",macAddress: "98:D3:32:30:8F:C4",
  4. /*
  5. Application constructor
  6. */
  7. initialize: function() {
  8. this.bindEvents();
  9. console.log("Starting SimpleSerial app");
  10. },
  11. /*
  12. bind any events that are required on startup to listeners:
  13. */
  14. bindEvents: function() {
  15. document.addEventListener('deviceready', this.onDeviceReady, false);
  16. connectButton.addEventListener('touchend', app.manageConnection, false);
  17. },
  18. /*
  19. this runs when the device is ready for user interaction:
  20. */
  21. onDeviceReady: function() {
  22. // check to see if Bluetooth is turned on.
  23. // this function is called only
  24. //if isEnabled(), below, returns success:
  25. var listPorts = function() {
  26. // list the available BT ports:
  27. bluetoothSerial.list(
  28. function(results) {
  29. app.display(JSON.stringify(results));
  30. },
  31. function(error) {
  32. app.display(JSON.stringify(error));
  33. }
  34. );
  35. }
  36. // if isEnabled returns failure, this function is called:
  37. var notEnabled = function() {
  38. app.display("Bluetooth is not enabled.")
  39. }
  40. // check if Bluetooth is on:
  41. bluetoothSerial.isEnabled(
  42. listPorts,
  43. notEnabled
  44. );
  45. },
  46. /*
  47. Connects if not connected, and disconnects if connected:
  48. */
  49. manageConnection: function() {
  50. // connect() will get called only if isConnected() (below)
  51. // returns failure. In other words, if not connected, then connect:
  52. var connect = function () {
  53. // if not connected, do this:
  54. // clear the screen and display an attempt to connect
  55. app.clear();
  56. app.display("Attempting to connect. " +
  57. "Make sure the serial port is open on the target device.");
  58. // attempt to connect:
  59. bluetoothSerial.connect(
  60. app.macAddress, // device to connect to
  61. app.openPort, // start listening if you succeed
  62. app.showError // show the error if you fail
  63. );
  64. };
  65. // disconnect() will get called only if isConnected() (below)
  66. // returns success In other words, if connected, then disconnect:
  67. var disconnect = function () {
  68. app.display("attempting to disconnect");
  69. // if connected, do this:
  70. bluetoothSerial.disconnect(
  71. app.closePort, // stop listening to the port
  72. app.showError // show the error if you fail
  73. );
  74. };
  75. // here's the real action of the manageConnection function:
  76. bluetoothSerial.isConnected(disconnect, connect);
  77. },
  78. /*
  79. subscribes to a Bluetooth serial listener for newline
  80. and changes the button:
  81. */
  82. openPort: function() {
  83. // if you get a good Bluetooth serial connection:
  84. app.display("Connected to: " + app.macAddress);
  85. // change the button's name:
  86. connectButton.innerHTML = "Disconnect";
  87. // set up a listener to listen for newlines
  88. // and display any new data that's come in since
  89. // the last newline:
  90. bluetoothSerial.subscribe('\n', function (data) {
  91. app.clear();
  92. app.display(data);
  93. });
  94. },
  95. /*
  96. unsubscribes from any Bluetooth serial listener and changes the button:
  97. */
  98. closePort: function() {
  99. // if you get a good Bluetooth serial connection:
  100. app.display("Disconnected from: " + app.macAddress);
  101. // change the button's name:
  102. connectButton.innerHTML = "Connect";
  103. // unsubscribe from listening:
  104. bluetoothSerial.unsubscribe(
  105. function (data) {
  106. app.display(data);
  107. },
  108. app.showError
  109. );
  110. },
  111. /*
  112. appends [url=home.php?mod=space&uid=40873]@error[/url] to the message div:
  113. */
  114. showError: function(error) {
  115. app.display(error);
  116. },
  117. /*
  118. appends [url=home.php?mod=space&uid=146195]@message[/url] to the message div:
  119. */
  120. display: function(message) {
  121. var display = document.getElementById("message"), // the message div
  122. lineBreak = document.createElement("br"), // a line break
  123. label = document.createTextNode(message); // create the label
  124. display.appendChild(lineBreak); // add a line break
  125. display.appendChild(label); // add the message node
  126. },
  127. /*
  128. clears the message div:
  129. */
  130. clear: function() {
  131. var display = document.getElementById("message");
  132. display.innerHTML = "";
  133. }
  134. }; // end of app
本帖最后由 北方 于 2017-9-21 13:39 编辑

回复评论 (1)

汇总贴在此:
体感手套—by 北方
https://bbs.eeworld.com.cn/thread-564975-1-1.html
玩板看这里: https://bbs.eeworld.com.cn/elecplay.html EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!
点赞  2017-9-21 15:51
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复