このトピックでは、WVBluetooth の JavaScript API について説明します。HTML5 アプリまたはミニアプリを作成する際に、このトピックを参照できます。 WVBluetooth の JavaScript API を使用して、Bluetooth の権限を申請し、Bluetooth Low Energy(BLE)デバイスを検索し、アプリが実行されているデバイスを Bluetooth デバイスに接続し、BLE デバイスに関連するすべてのサービスをクエリできます。
WVBluetooth.requestAuthorization
Bluetooth の権限を申請し、Bluetooth を有効にします。
入力パラメーター
入力パラメーターはありません。
コールバックパラメーター
成功コールバックのパラメーター:
[object]値: JSON オブジェクト。次のキーパラメーターとキーパラメーターの対応する値が含まれています。[string]state: 'poweredOn'。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
window.WindVane.call('WVBluetooth', 'requestAuthorization', {}, function(e) {
alert('success: ' + JSON.stringify(e));
}, function(e) {
alert('failure: ' + JSON.stringify(e));
});WVBluetooth.scan
BLE デバイスを検索します。
入力パラメーター
入力パラメーターはありません。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
イベントリスニング
WV.Event.WVBluetooth.discoverDevice: BLE デバイスを検出します。
イベントパラメーター:
[string]name: Bluetooth デバイスの名前。[string]devceId: Bluetooth デバイスのアドレス。
document.addEventListener('WV.Event.WVBluetooth.discoverDevice', function (e) {
alert('event discoverDevice: ' + JSON.stringify(e.param));
});
window.WindVane.call('WVBluetooth', 'scan', {}, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.stopScan
BLE デバイスの検索を停止します。
入力パラメーター
入力パラメーターはありません。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
window.WindVane.call('WVBluetooth', 'stopScan', {}, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.connect
アプリが実行されているデバイスを、指定された Bluetooth アドレスを持つ Bluetooth デバイスに接続します。
入力パラメーター
[string]deviceId: 接続する Bluetooth デバイスのアドレス。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd'
};
window.WindVane.call('WVBluetooth', 'connect', params, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.disconnect
接続されている Bluetooth デバイスを切断します。
入力パラメーター
入力パラメーターはありません。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
イベントリスニング
WV.Event.WVBluetooth.GATTServerDisconnected: BLE 接続が失われました
document.addEventListener('WV.Event.WVBluetooth.GATTServerDisconnected', function (e) {
alert('event GATTServerDisconnected: ' + JSON.stringify(e.param));
});
window.WindVane.call('WVBluetooth', 'disconnect', {}, function(e) {
}, function(e) {
alert('failure: + JSON.stringify(e));
}WVBluetooth.getServices
BLE デバイスに関連するサービスをクエリします。
入力パラメーター
[string]deviceId: Bluetooth デバイスのアドレス。
最初に、アプリが実行されているデバイスを Bluetooth デバイスに接続する必要があります。
コールバックパラメーター
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd'
};
window.WindVane.call('WVBluetooth', 'getServices', params, function(e) {
alert('success: ' + JSON.stringify(e));
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.getCharacteristics
BLE デバイスのすべての特性値を取得します。
入力パラメーター
[
string]deviceId: Bluetooth デバイスのアドレス。説明最初に、アプリを実行するデバイスを Bluetooth デバイスに接続する必要があります。
[string]serviceId: サービスの ID。ID は UUID 形式である必要があります。
コールバックパラメーター
成功コールバックのパラメーター:
[object]characteristics: JSON 配列。配列内の各 JSON オブジェクトには、次のキーパラメーターとキーパラメーターの対応する値が含まれています。[string]characteristicId: 特性値の ID。ID は UUID 形式である必要があります。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd',
serviceId: 'xxxx'
};
window.WindVane.call('WVBluetooth', 'getCharacteristics', params, function(e) {
alert('success: ' + JSON.stringify(e));
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.writeValue
指定された特性値を接続されている BLE デバイスに書き込みます。
入力パラメーター
[string]deviceId: Bluetooth デバイスのアドレス。最初に、アプリが実行されているデバイスを Bluetooth デバイスに接続する必要があることに注意してください。[string]serviceId: サービスの ID。ID は UUID 形式である必要があります。[string]characteristicId: 特性値の ID。ID は UUID 形式である必要があります。[
文字列] 値 : 書き込まれる値。説明上記の文字列値は、Base64 を使用してエンコードされています。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
イベントリスニング
WV.Event.WVBluetooth.characteristicValueChanged: characteristicValue が変更されます(通知を開くには startNotifications が必要です)
var params = {
deviceId: '00:aa:bb:cc:dd',
serviceId: 'xxxx',
characteristicId: 'xxx',
value: 'xxx'
};
window.WindVane.call('WVBluetooth', 'writeValue', params, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.readValue
接続されている BLE デバイスから指定された特性値を読み取ります。
入力パラメーター
[
string]deviceId: Bluetooth デバイスのアドレスです。説明最初に、アプリを実行するデバイスを Bluetooth デバイスに接続する必要があります。
[string]serviceId: サービスの ID。ID は UUID 形式である必要があります。[string]characteristicId: 特性値の ID。ID は UUID 形式である必要があります。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd',
serviceId: 'xxxx',
characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'readValue', params, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.startNotifications
特性値の変更の通知を有効にします。
入力パラメーター
[
string]deviceId: Bluetooth デバイスのアドレス。説明最初に、アプリを実行するデバイスを Bluetooth デバイスに接続する必要があります。
[string]serviceId: サービスの ID。ID は UUID 形式である必要があります。[string]characteristicId: 特性値の ID。ID は UUID 形式である必要があります。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd',
serviceId: 'xxxx',
characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'startNotifications', params, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}WVBluetooth.stopNotifications
特性値の変更の通知を無効にします。
入力パラメーター
[
string]deviceId: Bluetooth デバイスのアドレスです。説明最初に、アプリを実行するデバイスを Bluetooth デバイスに接続する必要があります。
[string]serviceId: サービスの ID。ID は UUID 形式である必要があります。[string]characteristicId: 特性値の ID。ID は UUID 形式である必要があります。
コールバックパラメーター
成功コールバックのパラメーター:
コールバックパラメーターはありません。
失敗コールバックのパラメーター:
[string]msg: エラーメッセージ。
var params = {
deviceId: '00:aa:bb:cc:dd',
serviceId: 'xxxx',
characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'stopNotifications', params, function(e) {
}, function(e) {
alert('failure: ' + JSON.stringify(e));
}