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: "",
}
连接成功的截图如下:
3. 事实上,这样的连接其实并不可靠,不是每次都可以顺利连接,需要多测试几次才能够。不过这个是蓝牙模块的性能问题,连接测试功能是通过了
4. 软件如下,
-
- var app = {
- macAddress: "98:D3:32:30:8F:C4", // get your mac address from bluetoothSerial.list
- chars: "", //macAddress: "98:D3:32:10:97:2C",macAddress: "98:D3:32:30:8F:C4",
-
- /*
- Application constructor
- */
- initialize: function() {
- this.bindEvents();
- console.log("Starting SimpleSerial app");
- },
- /*
- bind any events that are required on startup to listeners:
- */
- bindEvents: function() {
- document.addEventListener('deviceready', this.onDeviceReady, false);
- connectButton.addEventListener('touchend', app.manageConnection, false);
- },
-
- /*
- this runs when the device is ready for user interaction:
- */
- onDeviceReady: function() {
- // check to see if Bluetooth is turned on.
- // this function is called only
- //if isEnabled(), below, returns success:
- var listPorts = function() {
- // list the available BT ports:
- bluetoothSerial.list(
- function(results) {
- app.display(JSON.stringify(results));
- },
- function(error) {
- app.display(JSON.stringify(error));
- }
- );
- }
-
- // if isEnabled returns failure, this function is called:
- var notEnabled = function() {
- app.display("Bluetooth is not enabled.")
- }
-
- // check if Bluetooth is on:
- bluetoothSerial.isEnabled(
- listPorts,
- notEnabled
- );
- },
- /*
- Connects if not connected, and disconnects if connected:
- */
- manageConnection: function() {
-
- // connect() will get called only if isConnected() (below)
- // returns failure. In other words, if not connected, then connect:
- var connect = function () {
- // if not connected, do this:
- // clear the screen and display an attempt to connect
- app.clear();
- app.display("Attempting to connect. " +
- "Make sure the serial port is open on the target device.");
- // attempt to connect:
- bluetoothSerial.connect(
- app.macAddress, // device to connect to
- app.openPort, // start listening if you succeed
- app.showError // show the error if you fail
- );
- };
-
- // disconnect() will get called only if isConnected() (below)
- // returns success In other words, if connected, then disconnect:
- var disconnect = function () {
- app.display("attempting to disconnect");
- // if connected, do this:
- bluetoothSerial.disconnect(
- app.closePort, // stop listening to the port
- app.showError // show the error if you fail
- );
- };
-
- // here's the real action of the manageConnection function:
- bluetoothSerial.isConnected(disconnect, connect);
- },
- /*
- subscribes to a Bluetooth serial listener for newline
- and changes the button:
- */
- openPort: function() {
- // if you get a good Bluetooth serial connection:
- app.display("Connected to: " + app.macAddress);
- // change the button's name:
- connectButton.innerHTML = "Disconnect";
- // set up a listener to listen for newlines
- // and display any new data that's come in since
- // the last newline:
- bluetoothSerial.subscribe('\n', function (data) {
- app.clear();
- app.display(data);
- });
- },
-
- /*
- unsubscribes from any Bluetooth serial listener and changes the button:
- */
- closePort: function() {
- // if you get a good Bluetooth serial connection:
- app.display("Disconnected from: " + app.macAddress);
- // change the button's name:
- connectButton.innerHTML = "Connect";
- // unsubscribe from listening:
- bluetoothSerial.unsubscribe(
- function (data) {
- app.display(data);
- },
- app.showError
- );
- },
- /*
- appends [url=home.php?mod=space&uid=40873]@error[/url] to the message div:
- */
- showError: function(error) {
- app.display(error);
- },
-
- /*
- appends [url=home.php?mod=space&uid=146195]@message[/url] to the message div:
- */
- display: function(message) {
- var display = document.getElementById("message"), // the message div
- lineBreak = document.createElement("br"), // a line break
- label = document.createTextNode(message); // create the label
-
- display.appendChild(lineBreak); // add a line break
- display.appendChild(label); // add the message node
- },
- /*
- clears the message div:
- */
- clear: function() {
- var display = document.getElementById("message");
- display.innerHTML = "";
- }
- }; // end of app
-
本帖最后由 北方 于 2017-9-21 13:39 编辑