Bluetooth JavaScript Functions
You may use the KBBluetooth namespace when communicating with a bluetooth device, note you can communicate with more than just printers using SPP (serial port profile) over bluetooth.

You can find the JavaScript test page at https://www.android-kiosk.com/jstest.htm Just set this as the Kiosk URL on your device to test the functions.

Standard Communication

sendBytes(string bytes)
Send bytes directly to the connected bluetooth device. Returns false if device is not connected.

<script>
function sendBytes(bytes) {
KBBluetooth.sendBytes(bytes);
}
</script>


sendBytesCharset(string bytes, string charset)
Experimental Function - Send bytes directly to the connected bluetooth device, when string is converted to bytes the supplied charset will be used. UTF-8 is default in sendBytes(string bytes). Valid charset values can be found at https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html (not an exhaustive list). Returns false if device is not connected.

<script>
function sendBytes(bytes, charset) {
KBBluetooth.sendBytes(bytes, charset);
}
</script>


sendHexString(string hex)
Experimental Function - Send hex string, Kiosk Browser will automatically convert this into a byte array and send the data to the bluetooth device.

<script>
function sendHexString(hex) {
KBBluetooth.sendHexString(hex);
}
</script>


Connecting/Hardware

Enable/Disable Bluetooth
Enables or disables Bluetooth, requires version 2.5.7 or higher.

<script>
function setBluetoothEnabled(enabled) {
KBBluetooth.setWiFiEnabled(enabled);
}
</script>
<a href="javascript:setBluetoothEnabled(false);">Disable Bluetooth</a>
<a href="javascript:setBluetoothEnabled(true);">Enable Bluetooth</a>


Bluetooth Connect
Method 1. Connects to default device (specified in settings).
Method 2. Connects to specified device.

Connects to the specified bluetooth device via mac address, the device must be paired first. Please note this function is asynchronous, you should use isConnected() & isConnecting() to check whether the device is connected.

<script>
function bluetoothConnect() {
KBBluetooth.connect();
}
function bluetoothConnectTo(macaddress) {
KBBluetooth.connectTo(macaddress);
}
</script>
<a href="javascript:bluetoothConnect(null);">Bluetooth Connect</a>
<a href="javascript:bluetoothConnectTo('0F:03:E0:A0:A2:C3');">Bluetooth ConnectTo</a>


Bluetooth Disconnect
Disconnects from current device.

<script>
function bluetoothDisconnect() {
KBBluetooth.disconnect();
}
</script>
<a href="javascript:bluetoothDisconnect();">Bluetooth Disconnect</a>


Bluetooth Disconnect
Disconnects the connected bluetooth device. Generally you should call disconnect() after completing your sendbytes() operation, otherwise the device will remain connected and may impact battery life.

<script>
function bluetoothDisconnect() {
KBBluetooth.disconnect();
}
</script>
<a href="javascript:bluetoothDisconnect();">Bluetooth Disconnect</a>


Is Bluetooth Connected?
Returns state of bluetooth connection (true or false).

<script>
function bluetoothConnected() {
return KBBluetooth.isConnected();
}
</script>
<a href="javascript:console.log(bluetoothConnected());">Is Bluetooth connected to a device?</a>


Is Bluetooth Connecting?
Returns true if bluetooth is connecting to a device.

<script>
function bluetoothIsConnecting() {
return KBBluetooth.isConnecting();
}
</script>
<a href="javascript:console.log(bluetoothIsConnecting());">Is Bluetooth connecting to a device?</a>


Get Paired Devices
Returns JSON array of all paired bluetooth devices. i.e. [{"mAddress":"0F:03:E0:A0:A2:C3"}]. Note this does not mean the bluetooth device is available, just that it has been previously paired with the device.

<script>
function getPairedDevices() {
var devices = KBBluetooth.getPairedDevices();
console.log(devices);
}
</script>
<a href="javascript:getPairedDevices();">Get all paired devices (log to console)</a>


Get Paired Devices (detailed)
Returns detailed JSON array of all paired bluetooth devices. i.e. [{"address":"0F:03:E0:A0:A2:C3","name:printer x"},{"address":"0F:03:E0:A0:A2:C4","name:printer y"}]. Note this does not mean the bluetooth device is available, just that it has been previously paired with the device.
Requires version 2.5.7 or higher.

