- Polymer 教程
- Polymer - 主页
- Polymer - 概述
- Polymer - 安装
- Polymer - 元素
- Polymer - 自定义元素
- Polymer - Shadow DOM 及样式
- Polymer - 事件
- Polymer - 数据系统
- Polymer 有用资源
- Polymer - 快速指南
- Polymer - 有用资源
- Polymer - 讨论
Polymer - Platinum 蓝牙
它用于与采用 <platinum-bluetooth> 元素的附近蓝牙设备互动。
您可以在应用程序中使用此元素,方法是运行以下命令在您的项目目录中进行安装。
bower install --save PolymerElements/platinum-bluetooth
示例
以下示例指定了在 Polymer.js 中使用 platinum-bluetooth 元素。创建一个 index.html 文件,并添加以下代码。
<!doctype html>
<html>
<head>
<title>Polymer Example</title>
<script src = "bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel = "import" href = "bower_components/polymer/polymer.html">
<link rel = "import" href = "bower_components/paper-styles/demo-pages.html">
<link rel = "import" href = "bower_components/paper-button/paper-button.html">
<link rel = "import" href = "bower_components/platinum-bluetooth/platinum-bluetooth-device.html">
</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/。以下将显示输出。
当您单击按钮时,它将在控制台中显示一条消息,内容为“请求的蓝牙设备广告电池服务...”,并且如果未找到设备,会显示一条错误消息。
polymer_elements.htm
广告