- Using Bluetooth Scanner API
- Creating The Project
- Enabling Android Permissions
- Adding Some Code
- Running the Application
- Important Programming Tips
- What's Next
- Saved searches
- Use saved searches to filter your results more quickly
- tutsplus/Android-BluetoothScannerFinishedProject
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Using Bluetooth Scanner API
This guide will walk you through creating an EMDK For Android application that will use Bluetooth Scanner API introduced in EMDK V 3.1, to perform pairing with Bluetooth Scanning device using Bluetooth Pairing Utility of Bluetooth Scanner API.
Initially, the Bluetooth Scanner is not connected to the device. So the Bluetooth Pairing Utility would turn ON the Bluetooth of the device where the app is running (Ex. TC55) if not already turned ON. Once that is done, the utility will ask you to scan a Barcode in the app using Bluetooth Scanner (Ex. RS507). After scanning the Barcode, the utility will start pairing process and pair the Bluetooth Scanning device (Ex. RS507) with your device where the app is running (Ex. TC55). In this tutorial, we will explore the Bluetooth Scanner API by using it for developing a basic application that will pair the device with Bluetooth Scanner by allowing Bluetooth Scanner to scan the Barcode in the app through Bluetooth Pairing Utility.
Note: The Bluetooth Scanner API does not use Profile Wizard to pair with Bluetooth Scanner and everything needs to be configured programmatically through code.
Creating The Project
Enabling Android Permissions
- Modify the Application’s Manifest.xml to use the EMDK library and to set permission for the EMDK to scan the barcodes. You must first enable permissions for ‘com.symbol.emdk.permission.EMDK’:
When done, your manifest.xml should look like:
Adding Some Code
import com.symbol.emdk.EMDKManager; import com.symbol.emdk.barcode.BarcodeManager;
Then you must make the activity to implement EMDKListener. After that you also need to implement BarcodeManager.ScannerConnectionListener, which is an interface to notify the client when the scanner device (Ex. RS507) has been connected or disconnected to the mobile device (Ex. TC55). Override its onConnectionChange method, which would be called when the scanner gets connected or disconnected to the mobile device.
public class MainActivity extends Activity EMDKManager.EMDKListener, BarcodeManager.ScannerConnectionListener <.. .. .. .. .. .. . @Override public void onOpened(EMDKManager emdkManager) < >@Override public void onClosed() < >@Override public void onConnectionChange(ConnectionStatus connectionStatus)We will now create some global variables to hold the instance objects of EMDKManager and BarcodeManager. These variables would be used throughout the code. We will then add some UI elements, which is just a TextView for notifying intermediate status operations performed during pairing.
// Text View to display status during pairing operation private TextView statusView = null; // Declare a variable to store EMDKManager object private EMDKManager emdkManager = null; // Declare a variable to store Barcode Manager object private BarcodeManager barcodeManager = null; // Declare a variable to hold scanner device to scan private Scanner scanner = null;
The code till here looks like:
Now, let us design a simple UI that has simply has a TextView to indicate status during pairing operation. So, remove all the code, inside «res/layout/activity_main.xml» and add following XML layout code for UI.The layout should look like:
In the onCreate method, we take reference of UI elements that are declared in «res/layout/activity_main.xml» in order to use them in our Activity. We then call getEMDKManager so that the EMDK can be initialized and checked to see if it is ready.// Reference to UI elements statusView = (TextView) findViewById(R.id.textViewStatus); // The EMDKManager object will be created and returned in the callback. EMDKResults results = EMDKManager.getEMDKManager( getApplicationContext(), this); // Check the return status of getEMDKManager and update the status Text // View accordingly if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS)So the complete onCreate method looks like:
Get the EMDK Manager in the onOpened method, update the StatusView TextView with a message and add the ScannerConnectionListener to get the Connected/Disconnected events between Bluetooth Scanner and Mobile by adding following code in onOpened method.// Update status view with EMDK Open Success message statusView.setText("Status: " + "EMDK open success!"); this.emdkManager = emdkManager; // Get the Barcode Manager Instance barcodeManager = (BarcodeManager) emdkManager .getInstance(EMDKManager.FEATURE_TYPE.BARCODE); // Add the Scanner Connection Listener to receive Connected/Disconnected events if (barcodeManager != null) < barcodeManager.addConnectionListener(this); >// Initialize Scanner initScanner();
The onOpened method should look like:
You will see error as we have not added initScanner method for initializing Bluetooth Scanner of our mobile device (Ex TC55). initScanner method gets the number of supported devices in a list.List deviceList = barcodeManager .getSupportedDevicesInfo();
It then iterates through this list of devices and checks one with «Bluetooth Scanner» by refering friendly name. If the list has Bluetooth Scanner device, it initializes Scanner object with Bluetooth Scanner.
for (ScannerInfo scannerInfo : deviceList)If the Scanner Object is still null after iteration, your device doesn’t support Bluetooth Scanner. Finally, we enable the Bluetooth Scanner.
// Initialize Bluetooth Scanner private void initScanner() <
if (scanner == null) < // Get a list of supported scanner devices List<ScannerInfo> deviceList = barcodeManager .getSupportedDevicesInfo(); // Iterate through Scanner devices and check if it supports Bluetooth Scanner for (ScannerInfo scannerInfo : deviceList)< if(scannerInfo.getFriendlyName().equalsIgnoreCase("Bluetooth Scanner")) scanner = barcodeManager.getDevice(scannerInfo); >// If null, then your device does not support Bluetooth Scanner if(scanner == null) < statusView.setText("Bluetooth Scanner not supported. "); return; >else < // Supports Bluetooth Scanner try < // Enable the Scanner scanner.enable(); >catch (ScannerException e) < statusView.setText("Status: " + e.getMessage()); >> >
This should look like:
Now, we will add a method to de-initialize the Scanner named deInitScanner . This method will perform following operations in the same sequence.
- Cancel any pending read operations.
- Disable the Scanner.
- Release the Scanner resource.
- Eventually set the Scanner object to null .
The source code for deInitScanner method should look like:
// DeInitialize Scanner private void deInitScanner() < if (scanner != null) < try < // Cancel pending reads scanner.cancelRead(); // Disable Scanner scanner.disable(); // Release Scanner scanner.release();
> catch (ScannerException e) < statusView.setText("Status: " + e.getMessage()); >scanner = null; >
The method should look like:
// AsyncTask for Updating Status in statusView during pairing operation private class AsyncStatusUpdate extends AsyncTask <
@Override protected String doInBackground(String. params) < return params[0]; >@Override protected void onPostExecute(String result) < // Update Status View statusView.setText("Status: " + result); >
The AsyncStatusUpdate should look like:
We would now add the code to update statusView in onConnectionChange callback method. This method would be invoked when the scanner device has been connected or disconnected to the mobile device. So we will first get the connection state and friendly name of the Scanner device received through onConnectionChange callback method.// Returns the Connection State for Bluetooth Scanner through callback String statusBT = connectionStatus.getConnectionState().toString(); // Returns the Friendly Name of the Scanner through callback String scannerNameBT = connectionStatus.getScannerInfo() .getFriendlyName();
If the friendly name is "Bluetooth Scanner", then we would update the statusView with the Scanner Name and its state. Later, if the state is CONNECTED , we would invoke initScanner method and deInitScanner method for the DISCONNECTED state. So the source code for onConnectionChange callback method with the above mentioned steps would look like:
@Override public void onConnectionChange(ConnectionStatus connectionStatus) < String status = ""; String scannerName = "";
// Returns the Connection State for Bluetooth Scanner through callback String statusBT = connectionStatus.getConnectionState().toString(); // Returns the Friendly Name of the Scanner through callback String scannerNameBT = connectionStatus.getScannerInfo() .getFriendlyName(); // Get the friendly name of our device's Scanner scannerName = scanner.getScannerInfo().getFriendlyName(); // Check for the Bluetooth Scanner if (scannerName.equalsIgnoreCase(scannerNameBT)) < // If Bluetooth Scanner, update the status view status = scannerNameBT + ":" + statusBT; new AsyncStatusUpdate().execute(status); // Initialize or De-Initialize Bluetooth Scanner // device based on Connection State switch (connectionStatus.getConnectionState()) < case CONNECTED: // Initialize Scanner initScanner(); break; case DISCONNECTED: // De-Initialize Scanner deInitScanner(); break; >>
The onConnectionChange method should look like:
Finally, release all the resources in onClosed method. So it would remove the ScannerConnectionListener and release the EMDKManager using following code.@Override public void onClosed() < if (emdkManager != null) < // Remove the connection listener if (barcodeManager != null) < barcodeManager.removeConnectionListener(this); >// Release EMDK Manager emdkManager.release(); emdkManager = null; > statusView .setText("Status: " + "EMDK closed unexpectedly! Please close and restart the application."); >
The onClosed method should look like:
That's it. We are done with all the coding part that will let us perform pairing with Bluetooth Scanning device using Bluetooth Pairing Utility of Bluetooth Scanner API introduced in EMDK V 3.1. Now let us run the application.
Running the Application
Note: Initially the bluetooth of the device is turned OFF.
So it displays a Toast saying bluetooth scanner is not connected, which would start Bluetooth Pairing Utility to pair devices.
- It will first ask you to enable the Bluetooth of the device.
- Click "Allow" and the Bluetooth Scanning Utility would turn your device's Bluetooth ON.
- It would display a screen with a Barcode, which needs to be scanned to pair with this device.
- You can take a Bluetooth Scanning device (Ex RS507) and scan the Barcode shown on the screen of your app (App is on TC55) and Bluetooth Scanning Utility would pair these two devices.
- You can see a Toast confirming that the two devices (TC55 & RS507) have been paired and connected to each other along with the updated status on statusView .
- Now, lets check under Bluetooth Settings of the Mobile device (TC55) for further confirmation. So go to device's Settings -> Bluetooth.
- You can see RS507 under Paired devices that further confirms pairing. This is how we can perform pairing of Mobile device (Ex. TC55) with Bluetooth Scanning device (Ex. RS507) using Bluetooth Pairing Utility of Bluetooth Scanner API
Important Programming Tips
What's Next
Now that you have learned how to perform pairing with Bluetooth Scanning device using Bluetooth Pairing Utility of Bluetooth Scanner API, in the next tutorial we would concentrate on ScanAndPair APIs and develop an application to demonstrate its use.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Create a Bluetooth Scanner With Android's Bluetooth API
tutsplus/Android-BluetoothScannerFinishedProject
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Tuts+ Tutorial: Create a Bluetooth Scanner With Android's Bluetooth API
In this tutorial, we explore what Bluetooth is and how to use the Android Bluetooth API to create an app that scans and displays nearby Bluetooth devices. Additionally, we look over the basics of connecting with a nearby Bluetooth device.
Read this tutorial on Tuts+
About
Create a Bluetooth Scanner With Android's Bluetooth API