<script>
function getPairedDevicesDetailed() {
var devices = KBBluetooth.getPairedDevicesDetailed();
console.log(devices);
}
</script>
<a href="javascript:getPairedDevicesDetailed();">Get all paired devices - detailed (log to console)</a>

Samples

Print Image
Prints image (hex).

<script>
function printImage() {
sendHexString('1d7630003000200000000300000000000030000fc00000000000000000000000000030000000000000000001f8000000300000000000000000000f000000000000f0000fc000000000000000000000000000f0000000000000000001f8000000f00000000000000000001f000000000001f0000fc000000000000000000000000001f0000000000000000001f8000001f00000000000000000003f000000000003f0000fc000000000000000000000000003f0000000000000000001f8000003f00000000000000000003f000000000003f0000fc000000000000000000000000003f0000000000000000001f8000003f00000000000000000003f000000000003f000000000000000000000000000000003f000000000000000000000000003f0000000000000000000ffe03fc003fc0ffe000fc3e7f07e003fe003f3e00ff0000ffe01fe00001f3f007df1f87cfe0ffe000000000000000000ffe07fe00fff0ffe000fc3fff9ff00fff00fffe01ff8000ffe07ff80001fffc07ff9f87fff0ffe000000000000000000ffe1fff81fff8ffe000fc3ffffff81fff81fffe07ffe000ffe0fffc0001fffe07ff9f87fff8ffe000000000000000000ffe1fff83fffcffe000fc3ffffffc1fffc3fffe07ffe000ffe1fffe0001ffff07ff1f87fffcffe000000000000000000ffe3fffc3f9fcffe000fc3ffffffc3fffc3fffe0ffff000ffe3ffff0001ffff07ff1f87fffcffe0000000000000000003f03f9fc3f0f03f0000fc3fcff9fc3f1fc3f9fe0fe7f0003f03f87f0001fe7f07ff1f87f9fc3f00000000000000000003f07f0fe3fc003f0000fc3f87f0fc0e0fc7f0fe1fc3f8003f07f03f8001fc3f87f01f87f0fc3f00000000000000000003f07e07e3ff803f0000fc3f07e0fc003fc7e07e1f81f8003f07f03f8001f81f87f01f87e0fc3f00000000000000000003f07fffe3fff03f0000fc3f07e0fc03ffc7e07e1ffff8003f07e01f8001f81f87e01f87e0fc3f00000000000000000003f07fffe1fff83f0000fc3f07e0fc1fffc7e07e1ffff8003f07e01f8001f81f87e01f87e0fc3f00000000000000000003f07fffe0fffc3f0000fc3f07e0fc3fffc7e07e1ffff8003f07e01f8001f81f87e01f87e0fc3f00000000000000000003f07fffe01ffc3f0000fc3f07e0fc7fefc7e07e1ffff8003f07e01f8001f81f87e01f87e0fc3f00000000000000000003f07e000003fc3f0000fc3f07e0fc7f0fc7e07e1f8000003f07f03f8001f81f87e01f87e0fc3f00000000000000000003f07f0001e0fc3f0000fc3f07e0fc7e0fc7f0fe1fc000003f07f03f8001fc3f87e01f87e0fc3f00000000000000000003f03f8f87f1fc3f0000fc3f07e0fc7f3fc3f9fe0fe3e0003f03f87f0001fe7f07e01f87e0fc3f00000000000000000003fe3fffe7fffc3fe000fc3f07e0fc7fffc3fffe0ffff8003fe3ffff0001ffff07e01f87e0fc3fe0000000000000000003fe1fffe3fff83fe000fc3f07e0fc7fffc3fffe07fff8003fe1fffe0001ffff07e01f87e0fc3fe0000000000000000003fe1fffc3fff83fe000fc3f07e0fc3fffc1fffe07fff0003fe0fffc0001fffe07e01f87e0fc3fe0000000000000000001fe0fff80fff01fe000fc3f07e0fc1ff7e0fffe03ffe0001fe07ff80001fffc07e01f87e0fc1fe0000000000000000000fc03fe003fc00fc000fc3f07e0fc0fc7e03f7e00ff80000fc01fe00001f9f007e01f87e0fc0fc00000000000000000000000000000000000000000000000000003c07e00000000000000000001f80000000000000000000000000000000000000000000000000000000000000000000003f0fe00000000000000000001f80000000000000000000000000000000000000000000000000000000000000000000003fffc00000000000000000001f80000000000000000000000000000000000000000000000000000000000000000000003fffc00000000000000000001f80000000000000000000000000000000000000000000000000000000000000000000001fffc00000000000000000001f80000000000000000000000000000000000000000000000000000000000000000000000fff800000000000000000001f800000000000000000000000000000000a');
}
</script>
<a href="javascript:printImage();">Print Image via Hex String</a>


