Polymer-platinum-bluetooth
提供:Dev Guides
ポリマー-プラチナBluetooth
<platinum-bluetooth>要素を使用して、近くのbluetoothデバイスと対話するために使用されます。
次のコマンドを実行してプロジェクトディレクトリにインストールすることにより、アプリケーションでこの要素を使用できます。
bower install --save PolymerElements/platinum-bluetooth
例
次の例では、Polymer.jsでplatinum-bluetooth要素の使用を指定しています。 indexlファイルを作成し、その中に次のコードを追加します。
<!doctype html>
<html>
<head>
<title>Polymer Example</title>
<script src = "bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel = "import" href = "bower_components/polymer/polymerl">
<link rel = "import" href = "bower_components/paper-styles/demo-pagesl">
<link rel = "import" href = "bower_components/paper-button/paper-buttonl">
<link rel = "import" href = "bower_components/platinum-bluetooth/platinum-bluetooth-devicel">
</head>
<body>
<section>
<paper-button raised>Get bluetooth device</paper-button>
</section>
<script src = "platinum_bluetooth.js"></script>
</body>
</html>
今、platinum_bluetooth.jsという別のファイルを作成し、次のコードを含めます-
document.addEventListener('WebComponentsReady', function() {
var mybatteryDevice = document.querySelector('platinum-bluetooth-device');
var mybutton = document.querySelector('paper-button');
mybutton.addEventListener('click', function() {
console.log('The requested bluetooth device advertising a battery service...');
mybatteryDevice.request().then(function(device) {
console.log('Bluetooth device has been found...');
console.log('The device name is: ' + device.name);
})
.catch(function(error) {
console.error('Sorry!No device found...', error);
});
});
});
出力
アプリケーションを実行するには、作成されたプロジェクトディレクトリに移動し、次のコマンドを実行します。
polymer serve
ここでブラウザを開き、 http://127.0.0.1:8081/ に移動します。 出力は次のようになります。
ボタンをクリックすると、コンソールに「バッテリーサービスを広告する要求されたbluetoothデバイス…」というメッセージが表示され、デバイスが見つからない場合はエラーメッセージが表示されます。