About IOT(2)Using BLE in Javascript(bluejely)

[su_button url=”https://deus-ex-machina-ism.com/?page_id=3768&lang=en” target=”blank”]Machine Learning[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?page_id=3788&lang=en” target=”blank”]Artificial Intelligence[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=1294&lang=en” target=”blank”]Natural Language Processing[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=3794&lang=en” target=”blank”]Semantic Web[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=6626&lang=en” target=”blank”]Algorithm[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=1171&lang=en” target=”blank”]Search Technology[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=1267&lang=en” target=”blank”]DataBase Technology[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=4741&lang=en” target=”blank”]Ontology Technology[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?page_id=3812&lang=en” target=”blank”]Digital Transformation[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=17494&lang=en” target=”blank”]UI and DataVisualization[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=18738&lang=en” target=”blank”]Workflow & Services[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=26941&lang=en” target=”blank”]IT Infrastructure[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=6764&lang=en” target=”blank”]Probabilistic Generative Model[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=44143&lang=en” target=”blank”]Computer Hardware[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=17998&lang=en” target=”blank”]Time Series Data Analysis[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=6677&lang=en” target=”blank”]Stream Data Control[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?p=11281&lang=en” target=”blank”]IOT&Sensor[/su_button] [su_button url=”https://deus-ex-machina-ism.com/?page_id=12241&lang=en” target=”blank”]Navigation of this blog[/su_button]

About IOT(2)Using BLE in Javascript(bluejely)

Following the previous article, I would like to discuss actual BLE communication in this article. First, let’s try the simplest way to communicate with BLE, using a javascript library called bluejelly.

bluejelly is a wrapper for a lavascript library called Web Bluetooth API, and it works with only three files: html file, bluejelly.js, and style.css. For example For example, the simplest code in the sample that scans for external BLE devices looks like the following. (Excerpt of sample code from bluejelly.js)

<!doctype html>
<!--Copyright 2017-2020 JellyWare Inc. All Rights Reserved.-->
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="BlueJelly">
    <meta name="viewport" content="width=640, maximum-scale=1.0, user-scalable=yes">
    <title>BlueJelly</title>
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="bluejelly.js"></script>   
<h1></h1>
<div class="container">
    <div class="title margin">
        <p id="title">BlueJelly Sample</p>
        <p id="subtitle">Hello, BLE</p>
    </div>
    <div class="contents margin">
        <button id="scan" class="button">Scan</button>
        <hr>
        <div id="device_name"> </div>
    </div>
</div>
<script>
//--------------------------------------------------
//Global変数
//--------------------------------------------------
//BlueJellyのインスタンス生成
const ble = new BlueJelly();
//--------------------------------------------------
//ロード時の処理
//--------------------------------------------------
window.onload = function () {
  //UUIDの設定
  ble.setUUID("UUID1",   "00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000");
}
//--------------------------------------------------
//Scan後の処理
//--------------------------------------------------
ble.onScan = function (deviceName) {
  document.getElementById('device_name').innerHTML = deviceName;
}

//-------------------------------------------------
//ボタンが押された時のイベント登録
//--------------------------------------------------
document.getElementById('scan').addEventListener('click', function() {
      ble.scan('UUID1');
});
</script>
</body>
</html>

Open this html file in your browser and it will work. A “scan” button appears in the browser, and when you press it, it detects Bluetooth devices around you and displays them. However, depending on the version of the browser, it may or may not work, and the latest version of SAFARI on mac did not work, but chrome did. The only part of this code that interacts with BLE is the ble.scan function, and the rest is very simple and mostly related to display.

As a library with a little more flexibility, there is noble that runs on node. I will introduce them in the next article.

Exit mobile version
タイトルとURLをコピーしました