I am following a PhoneGap bluetooth tutorial and it works wonderfully, except that app.initialize() is only called upon the very first app instantiation after intall. (I am currently testing only on Andriod). If I exit the app and restart it, .initialize() is NOT called; I must uninstall/reinstall the app. Obviously I don't want a user uninstalling/reinstalling the app to achieve init. Why will init not work except upon the first run after install? I do not believe this problem has anything to do with Bluetooth or serial functionality specifically, but for completeness I have included the JS file below:
/*
SimpleSerial index.js
Created 7 May 2013
Modified 9 May 2013
by Tom Igoe
*/
var PARTNER_NAME = 'HC-06'; // look for this Bluetooth partner name
var count = 0;
function findPartner(results) {
for(var i=0; i<results.length; i++) {
if(results[i].name == PARTNER_NAME) {
count++;
app.macAddress = results[i].address; // get the MAC address
}
}
if(count == 0) {
alert('1')
}
else if(count > 1) {
alert('2')
}
else {
alert('3');
}
}
var app = {
macAddress: "AA:BB:CC:DD:EE:FF", // get your mac address from bluetoothSerial.list
chars: "",
/*
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) {
findPartner(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 @error to the message div:
*/
showError: function(error) {
app.display(error);
},
/*
appends @message 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