Skip to content

Commit

Permalink
Bug 1090799 - make nfc_handover v2 reuse system bluetooth adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
gasolin committed Mar 18, 2015
1 parent 777880a commit b9fb839
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 82 deletions.
6 changes: 4 additions & 2 deletions apps/system/js/core.js
Expand Up @@ -31,12 +31,14 @@
BaseModule.create(Core, {
name: 'Core',

// BluetoothCore should be load after NfcCore to
// make sure NFC can receive event from Bluetooth
REGISTRY: {
'mozTelephony': 'TelephonyMonitor',
'mozSettings': 'SettingsCore',
'mozBluetooth': 'BluetoothCore',
'mozMobileConnections': 'MobileConnectionCore',
'mozNfc': 'NfcCore'
'mozNfc': 'NfcCore',
'mozBluetooth': 'BluetoothCore'
},

getAPI: function(api) {
Expand Down
133 changes: 53 additions & 80 deletions apps/system/js/nfc_handover_manager.js
Expand Up @@ -26,6 +26,8 @@
];

NfcHandoverManager.EVENTS = [
'bluetooth-available',
'bluetooth-unavailable',
'bluetooth-enabled',
'bluetooth-disabled',
'nfc-transfer-started',
Expand All @@ -39,21 +41,14 @@
* @type {boolean}
* @memberof NfcHandoverManager.prototype
*/
DEBUG: false,

/**
* mozBluetooth object
* @type {Object}
* @memberof NfcHandoverManager.prototype
*/
bluetooth: null,
DEBUG: true,

/**
* Default bluetooth adapter
* @type {Object}
* @memberof NfcHandoverManager.prototype
*/
defaultAdapter: null,
_adapter: null,

/**
* Keeps a list of actions that need to be performed after
Expand Down Expand Up @@ -153,38 +148,16 @@
* @memberof NfcHandoverManager.prototype
*/
_start: function _start() {
var self = this;

this.bluetooth = navigator.mozBluetooth;

this.incomingFileTransferInProgress = false;
this.bluetoothStatusSaved = false;
this.bluetoothAutoEnabled = false;

if (typeof(window.navigator.mozBluetooth.onattributechanged) !==
'undefined') {
this._debug('Bluetooth APIv2 does not support NFC yet');
return;
}
if (this.bluetooth.enabled) {
this._debug('Bluetooth already enabled on boot');
var req = this.bluetooth.getDefaultAdapter();
req.onsuccess = function bt_getAdapterSuccess() {
self.defaultAdapter = req.result;
self._debug('MAC address: ' + self.defaultAdapter.address);
self._debug('MAC name: ' + self.defaultAdapter.name);
};
req.onerror = function bt_getAdapterError() {
self._logVisibly('init: Failed to get bluetooth adapter');
};
}

window.navigator.mozSetMessageHandler('nfc-manager-send-file',
function(msg) {
self._debug('In New event nfc-manager-send-file' +
this._debug('In New event nfc-manager-send-file' +
JSON.stringify(msg));
self.handleFileTransfer(msg.peer, msg.blob, msg.requestId);
});
this.handleFileTransfer(msg.peer, msg.blob, msg.requestId);
}.bind(this));
},

'_handle_nfc-transfer-completed': function(evt) {
Expand All @@ -195,49 +168,50 @@
this._transferStarted(evt);
},

'_handle_bluetooth-available': function(evt) {
if (evt.detail && evt.detail.adapter) {
this._adapter = evt.detail.adapter;
this._debug('MAC address: ' + this._adapter.address);
this._debug('MAC name: ' + this._adapter.name);
} else {
this._debug('receive the wrong event format');
this._logVisibly('Failed to get bluetooth adapter');
}
},

'_handle_bluetooth-unavailable': function() {
this._debug('clean the bluetooth adapter');
this._adapter = null;
},

'_handle_bluetooth-disabled': function() {
this._debug('bluetooth-disabled');
this._clearBluetoothStatus();
},

'_handle_bluetooth-enabled': function() {
if (typeof(window.navigator.mozBluetooth.onattributechanged) !==
'undefined') {
this._debug('Bluetooth APIv2 does not support NFC yet');
return;
}
var self = this;
self._debug('bluetooth-enabled');
var req = self.bluetooth.getDefaultAdapter();
req.onsuccess = function bt_getAdapterSuccess() {
self.settingsNotified = false;
self.defaultAdapter = req.result;
self._debug('MAC address: ' + self.defaultAdapter.address);
self._debug('MAC name: ' + self.defaultAdapter.name);
/*
* Call all actions that have queued up while Bluetooth
* was turned on.
*/
for (var i = 0; i < self.actionQueue.length; i++) {
var action = self.actionQueue[i];
action.callback.apply(self, action.args);
this._debug('bluetooth-enabled');
/*
* Call all actions that have queued up while Bluetooth
* was turned on.
*/
if (this._adapter) {
for (var i = 0; i < this.actionQueue.length; i++) {
var action = this.actionQueue[i];
action.callback.apply(this, action.args);
}
self.actionQueue = [];
};
req.onerror = function bt_getAdapterError() {
self._logVisibly('event listner: Failed to get bluetooth adater');
};
this.actionQueue = [];
}
},


/**
* Save the on/off status of Bluetooth.
* @memberof NfcHandoverManager.prototype
*/
_saveBluetoothStatus: function _saveBluetoothStatus() {
if (!this.bluetoothStatusSaved) {
this.bluetoothStatusSaved = true;
this.bluetoothAutoEnabled = !this.bluetooth.enabled;
this.bluetoothAutoEnabled = !Service.query('Bluetooth.isEnabled');
}
},

Expand All @@ -250,8 +224,7 @@
Service.query('BluetoothTransfer.isSendFileQueueEmpty')) {
if (this.bluetoothAutoEnabled) {
this._debug('Disabling Bluetooth');
// XXX: Let BluetoothCore do this for you
this.writeSetting({'bluetooth.enabled': false});
window.dispatchEvent(new CustomEvent('request-disable-bluetooth'));
this.bluetoothAutoEnabled = false;
}
this.bluetoothStatusSaved = false;
Expand All @@ -276,12 +249,11 @@
* @memberof NfcHandoverManager.prototype
*/
_doAction: function _doAction(action) {
if (!this.bluetooth.enabled) {
if (!Service.query('Bluetooth.isEnabled')) {
this._debug('Bluetooth: not yet enabled');
this.actionQueue.push(action);
if (this.settingsNotified === false) {
// XXX: Let BluetoothCore do this for you
this.writeSetting({'bluetooth.enabled': true});
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));
this.settingsNotified = true;
}
} else {
Expand Down Expand Up @@ -324,12 +296,12 @@
*/
_doPairing: function _doPairing(mac) {
this._debug('doPairing: ' + mac);
if (this.defaultAdapter == null) {
if (this._adapter == null) {
// No BT
this._debug('No defaultAdapter');
this._debug('No _adapter');
return;
}
var req = this.defaultAdapter.pair(mac);
var req = this._adapter.pair(mac);
var self = this;
req.onsuccess = function() {
self._debug('Pairing succeeded');
Expand Down Expand Up @@ -439,8 +411,9 @@
return;
}

var cps = this.bluetooth.enabled ? NDEF.CPS_ACTIVE : NDEF.CPS_ACTIVATING;
var mac = this.defaultAdapter.address;
var cps = Service.query('Bluetooth.isEnabled') ?
NDEF.CPS_ACTIVE : NDEF.CPS_ACTIVATING;
var mac = this._adapter.address;
var hs = NDEFUtils.encodeHandoverSelect(mac, cps);
var promise = nfcPeer.sendNDEF(hs);
promise.then(() => {
Expand Down Expand Up @@ -492,9 +465,9 @@
var job = {nfcPeer: nfcPeer, blob: blob, requestId: requestId,
onsuccess: onsuccess, onerror: onerror};
this.sendFileQueue.push(job);
var cps =
this.bluetooth.enabled ? NDEF.CPS_ACTIVE : NDEF.CPS_ACTIVATING;
var mac = this.defaultAdapter.address;
var cps = Service.query('Bluetooth.isEnabled') ?
NDEF.CPS_ACTIVE : NDEF.CPS_ACTIVATING;
var mac = this._adapter.address;
var hr = NDEFUtils.encodeHandoverRequest(mac, cps);
var promise = nfcPeer.sendNDEF(hr);

Expand Down Expand Up @@ -525,11 +498,11 @@
/*
* Bug 979427:
* After pairing we connect to the remote device. The only thing we
* know here is the MAC address, but the defaultAdapter.connect()
* know here is the MAC address, but the _adapter.connect()
* requires a BluetoothDevice argument. So we use getPairedDevices()
* to map the MAC to a BluetoothDevice instance.
*/
var req = this.defaultAdapter.getPairedDevices();
var req = this._adapter.getPairedDevices();
var self = this;
req.onsuccess = function() {
var devices = req.result;
Expand All @@ -541,10 +514,10 @@
self._debug('Address: ' + device.address);
self._debug('Connected: ' + device.connected);
if (device.address.toLowerCase() == mac.toLowerCase()) {
self._debug('Connecting to ' + mac);
var r = self.defaultAdapter.connect(device);
r.onsuccess = successCb;
r.onerror = errorCb;
self._debug('Connecting to ' + mac);
var r = self._adapter.connect(device);
r.onsuccess = successCb;
r.onerror = errorCb;
}
}
};
Expand Down

0 comments on commit b9fb839

Please sign in to comment.