Skip to content

Commit

Permalink
force to use address
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzu-Lin Huang committed Jun 23, 2015
1 parent 45c5298 commit 8c8683d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ button, input[type="text"], textarea {
float: right;
}

#info > .center {
position: absolute;
left: 40%;
}

.toolbar {
width: 100%;
position: absolute;
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<div id="joystickArea"></div>
<div id="info">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
<div class="toolbar">
Expand Down
10 changes: 7 additions & 3 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var App = {
joystickAreaDiv: document.getElementById('joystickArea'),
leftDebugInfoElem: document.querySelector('#info > .left'),
rightDebugInfoElem: document.querySelector('#info > .right'),
bluetoothInfoElem: document.querySelector('#info > .center'),
gistIdElem: document.getElementById('gist-id'),
scriptElem: document.getElementById('script'),

Expand Down Expand Up @@ -223,15 +224,18 @@ var App = {
this.rsHelper = new RollingSpiderHelper();

// XXX
['connecting', 'discovering-services', 'connected', 'disconnect'].forEach(
['connecting', 'discovering-services', 'connected', 'disconnect',
'scanning-start', 'finding-device', 'scanning-stop',
'gatt-connecting'].forEach(
function(eventName) {
that.rsHelper.on(eventName, function() {
that.changeConnectButtonText(eventName);
that.bluetoothInfoElem.textContent = eventName;
switch(eventName) {
case 'connected':
// start monitoring joystick movement when there is connection
that._intervalId =
window.setInterval(that.monitorJoystickMovement.bind(this), 50);
window.setInterval(that.monitorJoystickMovement.bind(that), 50);
break;
case 'disconnect':
// stop monitoring joystick movement when disconnect
Expand All @@ -243,7 +247,7 @@ var App = {
});
this.connectButton.addEventListener('click', function() {
if (that.rsHelper.isAbleToConnect()) {
that.rsHelper.connect().then(function onResolve() {
that.rsHelper.connect({address: 'a0:14:3d:29:d3:f0'}).then(function onResolve() {
that.changeConnectButtonText('connected');
}, function onReject() {
that.changeConnectButtonText('disconnect');
Expand Down
17 changes: 11 additions & 6 deletions js/rolling_spider_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ RollingSpiderHelper.prototype = evt({
return this._stateManager.isConnected();
},

connect: function (deviceNamePrefix) {
var prefix = deviceNamePrefix || 'RS_';
connect: function (options) {
var prefix = options.deviceNamePrefix || 'RS_';
var address = options.address;
var that = this;
if (this._stateManager.isAbleToConnect()) {
this.fire('connecting');
return new Promise(function(resolve, reject) {
that.fire('scanning-start');
that._startScan().then(function(/* handle */) {
that.fire('finding-device', {prefix: prefix});
return that._findDevice(prefix);
that.fire('finding-device', {prefix: prefix, address: address});
return that._findDevice({deviceNamePrefix: prefix, address: address});
}).then(function(device) {
that.fire('scanning-stop');
device.gatt.onconnectionstatechanged =
Expand Down Expand Up @@ -121,13 +122,17 @@ RollingSpiderHelper.prototype = evt({
}
},

_findDevice: function(deviceNamePrefix) {
_findDevice: function(options) {
var namePrefix = options.deviceNamePrefix;
var address = options.address;
var that = this;
// XXX: we should set timeout for rejection
return new Promise(function(resolve, reject) {
var onGattDeviceFount = function(evt) {
var device = evt.device;
if(device.name.startsWith(deviceNamePrefix) && !that._isGattConnected) {
console.log('found ' + device.name + ': ' + device.address);
if(!that._isGattConnected &&
(device.name.startsWith(namePrefix) || device.address === address)) {
that._device = device;
that._gatt = device.gatt;
resolve(evt.device);
Expand Down
2 changes: 1 addition & 1 deletion js/script_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

run: function() {
if (this._tasks.length > 0 && this._rsHelper.isAbleToConnect()) {
this._rsHelper.connect().then(function() {
this._rsHelper.connect({address: 'a0:14:3d:29:d3:f0'}).then(function() {
// on resolve
this._intervalId =
window.setInterval(this.onEachTask.bind(this), this._timeout);
Expand Down

0 comments on commit 8c8683d

Please sign in to comment.