Last active
June 25, 2024 09:29
-
-
Save tresf/f5ffb9445072dcd468209596de933202 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://github.com/WebBluetoothCG/demos/blob/gh-pages/bluetooth-printer/index.html | |
var SERVICE = '000018f0-0000-1000-8000-00805f9b34fb'; | |
var WRITE = '00002af1-0000-1000-8000-00805f9b34fb'; | |
var DATA = '' | |
+ '\x1B' + '\x61' + '\x31' // center align | |
+ '\x1D' + '\x21' + '\x11' + 'Hello\nBluetooth!\n\n' // double font size | |
+ '\x1D' + '\x21' + '\x00' + '... from your friends\nat https://qz.io' // normal font size | |
+ '\n\n\n\n\n\n\n'; // feed paper | |
var deviceHandle; | |
navigator.bluetooth.requestDevice({ filters: [{ services: [SERVICE]}] }).then(device => { | |
console.log(device); | |
deviceHandle = device; | |
return device.gatt.connect() | |
}).then(server => { | |
console.log(server); | |
return server.getPrimaryService(SERVICE); | |
}).then(service => { | |
console.log(service); | |
return service.getCharacteristic(WRITE); | |
}).then(channel => { | |
console.log(channel); | |
return channel.writeValue(new TextEncoder("utf-8").encode(DATA)); | |
}).catch(error => { | |
console.error(error) | |
}).finally(() => { | |
deviceHandle.gatt.disconnect(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much! It's help a lot!