I am currently developing an Android application that allows connecting to and reading RFID tags.
At the moment, the connection is successful, but I'm encountering an issue with creating the reader by calling the CreateReader method provided by Honeywell. Attached you'll find the logs I received when I clicked the button to create the reader, as well as the code of my application:
package com.example.projetrfid;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.honeywell.rfidservice.EventListener;
import com.honeywell.rfidservice.RfidManager;
import com.honeywell.rfidservice.TriggerMode;
import com.honeywell.rfidservice.rfid.Gen2;
import com.honeywell.rfidservice.rfid.RfidReader;
import com.honeywell.rfidservice.rfid.RfidReaderException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String LOG = "[IH45]";
private final String READER_STATUS = "READER_STATUS";
private final String TRIGGER_STATUS = "TRIGGER_STATUS";
private final String WRITE_TAG_STATUS = "WRITE_TAG_STATUS";
private final String BATTERY_STATUS = "BATTERY_STATUS";
private final String TAG = "TAG";
private final String TAGS = "TAGS";
private static RfidManager mRfidMgr;
private static RfidReader mRfidReader;
private static BluetoothDevice mDevice;
private static final ArrayList<String> cacheTags = new ArrayList<>();
private static boolean isSingleRead = false;
private static boolean isReading = false;
private static final ArrayList<BluetoothDevice> mDevices = new ArrayList<>();
private static BluetoothLeScanner scanner;
private static Handler handler;
private static final int BLUETOOTH_SCAN = 111;
private static final int BLUETOOTH_CONNECT = 112;
private static final int REQUEST_CODE_LOCATION_1 = 123; // Valeur arbitraire, peut être n'importe quel entier
private static final int REQUEST_CODE_LOCATION_2 = 124; // Valeur arbitraire, peut être n'importe quel entier
private static final int REQUEST_CODE_LOCATION_3 = 125; // Valeur arbitraire, peut être n'importe quel entier
private static final int REQUEST_CODE_LOCATION_4 = 126; // Valeur arbitraire, peut être n'importe quel entier
private static final int REQUEST_CODE_BLUETOOTH_CONNECT = 127; // Valeur arbitraire, peut être n'importe quel entier
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private RFIDConnectionManager mMyApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button BTN_CONNECT_ = findViewById(R.id.BTN_CONNECT);
Button BTN_CREARE_READER_ = findViewById(R.id.CREATE_READER);
Context tmpContext = getApplicationContext();
handler = new Handler(Looper.getMainLooper());
// Vérification de la permission ACCESS_FINE_LOCATION
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Demande de permission ACCESS_FINE_LOCATION à l'utilisateur
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION_1);
}
// Vérification de la permission BLUETOOTH_ADMIN
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
// Demande de permission BLUETOOTH_ADMIN à l'utilisateur
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, REQUEST_CODE_LOCATION_2);
}
// Vérification de la permission BLUETOOTH
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
// Demande de permission BLUETOOTH à l'utilisateur
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, REQUEST_CODE_LOCATION_3);
}
// Vérification de la permission WRITE_EXTERNAL_STORAGE
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Demande de permission WRITE_EXTERNAL_STORAGE à l'utilisateur
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_LOCATION_4);
}
// Vérification de la permission WRITE_EXTERNAL_STORAGE
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// Demande de permission WRITE_EXTERNAL_STORAGE à l'utilisateur
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_CODE_BLUETOOTH_CONNECT);
}
// if (mRfidMgr==null){Toast.makeText(getApplicationContext(), "avant mRfidMgr==null", Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "avant mRfidMgr!=null", Toast.LENGTH_SHORT).show();}
init();
mRfidMgr = RfidManager.getInstance(this);
// if (mRfidMgr==null){Toast.makeText(getApplicationContext(), "apres mRfidMgr==null", Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "apres mRfidMgr!=null", Toast.LENGTH_SHORT).show();}
BTN_CONNECT_.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Vérification des autorisations avant de tenter la connexion
checkPermissions();
}
});
BTN_CREARE_READER_.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mRfidMgr==null){Toast.makeText(getApplicationContext(), "apres mRfidMgr==null", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "apres mRfidMgr!=null", Toast.LENGTH_SHORT).show();
mRfidMgr.createReader();
}
}
});
}
private void checkPermissions() {
// Vérification de toutes les autorisations nécessaires
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// Demande de toutes les autorisations nécessaires
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BLUETOOTH_CONNECT
},
BLUETOOTH_CONNECT); // Utilisez une constante pour le code de demande de permission
} else {
// Toutes les autorisations sont accordées, connectez-vous
// Toast.makeText(getApplicationContext(), "connect", Toast.LENGTH_SHORT).show();
connect("BLE");
}
}
public void connect(final String name) {
// Toast.makeText(getApplicationContext(), "je suis dans connect", Toast.LENGTH_SHORT).show();
try {
// Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
mDevices.clear();
if (mRfidMgr != null && mRfidMgr.isConnected()) {
doDisconnect();
}
scanner = mBluetoothAdapter.getBluetoothLeScanner();
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, BLUETOOTH_SCAN);
Log.d("MainActivity", "PAS DE PERMISSION BLUETOOTH_CONNECT");
} else {
// Toast.makeText(getApplicationContext(), "startScan", Toast.LENGTH_SHORT).show();
scanner.startScan(scanCallback);
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, BLUETOOTH_SCAN);
Log.d("MainActivity", "PAS DE PERMISSION BLUETOOTH_CONNECT");
} else {
scanner.stopScan(scanCallback);
}
Toast.makeText(getApplicationContext(), "Failed to connect reader...Please make sure your reader is power on.", Toast.LENGTH_SHORT).show();
}
}, 8 * 1000);
}
} catch (Exception err) {
Toast.makeText(getApplicationContext(), "Exception err: "+err, Toast.LENGTH_SHORT).show();
}
}
private void doConnect() {
// Toast.makeText(getApplicationContext(), "7", Toast.LENGTH_SHORT).show();
if (mRfidMgr != null && mRfidMgr.isConnected()) {
doDisconnect();
}
if (mRfidMgr == null) {
// Toast.makeText(getApplicationContext(), "8", Toast.LENGTH_SHORT).show();
init();
}
if (mDevice != null) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Toast.makeText(getApplicationContext(), "connect"+mDevice.getAddress(), Toast.LENGTH_SHORT).show();
mRfidMgr.connect(mDevice.getAddress());
}
}, 3 * 1000);
}
}
private void doDisconnect() {
if (mRfidMgr != null) {
mRfidMgr.removeEventListener(mEventListener);
mRfidMgr.disconnect();
mRfidMgr = null;
}
if (mRfidReader != null) {
// mRfidReader.removeOnTagReadListener(dataListener);
mRfidReader = null;
}
}
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// Toast.makeText(getApplicationContext(), "4", Toast.LENGTH_SHORT).show();
BluetoothDevice device = result.getDevice();
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, BLUETOOTH_CONNECT);
// Toast.makeText(getApplicationContext(), "PAS DE PERMISSION BLUETOOTH_CONNECT", Toast.LENGTH_SHORT).show();
Log.d("MainActivity", "PAS DE PERMISSION BLUETOOTH_CONNECT");
} else {
if (device.getName() != null && !device.getName().isEmpty()) {
// Toast.makeText(getApplicationContext(), "5: "+device.getName(), Toast.LENGTH_SHORT).show();
boolean newDevice = true;
for (BluetoothDevice info : mDevices) {
if (device.getAddress().equals(info.getAddress())) {
newDevice = false;
}
}
if (newDevice && device.getName().equals("IH45")) {
// Toast.makeText(getApplicationContext(), "6: "+device.getAddress(), Toast.LENGTH_SHORT).show();
mDevices.add(device);
mDevice = device;
scanner.stopScan(scanCallback);
handler.removeCallbacksAndMessages(null);
// Toast.makeText(getApplicationContext(), "j'arrete le scan: "+device.getName(), Toast.LENGTH_SHORT).show();
doConnect();
}
}
}
}
};
private void init() {
mMyApplication = RFIDConnectionManager.getInstance();
RfidManager.create(this, new RfidManager.CreatedCallback() {
@Override
public void onCreated(RfidManager rfidManager) {
Log.d("MainActivity", "RfidManager created successfully.");
mRfidMgr = rfidManager;
if (mMyApplication != null) {
Log.e("MainActivity", "mMyApplication n'est pas null");
mMyApplication.setRfidMgr(rfidManager);
// Toast.makeText(getApplicationContext(), "setRfidManager(rfidManager)", Toast.LENGTH_SHORT).show();
// setRfidManager(rfidManager);
} else {
// Toast.makeText(getApplicationContext(), "mMyApplication = null dans onCreated dans init", Toast.LENGTH_SHORT).show();
// Gérer le cas où rfidConnectionManager est null
Log.e("MainActivity", "mMyApplication est null");
}
// mMyApplication.setRfidMgr(mRfidMgr);
mRfidMgr.addEventListener(mEventListener);
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
});
}
private final EventListener mEventListener = new EventListener() {
@Override
public void onDeviceConnected(Object o) {
mRfidMgr.createReader();
// Toast.makeText(getApplicationContext(), "Failed to connect reader...Please make sure your reader is power on.", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeviceDisconnected(Object o) {
mRfidReader = null;
}
@Override
public void onReaderCreated(boolean b, RfidReader rfidReader) {
if (rfidReader != null) {
Toast.makeText(getApplicationContext(), "onReaderCreated rfidReader != null", Toast.LENGTH_SHORT).show();
mRfidReader = rfidReader;
try {
mRfidReader.setSession(Gen2.Session.Session1);
} catch (RfidReaderException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "onReaderCreated err: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRfidTriggered(boolean state) {
}
@Override
public void onTriggerModeSwitched(TriggerMode triggerMode) {
}
};
public void setRfidManager(RfidManager rFidMng) {
mRfidMgr = rFidMng;
}
}
When I try to create the reader, I receive the RfidReader object as null in the onReaderCreated method, and below are the logs I received.
logs errors: honRfid onServicesDiscovered Service status:0
BluetoothGatt setCharacteristicNotification() - uuid: e093f3b5-00a3-a9e5-9eca-40046e0edc24 enable: true
BluetoothGatt setCharacteristicNotification() - uuid: e093f3b5-00a3-a9e5-9eca-40026e0edc24 enable: true BluetoothGatt setCharacteristicNotification() - uuid: e093f3b5-00a3-a9e5-9eca-40036e0edc24 enable: true
Accessing hidden field Landroid/bluetooth/BluetoothGatt;->mDeviceBusy:Ljava/lang/Boolean; (max-target-r, reflection, denied)
Accessing hidden field Landroid/bluetooth/BluetoothGatt;->mDeviceBusy:Ljava/lang/Boolean; (max-target-r, reflection, denied) java.lang.NoSuchFieldException: No field mDeviceBusy in class Landroid/bluetooth/BluetoothGatt; (declaration of 'android.bluetooth.BluetoothGatt' appears in /system/framework/framework.jar)
RfidService Failed to detect module type.
RfidService com.example.projetrfid I onReaderCreated() succ=false reader=null