Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
This commit is contained in:
parent
93e3a48004
commit
b38dea0597
25901 changed files with 9982925 additions and 369 deletions
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
define(['require','modules/navigator/mozBluetooth','modules/mvvm/observable'],function(require){'use strict';var NavigatorBluetooth=require('modules/navigator/mozBluetooth');var Observable=require('modules/mvvm/observable');var _debug=false;var debug=function(){};if(_debug){debug=function btam_debug(msg){console.log('--> [BluetoothAdapterManager]: '+msg);};}
|
||||
var BluetoothAdapterManager={defaultAdapter:null,_init:function btam__init(){if(!NavigatorBluetooth){return;}
|
||||
this._watchMozBluetoothAttributechanged();this._initDefaultAdapter();},_watchMozBluetoothAttributechanged:function btam__watchMozBluetoothAttributechanged(){NavigatorBluetooth.addEventListener('attributechanged',(evt)=>{for(var i in evt.attrs){switch(evt.attrs[i]){case'defaultAdapter':this.defaultAdapter=NavigatorBluetooth.defaultAdapter;debug('_watchMozBluetoothAttributechanged(): '+'this.defaultAdapter = '+this.defaultAdapter);break;default:break;}}});},_initDefaultAdapter:function btam__initDefaultAdapter(){this.defaultAdapter=NavigatorBluetooth.defaultAdapter;debug('_initDefaultAdapter(): this.defaultAdapter = '+
|
||||
this.defaultAdapter);}};var bluetoothAdapterManager=Observable(BluetoothAdapterManager);bluetoothAdapterManager._init();return bluetoothAdapterManager;});
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
define([],function(){'use strict';var _debug=false;var debug=function(){};if(_debug){debug=function btcodm_debug(msg){console.log('--> [BluetoothCODMapper]: '+msg);};}
|
||||
var MinorDeviceClasses={Computer:{0:'computer',1:'computer',2:'computer',3:'computer',4:'pda',5:'pda',6:'computer',7:'computer',reserved:'computer'},Phone:{0:'phone',1:'phone',2:'phone',3:'phone',4:'modem',5:'phone',reserved:'phone'},AudioVideo:{0:'audio-card',1:'audio-card',2:'audio-card',4:'audio-input-microphone',5:'audio-card',6:'audio-card',7:'audio-card',8:'audio-card',9:'audio-card',10:'audio-card',11:'camera-video',12:'camera-video',13:'camera-video',14:'video-display',15:'video-display',16:'video-display',18:'audio-card',reserved:'audio-card'},Peripheral:{1:'input-gaming',2:'input-gaming',16:'input-keyboard',17:'input-keyboard',18:'input-keyboard',19:'input-keyboard',20:'input-keyboard',21:'input-keyboard',22:'input-keyboard',23:'input-keyboard',24:'input-keyboard',25:'input-keyboard',26:'input-keyboard',27:'input-keyboard',28:'input-keyboard',29:'input-keyboard',30:'input-keyboard',31:'input-keyboard',32:'input-mouse',33:'input-mouse',34:'input-mouse',35:'input-mouse',36:'input-mouse',37:'input-tablet',38:'input-mouse',39:'input-mouse',40:'input-mouse',41:'input-mouse',42:'input-mouse',43:'input-mouse',44:'input-mouse',45:'input-mouse',46:'input-mouse',47:'input-mouse'},Imaging:{4:'video-display',8:'camera-photo',16:'scanner',32:'printer'}};var MajorDeviceClasses={1:MinorDeviceClasses.Computer,2:MinorDeviceClasses.Phone,3:'network-wireless',4:MinorDeviceClasses.AudioVideo,5:MinorDeviceClasses.Peripheral,6:MinorDeviceClasses.Imaging};var BluetoothClassOfDeviceMapper={getDeviceType:function btcodm_getDeviceType(cod){debug('cod.majorDeviceClass = '+cod.majorDeviceClass);debug('cod.majorServiceClass = '+cod.majorServiceClass);debug('cod.minorDeviceClass = '+cod.minorDeviceClass);var deviceType='';var majorDeviceClass=MajorDeviceClasses[cod.majorDeviceClass];debug('majorDeviceClass = '+majorDeviceClass);if(typeof(majorDeviceClass)==='object'){deviceType=majorDeviceClass[cod.minorDeviceClass]||majorDeviceClass.reserved||'';debug('return in minor class, type = '+deviceType);return deviceType;}else if(typeof(majorDeviceClass)==='string'){debug('return in major class, type = network-wireless');return majorDeviceClass;}else if(cod.majorServiceClass&0x100){deviceType='audio-card';debug('return in major service class, type = audio-card');return deviceType;}else{debug('not mapping in any class, return type = '+deviceType);return deviceType;}}};return BluetoothClassOfDeviceMapper;});
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
|
||||
define(['require','modules/bluetooth/bluetooth_adapter_manager','shared/async_storage'],function(require){'use strict';var AdapterManager=require('modules/bluetooth/bluetooth_adapter_manager');var AsyncStorage=require('shared/async_storage');var _debug=false;var debug=function(){};if(_debug){debug=function btam_debug(msg){console.log('--> [BluetoothConnectionManager]: '+msg);};}
|
||||
var BluetoothConnectionManager={Profiles:{'hfp':0x111E,'a2dp':0x110D},connectingAddress:null,_connectedDevicesInfo:{},_getConnectedDevicesPromise:null,_listeners:{'connecting':[],'connected':[],'disconnected':[],'profileChanged':[]},_defaultAdapter:null,_init:function btcm__init(){AdapterManager.observe('defaultAdapter',this._onDefaultAdapterChanged.bind(this));this._onDefaultAdapterChanged(AdapterManager.defaultAdapter);},_onDefaultAdapterChanged:function btcm__onDefaultAdapterChanged(newAdapter,oldAdapter){debug('_onDefaultAdapterChanged(): newAdapter = '+newAdapter);debug('_onDefaultAdapterChanged(): oldAdapter = '+oldAdapter);this._defaultAdapter=newAdapter;if(oldAdapter){this._unwatchProfilesStatuschanged(oldAdapter);this._unwatchDefaultAdapterOnattributechanged(oldAdapter);}
|
||||
if(newAdapter){this._watchProfilesStatuschanged(newAdapter);this._watchDefaultAdapterOnattributechanged(newAdapter);if(newAdapter.state==='enabled'){this._restoreConnection();}}else{this._resetConnectionInfo();}},getConnectedDevices:function btcm_getConnectedDevices(){if(!this._getConnectedDevicesPromise){this._getConnectedDevicesPromise=this._initConnectedDevicesInfo().then(()=>{debug('getConnectedDevices(): resolved with latest cache = '+
|
||||
JSON.stringify(this._connectedDevicesInfo));return this._connectedDevicesInfo;},(reason)=>{debug('getConnectedDevices(): rejected with reason = '+reason);this._getConnectedDevicesPromise=null;});}
|
||||
return this._getConnectedDevicesPromise;},_initConnectedDevicesInfo:function btcm__initConnectedDevicesInfo(){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._getConnectedDevicesFromPlatform().then((connectedDevicesByProfile)=>{this._constructDeviceItemsMap(connectedDevicesByProfile);},(reason)=>{debug('_initConnectedDevicesInfo(): rejected in '+'_getConnectedDevicesFromPlatform');return Promise.reject(reason);});},_constructDeviceItemsMap:function btcm__constructDeviceItemsMap(connectedDevices){debug('_constructDeviceItemsMap(): connectedDevices = '+
|
||||
JSON.stringify(connectedDevices));if(!connectedDevices){debug('_constructDeviceItemsMap(): early return with empty object');return;}
|
||||
Object.keys(this.Profiles).map((profileID)=>{connectedDevices[profileID].forEach((connectedDevice)=>{var connectionDeviceInfo={address:connectedDevice.address,connected:true,profileID:profileID,device:connectedDevice};debug('_constructDeviceItemsMap(): connectionDeviceInfo = '+
|
||||
JSON.stringify(connectionDeviceInfo));this._initConnectedDevicesCache(connectionDeviceInfo);});});},_initConnectedDevicesCache:function btcm__initConnectedDevicesCache(options){debug('_initConnectedDevicesCache(): options = '+
|
||||
JSON.stringify(options));var info=this._connectedDevicesInfo[options.address];if(info){info.connectedProfiles[options.profileID]=options.connected;}else{var connectedDevice=(options.device)?options.device:null;info={'device':connectedDevice,'connectedProfiles':{}};info.connectedProfiles[options.profileID]=options.connected;}
|
||||
this._connectedDevicesInfo[options.address]=info;var dataToCheckConnectedProfile={address:options.address,connectedDevices:this._connectedDevicesInfo};if(!this._hasConnectedProfileByAddress(dataToCheckConnectedProfile)){delete this._connectedDevicesInfo[options.address];}
|
||||
debug('_initConnectedDevicesCache(): this._connectedDevicesInfo = '+
|
||||
JSON.stringify(this._connectedDevicesInfo));return this._connectedDevicesInfo;},_updateConnectedDevices:function btcm__updateConnectedDevices(options){return this.getConnectedDevices().then((connectedDevicesInfo)=>{debug('_updateConnectedDevices(): connectedDevicesInfo = '+
|
||||
JSON.stringify(connectedDevicesInfo));var info=(connectedDevicesInfo)?connectedDevicesInfo[options.address]:null;if(info){info.connectedProfiles[options.profileID]=options.connected;}else{var connectedDevice=(options.device)?options.device:null;info={'device':connectedDevice,'connectedProfiles':{}};info.connectedProfiles[options.profileID]=options.connected;}
|
||||
this._connectedDevicesInfo[options.address]=info;var dataToCheckConnectedProfile={address:options.address,connectedDevices:this._connectedDevicesInfo};if(!this._hasConnectedProfileByAddress(dataToCheckConnectedProfile)){delete this._connectedDevicesInfo[options.address];}
|
||||
debug('_updateConnectedDevices(): this._connectedDevicesInfo = '+
|
||||
JSON.stringify(this._connectedDevicesInfo));return Promise.resolve(this._connectedDevicesInfo);},()=>{debug('_updateConnectedDevices(): rejected with some exception');return Promise.reject('rejected with some exception');});},_resetConnectionInfo:function btcm__resetConnectionInfo(){this.connectingAddress=null;this._getConnectedDevicesPromise=null;},_watchDefaultAdapterOnattributechanged:function btcm__watchDefaultAdapterOnattributechanged(adapter){adapter.addEventListener('attributechanged',this._onAdapterAttributeChanged.bind(this,adapter));},_unwatchDefaultAdapterOnattributechanged:function btcm__unwatchDefaultAdapterOnattributechanged(adapter){adapter.removeEventListener('attributechanged',this._onAdapterAttributeChanged);},_onAdapterAttributeChanged:function btcm__onAdapterAttributeChanged(adapter,evt){for(var i in evt.attrs){debug('_onAdapterAttributeChanged(): '+evt.attrs[i]);switch(evt.attrs[i]){case'state':if(adapter.state==='enabled'){this._restoreConnection();}
|
||||
break;default:break;}}},_watchProfilesStatuschanged:function btcm__watchProfilesStatuschanged(adapter){var eventName;for(var profileID in this.Profiles){eventName='on'+profileID+'statuschanged';adapter[eventName]=this._onProfileStatuschangeHandler.bind(this,profileID);}},_unwatchProfilesStatuschanged:function btc__unwatchProfilesStatuschanged(adapter){var eventName;for(var profileID in this.Profiles){eventName='on'+profileID+'statuschanged';adapter[eventName]=null;}},_onProfileStatuschangeHandler:function btcm__onProfileStatuschangeHandler(profileID,evt){debug('_onProfileStatuschangeHandler(): profileID = '+profileID+', evt.address = '+evt.address+', evt.status = '+evt.status);var options={address:evt.address,connected:evt.status,profileID:profileID};this._updateConnectionStatus(options);},_restoreConnection:function btcm__restoreConnection(){if(!this._defaultAdapter){return;}
|
||||
AsyncStorage.getItem('device.connected',(address)=>{if(!address){return;}
|
||||
this.getConnectedDevices().then((connectedDevices)=>{if(connectedDevices&&connectedDevices[address]){debug('_restoreConnection(): early return cause connected already');return;}
|
||||
var restoreDevice=this._getPairedDeviceByAddress(address);if(restoreDevice){this.connectingAddress=restoreDevice.address;var event={type:'connecting',detail:{address:restoreDevice.address}};this._emitEvent(event);this._connect(restoreDevice).then(()=>{debug('_restoreConnection(): restore connection successfully');},(reason)=>{debug('_restoreConnection(): restore connection failed, '+'reason = '+reason);this.connectingAddress=null;event={type:'disconnected',detail:{address:restoreDevice.address}};this._emitEvent(event);});}});});},_recordConnection:function btcm__recordConnection(action,address){if(action==='set'){AsyncStorage.setItem('device.connected',address);debug('_recordConnection(): set item');}else if((action==='remove')&&(this._defaultAdapter.state==='enabled')){AsyncStorage.removeItem('device.connected');debug('_recordConnection(): remove item');}},_updateConnectionStatus:function btcm___updateConnectionStatus(options){debug('_updateConnectionStatus(): address = '+options.address+', connected = '+options.connected+', profileID = '+options.profileID);this.connectingAddress=null;this._updateConnectedDevices(options).then((connectedDevicesInfo)=>{debug('_updateConnectionStatus(): _updateConnectedDevices() '+'resolved with connectedDevicesInfo = '+
|
||||
JSON.stringify(connectedDevicesInfo));var dataToCheckConnectedProfile={address:options.address,connectedDevices:connectedDevicesInfo};var event;if(options.connected){event={type:'connected',detail:{address:options.address}};this._emitEvent(event);this._recordConnection('set',options.address);}else{if(!this._hasConnectedProfileByAddress(dataToCheckConnectedProfile)){if(this._defaultAdapter.state==='enabled'){this._recordConnection('remove');}
|
||||
event={type:'disconnected',detail:{address:options.address}};this._emitEvent(event);}}
|
||||
var newProfiles;if(!this._hasConnectedProfileByAddress(dataToCheckConnectedProfile)){newProfiles=null;}else{newProfiles=connectedDevicesInfo[options.address].connectedProfiles;}
|
||||
event={type:'profileChanged',detail:{address:options.address,profiles:newProfiles}};this._emitEvent(event);},(reason)=>{debug('_updateConnectionStatus(): miss to update in rejected case, '+'reason = '+reason);});},connect:function btcm_connect(device){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
debug('connect(): Want to connect device address = '+device.address+', name = '+device.name);var connectedDevices=[];return this.getConnectedDevices().then((connectedDevicesInfo)=>{for(var address in connectedDevicesInfo){if(connectedDevicesInfo[address].device!==null){connectedDevices.push(connectedDevicesInfo[address].device);debug('connect(): push device cache in queue = '+
|
||||
JSON.stringify(connectedDevices));}else{var regetPairedDevice=this._getPairedDeviceByAddress(address);connectedDevices.push(regetPairedDevice);debug('connect(): push _getPairedDeviceByAddress in queue = '+
|
||||
JSON.stringify(regetPairedDevice));}}
|
||||
debug('connect(): Will disconnect these connected devices = '+
|
||||
JSON.stringify(connectedDevices));return Promise.all(connectedDevices.map((connectedDevice)=>{return this.disconnect(connectedDevice);})).then(()=>{debug('connect(): Start to connect with wanted device address = '+
|
||||
device.address);return this._connect(device).then(()=>{debug('connect(): Resolved');},(reason)=>{debug('connect(): reason = '+reason);return Promise.reject(reason);});});});},_connect:function btcm__connect(device){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
this.connectingAddress=device.address;var event={type:'connecting',detail:{address:device.address}};this._emitEvent(event);debug('_connect(): before start to connect, stop discovery first');return this._defaultAdapter.stopDiscovery().then(()=>{debug('_connect(): start connecting device, '+'address = '+device.address);return this._defaultAdapter.connect(device).then(()=>{debug('_connect(): resolve, already connected with address = '+
|
||||
device.address);},()=>{this.connectingAddress=null;event={type:'disconnected',detail:{address:device.address}};this._emitEvent(event);debug('_connect(): reject with connection failed');return Promise.reject('connection failed');});},()=>{this.connectingAddress=null;event={type:'disconnected',detail:{address:device.address}};this._emitEvent(event);debug('_connect(): reject with stop discovery failed');return Promise.reject('stop discovery failed');});},disconnect:function btcm_disconnect(device){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.disconnect(device).then(()=>{debug('disconnect(): onsuccess(): resolve');},()=>{debug('disconnect(): onerror(): reject with disconnect failed');return Promise.reject('disconnect failed');});},_getConnectedDevicesFromPlatform:function btcm__getConnectedDevicesFromPlatform(){return Promise.all(Object.keys(this.Profiles).map((profile)=>{return this._getConnectedDevicesByProfile(this.Profiles[profile]);})).then((connectedDevices)=>{var collectedConnectedDevicesByProfile={};Object.keys(this.Profiles).forEach((profile,index)=>{collectedConnectedDevicesByProfile[profile]=connectedDevices[index];});debug('_getConnectedDevicesFromPlatform(): '+'collectedConnectedDevicesByProfile = '+
|
||||
JSON.stringify(collectedConnectedDevicesByProfile));return Promise.resolve(collectedConnectedDevicesByProfile);});},_getConnectedDevicesByProfile:function btcm__getConnectedDevicesByProfile(profileID){if(!this._defaultAdapter){debug('_getConnectedDevicesByProfile(): reject with no adapter');return Promise.reject('default adapter is not existed!!');}
|
||||
if(this._defaultAdapter.state==='disabled'){debug('_getConnectedDevicesByProfile(): resolve with empty array '+'since it is impossible to connect with any device');return Promise.reject('getConnectedDevices in disabled state');}
|
||||
return this._defaultAdapter.getConnectedDevices(profileID).then((connectedDevice)=>{debug('_getConnectedDevicesByProfile(): resolved with '+'connectedDevice = '+JSON.stringify(connectedDevice));return Promise.resolve(connectedDevice||[]);},(reason)=>{debug('_getConnectedDevicesByProfile(): rejected with '+'reason = '+reason);return Promise.reject(reason);});},_getPairedDeviceByAddress:function btcm__getPairedDeviceByAddress(address){if(!this._defaultAdapter){return null;}
|
||||
var pairedDevices=this._defaultAdapter.getPairedDevices();if(pairedDevices.length===0){return null;}else{for(var i in pairedDevices){if(pairedDevices[i].address===address){return pairedDevices[i];}}
|
||||
return null;}},_hasConnectedProfileByAddress:function btcm__hasConnectedProfileByAddress(options){if(!options.connectedDevices[options.address]){return false;}
|
||||
var hasConnectedProfile=false;for(var profileID in this.Profiles){var connectedProfiles=options.connectedDevices[options.address].connectedProfiles;if(connectedProfiles&&(connectedProfiles[profileID]===true)){hasConnectedProfile=true;}}
|
||||
return hasConnectedProfile;},_emitEvent:function btcm__emitEvent(options){this._listeners[options.type].forEach(function(listener){listener(options);});},addEventListener:function btcm_addEventListener(eventName,callback){if(callback&&(this._listeners.hasOwnProperty(eventName))){this._listeners[eventName].push(callback);}},removeEventListener:function btcm_removeEventListener(eventName,callback){if(callback&&(this._listeners.hasOwnProperty(eventName))){var index=this._listeners[eventName].indexOf(callback);if(index>=0){this._listeners[eventName].splice(index,1);}}}};BluetoothConnectionManager._init();return BluetoothConnectionManager;});
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
define(['require','modules/bluetooth/bluetooth_adapter_manager','modules/bluetooth/bluetooth_device','modules/bluetooth/bluetooth_connection_manager','modules/mvvm/observable','modules/mvvm/observable_array','modules/settings_cache'],function(require){'use strict';var AdapterManager=require('modules/bluetooth/bluetooth_adapter_manager');var BtDevice=require('modules/bluetooth/bluetooth_device');var ConnectionManager=require('modules/bluetooth/bluetooth_connection_manager');var Observable=require('modules/mvvm/observable');var ObservableArray=require('modules/mvvm/observable_array');var SettingsCache=require('modules/settings_cache');var settings=navigator.mozSettings;var _debug=false;var debug=function(){};if(_debug){debug=function btc_debug(msg){console.log('--> [BluetoothContext]: '+msg);};}
|
||||
var BluetoothContext={state:'disabled',enabled:undefined,address:null,name:'',discoverable:false,discovering:false,numberOfPairedDevices:0,firstPairedDeviceName:'',hasPairedDevice:false,_defaultAdapter:null,_init:function btc__init(){AdapterManager.observe('defaultAdapter',this._onDefaultAdapterChanged.bind(this));this._onDefaultAdapterChanged(AdapterManager.defaultAdapter);ConnectionManager.addEventListener('connecting',this._updateDeviceConnectionInfo.bind(this));ConnectionManager.addEventListener('connected',this._updateDeviceConnectionInfo.bind(this));ConnectionManager.addEventListener('disconnected',this._updateDeviceConnectionInfo.bind(this));ConnectionManager.addEventListener('profileChanged',this._updateDeviceConnectionInfo.bind(this));},_initProperties:function btc__initProperties(adapter){this._updateStatus(adapter.state);this.address=adapter.address;this.name=adapter.name;this.discoverable=adapter.discoverable;this.discovering=adapter.discovering;},_resetProperties:function btc__resetProperties(){this._updateStatus('disabled');this.address='';this.name='';this.discoverable=false;this.discovering=false;},_watchDefaultAdapterAttributechanged:function btc__watchDefaultAdapterAttributechanged(adapter){adapter.addEventListener('attributechanged',this._onAdapterAttributeChanged.bind(this,adapter));},_unwatchDefaultAdapterAttributechanged:function btc__unwatchDefaultAdapterAttributechanged(adapter){adapter.removeEventListener('attributechanged',this._onAdapterAttributeChanged);},_watchDefaultAdapterOndevicepaired:function btc__watchDefaultAdapterOndevicepaired(adapter){adapter.ondevicepaired=this._onAdapterDevicepaired.bind(this,adapter);},_unwatchDefaultAdapterOndevicepaired:function btc__unwatchDefaultAdapterOndevicepaired(adapter){adapter.ondevicepaired=null;},_watchDefaultAdapterOndeviceunpaired:function btc__watchDefaultAdapterOndeviceunpaired(adapter){adapter.ondeviceunpaired=this._onAdapterDeviceunpaired.bind(this,adapter);},_unwatchDefaultAdapterOndeviceunpaired:function btc__unwatchDefaultAdapterOndeviceunpaired(adapter){adapter.ondeviceunpaired=null;},_onAdapterAttributeChanged:function btc__onAdapterAttributeChanged(adapter,evt){for(var i in evt.attrs){debug('--> _onAdapterAttributeChanged(): '+evt.attrs[i]);switch(evt.attrs[i]){case'state':this._updateStatus(adapter.state);if(adapter.state==='enabled'){this._refreshPairedDevicesInfo(adapter);this.startDiscovery();}
|
||||
break;case'address':this.address=adapter.address;break;case'name':this.name=adapter.name;break;case'discoverable':this.discoverable=adapter.discoverable;break;case'discovering':this.discovering=adapter.discovering;break;default:break;}}},_onAdapterDevicepaired:function btc__onAdapterDevicepaired(adapter,evt){debug('_onAdapterDevicepaired evt = '+evt);this._removeItemFromList(this._remoteDevices,evt.device.address);this._refreshPairedDevicesInfo(adapter);},_onAdapterDeviceunpaired:function btc__onAdapterDeviceunpaired(adapter,evt){debug('_onAdapterDeviceunpaired evt = '+evt);this._refreshPairedDevicesInfo(adapter);},_onDefaultAdapterChanged:function btc__onDefaultAdapterChanged(newAdapter,oldAdapter){debug('_onDefaultAdapterChanged(): newAdapter = '+newAdapter);debug('_onDefaultAdapterChanged(): oldAdapter = '+oldAdapter);this._defaultAdapter=newAdapter;if(oldAdapter){this._unwatchDefaultAdapterAttributechanged(oldAdapter);this._unwatchDefaultAdapterOndeviceunpaired(oldAdapter);this._unwatchDefaultAdapterOndevicepaired(oldAdapter);}
|
||||
if(newAdapter){this._initProperties(newAdapter);this._watchDefaultAdapterAttributechanged(newAdapter);this._watchDefaultAdapterOndevicepaired(newAdapter);this._watchDefaultAdapterOndeviceunpaired(newAdapter);if(newAdapter.state==='enabled'){this._refreshPairedDevicesInfo(newAdapter);}}else{this._resetProperties();}},_refreshPairedDevicesInfo:function btc__refreshPairedDevicesInfo(adapter){var pairedDevices=adapter.getPairedDevices();debug('pairedDevices.length = '+pairedDevices.length);this._pairedDevices.reset([]);if(pairedDevices.length===0){this.firstPairedDeviceName='';this.hasPairedDevice=false;}else{pairedDevices.sort(function(a,b){return a.name>b.name;});for(var i in pairedDevices){var observableBtDevice=BtDevice(pairedDevices[i]);this._pairedDevices.push(observableBtDevice);}
|
||||
this.firstPairedDeviceName=this._pairedDevices.get(0).name;this.hasPairedDevice=true;this._initConnectingDevices();this._initConnectedDevices();}
|
||||
this.numberOfPairedDevices=pairedDevices.length;},_updateStatus:function btc__updateStatus(state){var enabled;if(state==='enabled'||state==='disabling'){enabled=true;}else if(state==='disabled'||state==='enabling'){enabled=false;}
|
||||
this.state=state;debug('_updateStatus(): set state = '+state);this.enabled=enabled;debug('_updateStatus(): set enabled = '+enabled);this._syncWithSettingsKey(enabled);},_syncWithSettingsKey:function btc__syncWithSettingsKey(enabled){SettingsCache.getSettings((results)=>{var btEnabled=results['bluetooth.enabled'];if(btEnabled!==enabled){settings.createLock().set({'bluetooth.enabled':enabled});}});},setEnabled:function btc_setEnabled(enabled){debug('setEnabled(): request set enabled = '+enabled);if((this.enabled===enabled)||(this.state==='enabling')||(this.state==='disabling')){return Promise.reject('state transition!!');}
|
||||
if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
if(enabled){return this._defaultAdapter.enable().then(()=>{debug('setEnabled(): set enable successfully :)');},(reason)=>{debug('setEnabled(): set enable failed: reason = '+reason);return Promise.reject(reason);});}else{return this._defaultAdapter.disable().then(()=>{debug('setEnabled(): set disable successfully :)');},(reason)=>{debug('setEnabled(): set disable failed: reason = '+reason);return Promise.reject(reason);});}},setDiscoverable:function btc_setDiscoverable(enabled){if((this.discoverable===enabled)||(this.state!=='enabled')){return Promise.reject('same state or Bluetooth is disabled!!');}
|
||||
if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.setDiscoverable(enabled).then(()=>{debug('setDiscoverable(): set discoverable '+
|
||||
enabled+' successfully :)');},(reason)=>{debug('setDiscoverable(): set discoverable failed: '+'reason = '+reason);return Promise.reject(reason);});},setName:function btc_setName(name){if(name===this.name){return Promise.reject('same name!!');}
|
||||
if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.setName(name).then(()=>{debug('setName(): set name successfully :) name = '+
|
||||
this._defaultAdapter.name);},(reason)=>{debug('setName(): set name failed: reason = '+reason);return Promise.reject(reason);});},setNameByProductModel:function btc_setNameByProductModel(){SettingsCache.getSettings((results)=>{var productModel=results['deviceinfo.product_model'];debug('setNameByProductModel(): productModel = '+productModel);this.setName(productModel);});},_pairedDevices:ObservableArray([]),_remoteDevices:ObservableArray([]),_discoveryHandler:null,startDiscovery:function btc_startDiscovery(){if((this.discovering===true)||(this.state!=='enabled')){return Promise.reject('same state or Bluetooth is disabled!!');}
|
||||
this._remoteDevices.reset([]);if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.startDiscovery().then((handle)=>{debug('startDiscovery(): startDiscovery successfully :)');this._setDiscoveryHandler(handle);},(reason)=>{debug('startDiscovery(): startDiscovery failed: '+'reason = '+reason);return Promise.reject(reason);});},stopDiscovery:function btc_stopDiscovery(){if(this.discovering===false){debug('stopDiscovery(): stopDiscovery successfully in same state :)');return Promise.resolve('same state');}
|
||||
if(this.state!=='enabled'){return Promise.reject('Bluetooth is disabled!!');}
|
||||
if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.stopDiscovery().then(()=>{debug('stopDiscovery(): stopDiscovery successfully :)');},(reason)=>{debug('stopDiscovery(): stopDiscovery failed: reason = '+reason);return Promise.reject(reason);});},_setDiscoveryHandler:function btc__setDiscoveryHandler(handle){debug('_setDiscoveryHandler(): handle = '+handle);this._discoveryHandler=handle;this._discoveryHandler.ondevicefound=this._onDeviceFound.bind(this);},_onDeviceFound:function btc__onDeviceFound(evt){this._saveDevice(evt.device);},_saveDevice:function btc__saveDevice(device){var existedDevice=this._findDeviceByAddress({paired:device.paired,address:device.address});if(!existedDevice){var observableBtDevice=BtDevice(device);var operatingDevices=(device.paired)?this.getPairedDevices():this.getRemoteDevices();operatingDevices.push(observableBtDevice);}else{}},_removeItemFromList:function btc__removeItemFromList(list,address){var index=list.array.findIndex(this._matchDeviceByAddress.bind(this,address));if(index>-1){list.splice(index,1);debug('_removeItemFromList(): index = '+index);}else{}},_findDeviceByAddress:function btc__findDeviceByAddress(options){var operatingDevices=(options.paired)?this.getPairedDevices():this.getRemoteDevices();var index=operatingDevices.array.findIndex(this._matchDeviceByAddress.bind(this,options.address));if(index>-1){return operatingDevices.get(index);}else{return null;}},_matchDeviceByAddress:function btc__matchDeviceByAddress(address,btDevice){return btDevice.address&&(btDevice.address===address);},getPairedDevices:function btc_getPairedDevices(){return this._pairedDevices;},getRemoteDevices:function btc_getRemoteDevices(){return this._remoteDevices;},pair:function btc_pair(address){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this.stopDiscovery().then(()=>{return this._defaultAdapter.pair(address).then(()=>{debug('pair(): Resolved with void value');},(reason)=>{debug('pair(): Reject with this reason: '+reason);return Promise.reject(reason);});},(reason)=>{return Promise.reject(reason);});},unpair:function btc_unpair(address){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.unpair(address).then(()=>{debug('unpair(): Resolved with void value');},(reason)=>{debug('unpair(): Reject with this reason: '+reason);return Promise.reject(reason);});},sendFile:function btc_sendFile(address,blob){if(!this._defaultAdapter){return Promise.reject('default adapter is not existed!!');}
|
||||
return this._defaultAdapter.sendFile(address,blob).then(()=>{debug('sendFile(): Resolved with void value');},(reason)=>{debug('sendFile(): Reject with this reason: '+reason);return Promise.reject(reason);});},_initConnectingDevices:function btc__initConnectingDevices(){if(!ConnectionManager.connectingAddress){return;}
|
||||
var existedDevice=this._findDeviceByAddress({paired:true,address:ConnectionManager.connectingAddress});if(existedDevice){var options={connectionStatus:'connecting'};debug('_initConnectingDevices(): options = '+JSON.stringify(options));existedDevice.updateConnectionInfo(options);}},_initConnectedDevices:function btc__initConnectedDevices(){ConnectionManager.getConnectedDevices().then((connectedDevices)=>{for(var address in connectedDevices){var existedDevice=this._findDeviceByAddress({paired:true,address:address});if(existedDevice){var options={connectionStatus:'connected',profiles:connectedDevices[address].connectedProfiles};debug('_initConnectedDevices(): address = '+address+', options = '+JSON.stringify(options));existedDevice.updateConnectionInfo(options);}}},(reason)=>{debug('_initConnectedDevices(): getConnectedDevices(): failed, '+'reason = '+reason);});},_updateDeviceConnectionInfo:function btc__updateDeviceConnectionInfo(event){debug('_updateDeviceConnectionInfo(): event = '+JSON.stringify(event));var existedDevice=this._findDeviceByAddress({paired:true,address:event.detail.address});if(existedDevice){var options;switch(event.type){case'connecting':case'connected':case'disconnected':options={connectionStatus:event.type};break;case'profileChanged':options={profiles:event.detail.profiles};break;default:break;}
|
||||
debug('_updateDeviceConnectionInfo(): options = '+
|
||||
JSON.stringify(options));existedDevice.updateConnectionInfo(options);}else{}}};var bluetoothContext=Observable(BluetoothContext);bluetoothContext._init();return bluetoothContext;});
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
define(['require','modules/bluetooth/bluetooth_cod_mapper','modules/mvvm/observable'],function(require){'use strict';var BtClassOfDeviceMapper=require('modules/bluetooth/bluetooth_cod_mapper');var Observable=require('modules/mvvm/observable');var _debug=false;var debug=function(){};if(_debug){debug=function btd_debug(msg){console.log('--> [BluetoothDevice]: '+msg);};}
|
||||
var updateConnectionInfo=function(options){debug('updateConnectionInfo(): this.address = '+this.address+', this.name = '+this.name+', options = '+JSON.stringify(options));if(options.connectionStatus){this.connectionStatus=options.connectionStatus;}
|
||||
if(options.profiles){this.profiles=Object.create(options.profiles);}
|
||||
debug('updateConnectionInfo(): this.connectionStatus = '+
|
||||
this.connectionStatus+', this.profiles = '+
|
||||
JSON.stringify(this.profiles));};var updateDescriptionText=function(){debug('updateDescriptionText():');if(this.paired===false){this.descriptionText='tapToConnect';}else if(this.paired==='pairing'){this.descriptionText='pairing';}else if(this.paired===true){this.descriptionText='paired';if(this.connectionStatus==='connecting'){this.descriptionText='connecting';}else if(this.connectionStatus==='connected'){if(this.profiles){var hfpConnected=this.profiles.hfp;var a2dpConnected=this.profiles.a2dp;if(hfpConnected&&a2dpConnected){this.descriptionText='connectedWithDeviceMedia';}else if(hfpConnected){this.descriptionText='connectedWithDevice';}else if(a2dpConnected){this.descriptionText='connectedWithMedia';}else{this.descriptionText='connectedWithNoProfileInfo';}}else{this.descriptionText='connectedWithNoProfileInfo';}}else if(this.connectionStatus==='disconnected'){this.descriptionText='disconnected';}}
|
||||
debug('updateDescriptionText(): this.descriptionText = '+
|
||||
this.descriptionText);};return function ctor_bluetooth_device(device){var type=BtClassOfDeviceMapper.getDeviceType(device.cod);var connectionStatus=device.connectionStatus||'disconnected';var observableBluetoothDevice=Observable({name:device.name,paired:device.paired,address:device.address,type:type,connectionStatus:connectionStatus,profiles:null,descriptionText:'',get data(){return device;},updateConnectionInfo:updateConnectionInfo,updateDescriptionText:updateDescriptionText});observableBluetoothDevice._init=function btd__init(){this.observe('paired',this.updateDescriptionText.bind(this));this.observe('connectionStatus',this.updateDescriptionText.bind(this));this.observe('profiles',this.updateDescriptionText.bind(this));};device.onattributechanged=function btd_onDeviceAttributeChanged(evt){for(var i in evt.attrs){debug('onDeviceAttributeChanged(): '+evt.attrs[i]);switch(evt.attrs[i]){case'name':observableBluetoothDevice.name=device.name;break;case'paired':debug('onDeviceAttributeChanged(): '+'device.paired = '+device.paired);observableBluetoothDevice.paired=device.paired;break;case'cod':debug('onDeviceAttributeChanged(): '+'device.cod = '+device.cod);observableBluetoothDevice.type=BtClassOfDeviceMapper.getDeviceType(device.cod);break;default:break;}}};observableBluetoothDevice._init();return observableBluetoothDevice;};});
|
||||
Loading…
Reference in a new issue