Getting Innovative with Bluetooth Scanners

At Shockoe, we are given the opportunity to work on innovative projects that push to incorporate the latest technologies. Upon entering Shockoe, my first assignment was to develop a native module to allow bluetooth barcode scanners to pair and interact with an android device via the app. As this was my first time jumping into both titanium and java, I was thoroughly confused on how to accomplish my assignment.


Through scavenging together documentation and filtering through previous projects, I somehow got the job done. For those of you who may be new to titanium and modules, here are some useful tips and broken down concepts that I used to make module development easier.


Creating a shell script for building and packaging your module

*Primarily for CLI users


If you’ve done module development before, you know it can be a chore to integrate your module into your titanium project. You have to build the module, delete it from your titanium project if it is already installed, uncompress the module into your project and install it locally.


Having to do this process over and over while testing a module can take up an unbelievable chuck of your development time. Luckily for me, my co-worker Eric, a developer who I was collaborating with to create the module, came up with the idea to use a shell script to build our module and integrate it into the titanium project for us.


Here is an example of the script:


#Remove the current module from the project folder
rm -r ~/Files/Projects/BlueToothFolder/BluetoothDemo/modules/android/com.shockoe.bluetoothModuleDemo

#Navigate to the module project folder
cd ~/Files/Projects/BlueToothFolder/modules/bluetoothModuleDemo/android

#Clean the folder
ant clean

#Run ant to build the project

#Unzip the created folder
cd dist/
unzip -o

#Move the unzipped folder into the Titanium folder
cd modules/android

mv com.shockoe.bluetoothModuleDemo/ ~/Files/Projects/BlueToothFolder/BluetoothDemo/modules/android

#Alert when finish and return back to the project folder

echo "Finished"

cd ~/Files/Projects/BlueToothFolder/BluetoothDemo/ 

#Build the project to your connected device

appc run -p android -T device

This script saved me countless hours of having to re-build and install my tests while developing the module. Feel free to use it and edit it to your needs!


Exposing Methods

To allow your titanium project to access methods in your module, you can expose methods through the @Kroll.method annotation.

A simple example:

public String returnstring(String testString){
	return testString;

Then in your index.js:


// Require the module into your controller
// This particular module is Android only
	var blueToothDemo = require('com.shockoe.bluetoothModuleDemo');
console.log(blueToothDemo.returnstring(‘Hello World!’));

And as expected, ‘Hello World’ is printed to our console.


Here is a simple example that I used in the bluetooth module:

// Expose the method
public void enableBluetooth(){
	// Determine if bluetooth is enabled or not on the device
		// If bluetooth is not enabled, enable it now
		Log.d(TAG, "Bluetooth Enabled”);

Now in our controller we will be able to directly call the enableBluetooth function:


// Call the enableBlutooth function in the module


Firing Events

Just like how you can trigger events in Javascript, you can trigger events in the module’s Proxy through its built-in event management with fireEvent and hasListeners.

A simple example:

// Some method that gets called to fire a string after it is given data
private void exampeFire(String data){
	if(data == null)
	this.fireEvent("sendData", data);

In your controller, you would receive the data listen for the data like this:


// Listen for emitter from the bluetooth module
blueToothDemo.addEventListener(‘sendData’, obtainedData);

function obtainedData(data){
	console.log(‘received the following data: ‘ + data);

In a larger concept, this is how I used fireEvent within the bluetooth module:

// Using the BroadcastReceiver for registering when a bluetooth device is found.
    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Message msg = Message.obtain();
            String action = intent.getAction();

            	bluetoothDevice        = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            	BluetoothClass blclass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
            	int Majorclass = blclass.getMajorDeviceClass();
            	int minorclass = blclass.getDeviceClass();
                deviceList = new KrollDict();
                catch (Exception e) {
                    Log.w(TAG, "devicesFound exception: "+e.getMessage());


// Fire the event with our deviceList to our titanium project
private void devicesFound(){    	
    	this.fireEvent("devicesFound", deviceList);     			

And just like in the simple example, we will listen for the event in our controller:


// Listen for emitter from the bluetooth module
blueToothDemo.addEventListener(‘devicesFound’, devicesFound);

var devicesFound = function(e){

Our deviceList from the module’s devicesFound method will be represented by e. If all went well, then console.log( should print the bluetooth device’s name into our logs!

Exposing methods and firing events was the bulk of the information that I needed to comprehend to get started on my bluetooth module. Hopefully this helps a titanium and module novice get their start on their project as well.


Small Tips

  • If you’re new to modules, I would play around with Appcelerator’s ti.moddevguide on github. To reach the module code, follow the path in the src folder. To see how the module can be implemented into a titanium project, check out example/navigator.js.
  • Find or develop a native method of what you would like your module to do and break it down to be usable in a module. Determine what methods to expose and where events need to be fired
  • Read Appcelerator’s Module Architecture Guide. The docs do an excellent job at detailing the various ways to expose methods and handle properties.
  • Specifically for bluetooth, read Android’s developers guide for bluetooth development
Jay Soumphont

Jay Soumphont

July 5, 2016

Jay Soumphontphakdy is a Cross-Platform Mobile App developer who enjoys working in a transdisciplinary manner— collaborating and wearing hats between roles for design, development, and QA. He is dedicated to continuing his education to create high-quality user-friendly applications, recently becoming an ICAgile Certified Professional in Agile Testing and Test Automation.

More like this delivered right to you:

Sign up for our Newsletter to get our latest posts plus invitations to our events and access to future whitepapers.

Related Posts

Seeing the Member Experience in a “User-Experience” World

Seeing the Member Experience in a “User-Experience” World

In the world of product development, we are often always saying “user” _______. User-centered design, user analytics, user personas, user journeys, etc, etc.It’s a term that holds importance because it is a genuine focus on the end-user experience.  It’s also often...

7 Tips for Utilizing Amazon Alexa to Engage with Customers

7 Tips for Utilizing Amazon Alexa to Engage with Customers

Amazon first released Alexa virtual assistant and smart speaker Echo in late 2014. An in-home virtual assistant is an impressive tool, but creating a seamless user experience with it can be a challenge. So how do companies overcome this challenge? What engagement can...

Ready to drop us a line?