WebTelephony API と WebSMS API – WebAPI の一部
原文: WebTelephony API and WebSMS API – Part of WebAPI (on March 1, 2012 by Robert Nyman)
以前、Mozilla’s Boot to Gecko – The Web is the Platform と Gaia, Mozilla’s user interface for Boot to Gecko で議論され明らかになったように、Web はとてもパワフルなプラットフォームになりつつあります! そこで、私たちの WebAPI イニシアチブから、WebTelephony と WebSMS の 2 つのエキサイティングな API を紹介したいと思います。
WebTelephony
基本的な電話機能へのアクセスは、単純に navigator.mozTelephony
を通して行うだけです。このオブジェクトへの参照を取得すれば、電話をかけたり受けたりできるようになります。以下は簡単なコード例です:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Telephony object | |
var tel = navigator.mozTelephony; | |
// Check if the phone is muted (read/write property) | |
console.log(tel.muted); | |
// Check if the speaker is enabled (read/write property) | |
console.log(tel.speakerEnabled); | |
// Place a call | |
var call = tel.dial("123456789"); | |
// Events for that call | |
call.onstatechange = function (event) { | |
/* | |
Possible values for state: | |
"dialing", "ringing", "busy", "connecting", "connected", | |
"disconnecting", "disconnected", "incoming" | |
*/ | |
console.log(event.state); | |
}; | |
// Above options as direct events | |
call.onconnected = function () { | |
// Call was connected | |
}; | |
call.ondisconnected = function () { | |
// Call was disconnected | |
}; | |
// Receiving a call | |
tel.onincoming = function (event) { | |
var incomingCall = event.call; | |
// Get the number of the incoming call | |
console.log(incomingCall.number); | |
// Answer the call | |
incomingCall.answer(); | |
}; | |
// Disconnect a call | |
call.hangUp(); | |
// Iterating over calls, and taking action depending on their changed status | |
tel.oncallschanged = function (event) { | |
tel.calls.forEach(function (call) { | |
// Log the state of each call | |
console.log(call.state); | |
}); | |
}; |
// Telephony オブジェクト var tel = navigator.mozTelephony; // 電話がミュートになっていないか確認 (read/write プロパティ) console.log(tel.muted); // スピーカが有効か確認 (read/write プロパティ) console.log(tel.speakerEnabled); // 電話をかける var call = tel.dial("123456789"); // 通話のためのイベント call.onstatechange = function (event) { /* 状態に使われる値: "dialing", "ringing", "busy", "connecting", "connected", "disconnecting", "disconnected", "incoming" */ console.log(event.state); }; // 上記のイベントに対応する関数 call.onconnected = function () { // 通話が接続された }; call.ondisconnected = function () { // 通話が切断された }; // 呼び出しを受ける tel.onincoming = function (event) { var incomingCall = event.call; // 呼び出し回数 console.log(incomingCall.number); // 呼び出しへの応答 incomingCall.answer(); }; // 通話を切断する call.hangUp(); // 呼び出しを繰り返し、状態の変更に応じた動作を行う tel.oncallschanged = function (event) { tel.calls.forEach(function (call) { // Log the state of each call console.log(call.state); }); };
Telephony は、Gaia の dialer と ホーム画面から利用可能です。
WebSMS
携帯電話のもう一つの中心的な機能は、SMS メッセージの送受信です。以下のコードでどのように行うかを示します:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SMS object | |
var sms = navigator.mozSMS; | |
// Send a message | |
sms.send("123456789", "Hello world!"); | |
// Recieve a message | |
sms.onrecieved = function (event) { | |
// Read message | |
console.log(event.message); | |
}; |
// SMS オブジェクト var sms = navigator.mozSMS; // メッセージ送信 sms.send("123456789", "Hello world!"); // メッセージ受信 sms.onrecieved = function (event) { // メッセージを読む console.log(event.message); };
ハックと貢献
これらの API とその内部の動作の探求に興味がある方は、Mozilla の Boot to Gecko のユーザインターフェース (Gaia) を調べることをお勧めします。Gaia に含まれる dialer.js ファイルと sms.js ファイルを調べてください。
また、Web 技術のスキルをスマートフォンの開発やカスタマイズのために役立てたい方は、遠慮せずに Gaia の開発に協力してください!