var gDeviceList=null;navigator.mozL10n.once(function(){function e(e){o&&console.log("[Bluetooth APP Device List]: "+e)}var t=navigator.mozL10n.get,n=window.navigator.mozSettings,i=window.navigator.mozBluetooth,d=null,o=!1;n&&i&&(gDeviceList=function(){function n(e,n){var i=document.createElement("span"),d=""===e.name?t("unnamed-device"):e.name,o=""===e.name?"unnamed-device":"";i.textContent=d,i.dataset.l10nId=o;var a=document.createElement("small");a.textContent=""===n?"":t(n),a.dataset.l10nId=n;var s=document.createElement("progress");s.classList.add("overlapping-icon"),s.classList.add("hidden");var l=document.createElement("a");l.appendChild(i),l.appendChild(a);var r=document.createElement("li");return r.classList.add("bluetooth-device"),r.classList.add("bluetooth-type-"+e.icon),r.appendChild(l),r.appendChild(s),r}function o(e){p.hidden=!e,e?(q.show(!0),x.hidden=!1):(q.show(!1),S.show(!1),x.hidden=!0,C=null,E=null,b=null,clearTimeout(I),I=null)}function a(e,t,n){f.hidden=!1,y.addEventListener("action",m),d=e,d.onpairedstatuschanged=function(e){c(e.status,"Authentication Failed",e.address)},d.ondiscoverystatechanged=function(e){e.discovering?(g.disabled=!0,x.hidden=!1):(g.disabled=!1,x.hidden=!0,clearTimeout(I),I=null)},L=t,B=n,l(),u()}function s(){f.hidden=!0,d=null,y.removeEventListener("action",m)}function l(){if(i.enabled&&d){var e=d.getPairedDevices();e.onsuccess=function(){function i(){return function(e){var i=e.address===b?"device-status-connected":"",d=n(e,i);d.onclick=function(){if(!D&&!C){v();var n=d.querySelector("small");n.textContent=t("device-status-connecting"),n.dataset.l10nId="device-status-connecting",h(e)}},S.list.appendChild(d),S.index[e.address]=[e,d]}}var d=e.result.slice(),o=d.length;if(0===o)return void S.show(!1);S.clear(),d.sort(function(e,t){return e.name>t.name});for(var a=0;o>a;a++)i()(d[a]);S.show(!0)}}}function r(i){var o=i.device,a=q.index[o.address]||S.index[o.address];if(a){var s=a[1];if(o.name&&s){var l=s.querySelector("a > span");l&&(l.dataset.l10nId="",l.textContent=o.name)}}else{var r=n(o,"device-status-tap-connect");r.onclick=function(){if(v(),!D&&!C){var n=r.querySelector("small");n.textContent=t("device-status-pairing"),n.dataset.l10nId="device-status-pairing",r.classList.add("icon-hidden");var i=r.querySelector("progress");i.classList.remove("hidden");var a=d.pair(o.address);C=o.address;var s="pairing with address = "+C;e(s),a.onerror=function(e){c(!1,a.error.name)}}},q.list.appendChild(r),q.index[o.address]=[o,r]}}function c(n,i,d){if(C){var o=C;if(C=null,n){if(q.index[o]){var a=q.index[o][0],s=q.index[o][1];q.list.removeChild(s),delete q.index[o],E=o,h(a)}}else{var r=t("error-pair-title");if("Repeated Attempts"===i?r=r+"\n"+t("error-pair-toofast"):"Authentication Failed"===i&&(r=r+"\n"+t("error-pair-pincode")),e(r),window.alert(r),q.index[o]){var c=q.index[o][1].querySelector("small");c.textContent=t("device-status-tap-connect"),c.dataset.l10nId="device-status-tap-connect";var u=q.index[o][1];u.classList.remove("icon-hidden");var v=q.index[o][1].querySelector("progress");v.classList.add("hidden")}}l()}else if(l(),d&&n&&q.index[d]){var m=q.index[d][1];q.list.removeChild(m),delete q.index[d]}}function u(){if(i.enabled&&d&&!I){var e=d.startDiscovery();e.onsuccess=function(){I||(I=setTimeout(v,w))},e.onerror=function(){console.error("Can not discover nearby device")}}}function v(){if(i.enabled&&d&&I){var e=d.stopDiscovery();e.onerror=function(){console.error("Failed to stop discovery of nearby devices")},clearTimeout(I),I=null}}function h(e){D=!0,L&&L(e)}function m(){B&&B()}var f=document.getElementById("devices-list-view"),p=document.getElementById("bluetooth-search"),g=document.getElementById("search-device"),x=document.getElementById("bluetooth-searching"),y=document.getElementById("devices-list-header"),C=null,E=null,b=null,w=6e4,I=null,L=null,B=null,D=!1,S={title:document.getElementById("bluetooth-paired-title"),list:document.getElementById("bluetooth-paired-devices"),index:[],clear:function(){for(;this.list.hasChildNodes();)this.list.removeChild(this.list.lastChild);this.index=[]},show:function(e){e||this.clear(),this.title.hidden=!e,this.list.hidden=!e}},q={title:document.getElementById("bluetooth-found-title"),list:document.getElementById("bluetooth-devices"),index:[],clear:function(){for(;this.list.hasChildNodes();)this.list.removeChild(this.list.lastChild);this.index=[]},show:function(e){e||this.clear(),this.title.hidden=!e,this.list.hidden=!e}};return g.onclick=function(){D||C||(o(!0),q.clear(),u())},{update:o,initWithAdapter:a,uninit:s,startDiscovery:u,onDeviceFound:r}}())});