Bluetooth Connect & Print Test
Returns paired bluetooth devices.

<script>
function connectPrint() {
if (KBBluetooth.isConnected()) {
receiptPrint();
}
else {
showAndroidToast("Default bluetooth device not connected! Attempting re-connect");
KBBluetooth.connect();
//attempt print in 5 seconds
setTimeout(receiptPrint, 5000)
}
}

function receiptPrint() {
KBBluetooth.sendBytes(RESET); //CLEAR ANY PREVIOUS FORMATTING ETC
KBBluetooth.sendBytes('Printer Testing\n');
KBBluetooth.sendBytes('sendBytes() test\n');
//KBBluetooth.sendBytes(DOUBLE_HEIGHT_ON);
//KBBluetooth.sendBytes('Double Height On\n');
//KBBluetooth.sendBytes(DOUBLE_HEIGHT_OFF);
//KBBluetooth.sendBytes('Double Height Off\n');
//KBBluetooth.sendBytes(EMPHASIZE_ON);
//KBBluetooth.sendBytes("EMPHASIZE ON\n");
//KBBluetooth.sendBytes(EMPHASIZE_OFF);
//KBBluetooth.sendBytes("EMPHASIZE OFF\n");
//KBBluetooth.sendBytes(UNDERLINE_ON);
//KBBluetooth.sendBytes("UNDERLINE ON\n");
//KBBluetooth.sendBytes(UNDERLINE_DOUBLE);
//KBBluetooth.sendBytes("UNDERLINE DOUBLE\n");
//KBBluetooth.sendBytes(UNDERLINE_OFF);
//KBBluetooth.sendBytes("UNDERLINE OFF\n");
//KBBluetooth.sendBytes(DOUBLESTRIKE_ON);
//KBBluetooth.sendBytes("DOUBLESTRIKE ON\n");
//KBBluetooth.sendBytes(DOUBLESTRIKE_OFF);
//KBBluetooth.sendBytes("DOUBLESTRIKE OFF\n");
//KBBluetooth.sendBytes(CENTER_JUSTIFIED);
//KBBluetooth.sendBytes("CENTER_JUSTIFIED\n");
//KBBluetooth.sendBytes(RIGHT_JUSTIFIED);
//KBBluetooth.sendBytes("RIGHT_JUSTIFIED\n");
//KBBluetooth.sendBytes(FEED_3_LINES);
KBBluetooth.disconnect();
}

function sendBytes(bytes) {
KBBluetooth.sendBytes(bytes);
}

function sendHexString(hex) {
KBBluetooth.sendHexString(hex);
}
var RESET = "\x1B\x40";
var DOUBLE_HEIGHT_ON = "\x1B\x21\x10";
var DOUBLE_HEIGHT_OFF = "\x1B\x21\x00";
var UNDERLINE_ON = "\x1B\x2D\x01";
var UNDERLINE_DOUBLE = "\x1B\x2D\x02";
var UNDERLINE_OFF = "\x1B\x2D\x00";
var EMPHASIZE_ON = "\x1B\x45\x01";
var EMPHASIZE_OFF = "\x1B\x45\x02";
var DOUBLESTRIKE_ON = "\x1B\x47\x01"
var DOUBLESTRIKE_OFF = "\x1B\x47\x00"
var LEFT_JUSTIFIED = "\x1B\x61\x00";
var CENTER_JUSTIFIED = "\x1B\x61\x01";
var RIGHT_JUSTIFIED = "\x1B\x61\x02";
var FEED_3_LINES = "\x1B\x64\x03";
var REVERSE_FEED_5_LINES = "\x1B\x65\x05";
</script>
<a href="javascript:connectPrint();">Bluetooth Print Test</a>
Was this article helpful?
Thank you!