Chrome Bluetooth API 呼び出しを実行しようとしていますが、typescript で成功しません。私はすでに html () でファイルを適用しようとしましたが、役に立ちませんでした。しかし、コードをコピーしてブラウザー コンソールに貼り付けると、コードは完全に実行されます:(
コンソールのエラー (Ts でコードを実行する場合):
DOMException: Must be handling a user gesture to show a permission request.
// Typescript/TS:
requestBluetooth() {
(window.navigator as any).bluetooth.requestDevice({ filters: [{ services: [this.PRINT_SERVICE_CODE] }] })
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(this.PRINT_SERVICE_CODE);
})
.then(service => {
return service.getCharacteristic('00002af1-0000-1000-8000-00805f9b34fb');
})
.then((characteristic) => {
this.sendTextData(characteristic);
})
.catch(error => console.log(error));
}
sendTextData(characteristic) {
// Get the bytes for the text
const encoder = new TextEncoder('UTF-8');
const text = encoder.encode('TESTETSETS' + '\u000A\u000D');
return characteristic.writeValue(text)
.then(() => console.log('Write done.'));
}
// コンソール ブラウザでの作業:
const PRINT_SERVICE_CODE = '000018f0-0000-1000-8000-00805f9b34fb';
const sendTextData = (function (characteristic) {
const encoder = new TextEncoder('UTF-8');
const text = encoder.encode('TESTETSETS' + '\u000A\u000D');
return characteristic.writeValue(text)
.then(() => console.log('Write done.'));
})
const request = (function () {
navigator.bluetooth.requestDevice({ filters: [{ services: [PRINT_SERVICE_CODE] }] })
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(PRINT_SERVICE_CODE);
})
.then(service => {
return service.getCharacteristic('00002af1-0000-1000-8000-00805f9b34fb');
})
.then((characteristic) => {
this.sendTextData(characteristic);
})
.catch(error => console.log(error));
})
request();
// プロジェクトの情報:
Angular CLI: 6.0.8
ノード: 8.11.3
OS: Linux x64
角度: 6.0.9