Jump to content

Recommended Posts

  • Moderators

How to connect webapp to a bluetooth enabled printer.

Basically there are 2 avenues to do that : a) going native and b) the web bluetooth api

Going native

This involves using PhoneGap or Cordova, plus installing a plugin to make the bluetooth capacities of the mobile target platform available in the js world.

I found this plugin on github, a cordova plugin, so used cordova rather than phonegap.

The install sequence for a clean install of everything looks something like

see https://cordova.apache.org/docs/en/latest/guide/cli/
  install node
  install npm
  install cordova : npm install -g cordova

install pre-requisites : see https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html#requirements-and-support
  java development kit (jdk)
  android studio / android sdk 

set environment and path variables

create app structure
  create app : cordova create bluetoothprinter com.smartmobilestudio.bluetoothprinter BluetoothPrinter

install plugin
  cordova plugin add https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer.git

create remainder of app
  add platform : cordova platform add android
  create SMS project
  copy SMS project output into the cordova app structure (www directory)
  build app : cordova build android

install apk file on android

Amazingly this all works out.

Some gotcha's :

- environment and path variables don't allow spaces, so for instance .../Program Files/... has to be shortened to .../Progra~1/...

- cordova now requires plugins to have their own package.json file. The selected plugin doesn't have that, so I had to download the git repository, create a package.json file and install the plugin from local (cordova plugin add cordova-plugin-bluetooth-printer --save --searchpath ../plugin)

- the SMS project needs these entries in its template index file

  <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
  <script type="text/javascript" charset="utf-8" src="BluetoothPrinter.js"></script>

and compile may end up in errors. However the cordova build process takes care of that and produces a viable apk file.

It is a bit of a process to go through, but works out well in the end.


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...