Skip to content

Commit

Permalink
adding default UUID to bluetooth connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck ALARY committed Jul 1, 2020
1 parent bf12ee2 commit 7db6792
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dantsu.escposprinter.connection.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.ParcelUuid;
Expand All @@ -8,12 +9,13 @@
import com.dantsu.escposprinter.exceptions.EscPosConnectionException;

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

public class BluetoothConnection extends DeviceConnection {

private BluetoothDevice device;
private BluetoothSocket socket = null;

/**
* Create un instance of BluetoothConnection.
*
Expand All @@ -23,7 +25,7 @@ public BluetoothConnection(BluetoothDevice device) {
super();
this.device = device;
}

/**
* Get the instance BluetoothDevice connected.
*
Expand All @@ -46,23 +48,27 @@ public boolean isConnected() {
/**
* Start socket connection with the bluetooth device.
*/
@SuppressLint("MissingPermission")
public BluetoothConnection connect() throws EscPosConnectionException {
if(this.isConnected()) {
if (this.isConnected()) {
return this;
}

if(this.device == null) {
if (this.device == null) {
throw new EscPosConnectionException("Bluetooth device is not connected.");
}

ParcelUuid[] uuid = this.device.getUuids();
ParcelUuid[] uuids = this.device.getUuids();
UUID uuid;

if(uuid == null || uuid.length == 0) {
throw new EscPosConnectionException("Unable to find bluetooth device UUID.");
if (uuids != null && uuids.length > 0) {
uuid = uuids[0].getUuid();
} else {
uuid = UUID.randomUUID();
}

try {
this.socket = this.device.createRfcommSocketToServiceRecord(uuid[0].getUuid());
this.socket = this.device.createRfcommSocketToServiceRecord(uuid);
this.socket.connect();
this.stream = this.socket.getOutputStream();
this.data = new byte[0];
Expand All @@ -74,21 +80,21 @@ public BluetoothConnection connect() throws EscPosConnectionException {
}
return this;
}

/**
* Close the socket connection with the bluetooth device.
*/
public BluetoothConnection disconnect() {
this.data = new byte[0];
if(this.stream != null) {
if (this.stream != null) {
try {
this.stream.close();
} catch (IOException e) {
e.printStackTrace();
}
this.stream = null;
}
if(this.socket != null) {
if (this.socket != null) {
try {
this.socket.close();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.dantsu.escposprinter.connection.bluetooth;

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

import com.dantsu.escposprinter.exceptions.EscPosConnectionException;

public class BluetoothPrintersConnections extends BluetoothConnections {

/**
* Easy way to get the first bluetooth printer paired / connected.
*
Expand All @@ -15,7 +16,7 @@ public class BluetoothPrintersConnections extends BluetoothConnections {
public static BluetoothConnection selectFirstPaired() {
BluetoothPrintersConnections printers = new BluetoothPrintersConnections();
BluetoothConnection[] bluetoothPrinters = printers.getList();

if (bluetoothPrinters != null && bluetoothPrinters.length > 0) {
for (BluetoothConnection printer : bluetoothPrinters) {
try {
Expand All @@ -27,31 +28,36 @@ public static BluetoothConnection selectFirstPaired() {
}
return null;
}


/**
* Get a list of bluetooth printers.
*
* @return an array of EscPosPrinterCommands
*/
@SuppressLint("MissingPermission")
public BluetoothConnection[] getList() {
BluetoothConnection[] bluetoothDevicesList = super.getList();
if(bluetoothDevicesList == null) {

if (bluetoothDevicesList == null) {
return null;
}

int i = 0;
BluetoothConnection[] printersTmp = new BluetoothConnection[bluetoothDevicesList.length];
for (BluetoothConnection bluetoothConnection : bluetoothDevicesList) {
BluetoothDevice device = bluetoothConnection.getDevice();
if (device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.IMAGING && device.getBluetoothClass().getDeviceClass() == 1664) {

int majDeviceCl = device.getBluetoothClass().getMajorDeviceClass(),
deviceCl = device.getBluetoothClass().getDeviceClass();

if (majDeviceCl == BluetoothClass.Device.Major.IMAGING && (deviceCl == 1664 || deviceCl == BluetoothClass.Device.Major.IMAGING)) {
printersTmp[i++] = new BluetoothConnection(device);
}
}
BluetoothConnection[] bluetoothPrinters = new BluetoothConnection[i];
System.arraycopy(printersTmp, 0, bluetoothPrinters, 0, i);
return bluetoothPrinters;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TcpConnection(String address, int port) {
* @return true if is connected
*/
public boolean isConnected() {
return this.socket != null;
return this.socket != null && this.socket.isConnected() && super.isConnected();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public UsbPrintersConnections(Context context) {
}

/**
* Easy way to get the first bluetooth printer paired / connected.
* Easy way to get the first USB printer paired / connected.
*
* @return a EscPosPrinterCommands instance
* @return a UsbConnection instance
*/
public static UsbConnection selectFirstConnected(Context context) {
UsbPrintersConnections printers = new UsbPrintersConnections(context);
Expand All @@ -40,9 +40,9 @@ public static UsbConnection selectFirstConnected(Context context) {


/**
* Get a list of bluetooth printers.
* Get a list of USB printers.
*
* @return an array of EscPosPrinterCommands
* @return an array of UsbConnection
*/
public UsbConnection[] getList() {
UsbConnection[] usbConnections = super.getList();
Expand Down

0 comments on commit 7db6792

Please sign in to comment.