Skip to content

Commit

Permalink
Missing Annotations Added, BT UUID issue Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
EmHaseeb committed May 29, 2023
1 parent c9c945f commit 72c3b25
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import com.emh.thermalprinter.exceptions.EscPosConnectionException;

import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

public class BluetoothConnection extends DeviceConnection {

private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

private BluetoothDevice device;
private BluetoothSocket socket = null;

Expand Down Expand Up @@ -79,11 +82,16 @@ public BluetoothConnection connect() throws EscPosConnectionException {
/**
* Get bluetooth device UUID
*/
@SuppressLint("MissingPermission")
protected UUID getDeviceUUID() {
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice - "00001101-0000-1000-8000-00805f9b34fb" SPP UUID
ParcelUuid[] uuids = device.getUuids();
return (uuids != null && uuids.length > 0) ? uuids[0].getUuid() : UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
if (uuids != null && uuids.length > 0) {
if (Arrays.asList(uuids).contains(new ParcelUuid(BluetoothConnection.SPP_UUID))) {
return BluetoothConnection.SPP_UUID;
}
return uuids[0].getUuid();
} else {
return BluetoothConnection.SPP_UUID;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.emh.thermalprinter.connection.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import androidx.annotation.Nullable;

import java.util.Set;

public class BluetoothConnections {
Expand All @@ -19,6 +22,8 @@ public BluetoothConnections() {
* Get a list of bluetooth devices available.
* @return Return an array of BluetoothConnection instance
*/
@SuppressLint("MissingPermission")
@Nullable
public BluetoothConnection[] getList() {
if (this.bluetoothAdapter == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;

import androidx.annotation.Nullable;
import com.emh.thermalprinter.exceptions.EscPosConnectionException;

public class BluetoothPrintersConnections extends BluetoothConnections {
Expand All @@ -13,6 +13,7 @@ public class BluetoothPrintersConnections extends BluetoothConnections {
*
* @return a EscPosPrinterCommands instance
*/
@Nullable
public static BluetoothConnection selectFirstPaired() {
BluetoothPrintersConnections printers = new BluetoothPrintersConnections();
BluetoothConnection[] bluetoothPrinters = printers.getList();
Expand All @@ -36,6 +37,7 @@ public static BluetoothConnection selectFirstPaired() {
* @return an array of EscPosPrinterCommands
*/
@SuppressLint("MissingPermission")
@Nullable
public BluetoothConnection[] getList() {
BluetoothConnection[] bluetoothDevicesList = super.getList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public TcpConnection(String address, int port) {
/**
* Create un instance of TcpConnection.
*
* Overload of the above function TcpConnection()
* Include timeout parameter in milliseconds.
*
* @param address IP address of the device
* @param port Port of the device
* @param timeout Timeout in milliseconds to establish a connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import androidx.annotation.Nullable;

import java.util.Collection;

public class UsbConnections {
Expand All @@ -22,6 +24,7 @@ public UsbConnections(Context context) {
* Get a list of USB devices available.
* @return Return an array of UsbConnection instance
*/
@Nullable
public UsbConnection[] getList() {
if (this.usbManager == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;

import androidx.annotation.Nullable;

public class UsbDeviceHelper {
/**
* Find the correct USB interface for printing
*
* @param usbDevice USB device
* @return correct USB interface for printing, null if not found
*/
@Nullable
static public UsbInterface findPrinterInterface(UsbDevice usbDevice) {
if (usbDevice == null) {
return null;
Expand All @@ -32,6 +35,7 @@ static public UsbInterface findPrinterInterface(UsbDevice usbDevice) {
* @param usbInterface USB interface
* @return Input endpoint or null if not found
*/
@Nullable
static public UsbEndpoint findEndpointIn(UsbInterface usbInterface) {
if (usbInterface != null) {
int endpointsCount = usbInterface.getEndpointCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;

import androidx.annotation.Nullable;

import com.emh.thermalprinter.exceptions.EscPosConnectionException;

public class UsbPrintersConnections extends UsbConnections {
Expand All @@ -22,6 +24,7 @@ public UsbPrintersConnections(Context context) {
*
* @return a UsbConnection instance
*/
@Nullable
public static UsbConnection selectFirstConnected(Context context) {
UsbPrintersConnections printers = new UsbPrintersConnections(context);
UsbConnection[] bluetoothPrinters = printers.getList();
Expand All @@ -39,6 +42,7 @@ public static UsbConnection selectFirstConnected(Context context) {
*
* @return an array of UsbConnection
*/
@Nullable
public UsbConnection[] getList() {
UsbConnection[] usbConnections = super.getList();

Expand Down

0 comments on commit 72c3b25

Please sign in to comment.