0% found this document useful (0 votes)
7 views

Code Connect

This method handles different connection methods for Bluetooth based on the scan method preference. It checks for required permissions, then either starts a QR code scanner, RFID tag scanner, or connects to Bluetooth. For Bluetooth connection, it determines the scan method from preferences and calls the appropriate startBluetooth method, passing required parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Code Connect

This method handles different connection methods for Bluetooth based on the scan method preference. It checks for required permissions, then either starts a QR code scanner, RFID tag scanner, or connects to Bluetooth. For Bluetooth connection, it determines the scan method from preferences and calls the appropriate startBluetooth method, passing required parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

public void connectClicked(View view) {

connectStart = 0;
String bleScanMethod = prefs.getString("bleScanMethod", "Basic");
if (bleScanMethod.equals("QR_Code")) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
log("Requesting camera permission");
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CAMERA}, CAMERA_PERMISSION_RESULT);
return;
}
connectStart = System.currentTimeMillis();
log("Starting scan");
startQRCodeScanner();
} else if (bleScanMethod.equals("RFID_Tag") && !isPlayerCarded) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.NFC)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
log("Requesting nfc permission");
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.NFC}, NFC_PERMISSION_RESULT);
return;
}
connectStart = System.currentTimeMillis();
log("Starting RFID Scan Activity");
startRFIDTagScanner();
} else {
connectBluetooth();
}
}

-------------------------------
private void connectBluetooth() {
configureProcessor();
Log.d("Hoang", "connectClicked: 123");
connectStart = System.currentTimeMillis();
String selected = (String)spinner.getSelectedItem();
BaseOptions options = optionsMap.get(selected);
String serviceId = options.getFieldValue(R.id.serviceid);

SERVICE_ID = serviceId == null ? SERVICE_ID : serviceId;


Log.d("HoangHoang", SERVICE_ID);
String bleScanMethod = prefs.getString("bleScanMethod",
"Auto_Calibration");
log("BLE Scan Method: " + bleScanMethod.replaceAll("_", " "));
log("Starting bluetooth scan");

if (bleScanMethod.equals("Incremental")) {
Log.d("Hoang", "connectClicked: Incremental");
int bleScanTimerPeriodMilliSecs =
Integer.parseInt(prefs.getString("bleScanTimerPeriodMilliSecs", "250"));
int startScanThreshold =
Integer.parseInt(prefs.getString("startScanThreshold", "-45"));
int minScanThreshold =
Integer.parseInt(prefs.getString("connectThreshold", "-55"));
int incrementStepSize =
Integer.parseInt(prefs.getString("incrementStepSize", "1"));
processor.startBluetoothWithIncrements(bleScanTimerPeriodMilliSecs,
incrementStepSize,
startScanThreshold, minScanThreshold);
} else if (bleScanMethod.equals("Auto_Calibration")) {
Log.d("Hoang", "connectClicked: Auto_Calibration");
int calibrationDiff =
Integer.parseInt(prefs.getString("calibrationDiff", "20"));
Log.d("Hoang", "calibrationDiff: " +calibrationDiff);
processor.startBluetoothWithCalibration(calibrationDiff);
} else {
Log.d("Hoang", "connectClicked: Basic");
processor.startBluetooth(SERVICE_ID);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
connectButton.setEnabled(false);
disconnectButton.setEnabled(true);
pingCheckbox.setEnabled(false);
}
});
}

-----------------------
public void startBluetoothWithCalibration(int calibrationDiff) {
clearSessionData();
startBluetooth(null, calibrationDiff);
}
--------------------------------
private void startBluetooth(String serviceId, Integer calibrationDiff) {

_serviceId = null;
if (serviceId != null && !serviceId.trim().isEmpty()) {
_serviceId = serviceId.trim();
_isPolling = true;
} else {
_isPolling = false;
}

if (_commandListener == null || _config == null) {


throw new IllegalStateException("The listener and configuration have
not been set yet.");
}

_lastHostSequence = 0;
if (_serviceConnection == null) {
_serviceConnection = new BLEService(context, this, encryptionManager);
}
if (_serviceConnection instanceof BLEService) {
((BLEService)_serviceConnection).resetThresholds(_config);
if (_config.getTunerListener() != null) {

((BLEService)_serviceConnection).setListener(_config.getTunerListener());
}
_serviceConnection.connect(_serviceId, calibrationDiff);
}
}

--------------------------------
@Override
public void connect(String serviceId, Integer calibrationDiff) {
_serviceId = null;
if (serviceId != null && !serviceId.trim().isEmpty()) {
_serviceId = serviceId;
}

_calibrationDiff = calibrationDiff;
startScan();
}
------------------------------
private void startScan() {
synchronized (this) {
if (_connectionState != ConnectionState.Disconnected) {
return;
}
setState(ConnectionState.Scanning);
resetDevices();
final UUID[] deviceFilter = new UUID[]{UUID.fromString(DEVICE_ID)};
bluetoothAdapter.startLeScan(deviceFilter, this);
if (_scanTimer == null) {
_scanTimer = new Timer();
_scanTimer.schedule(new TimerTask() {
@Override
public void run() {
if (System.currentTimeMillis() - _lastScanTime > 1000) {
Log.d("cc", "NO SCAN! Restarting!");
bluetoothAdapter.stopLeScan(BLEService.this);
bluetoothAdapter.startLeScan(deviceFilter,
BLEService.this);
}
}
}, 1000, 1000);
}
}

------------------------------------
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
_lastScanTime = System.currentTimeMillis();
String deviceName = device.getName();
if (deviceName == null) {
return;
}
String cardReaderName = _processor.getConfiguration().getCardReaderName();
if (matchNameStart) {
if (!deviceName.startsWith(cardReaderName)) {
return;
}
} else {
if (!deviceName.equals(cardReaderName)) {
return;
}
}
BLEAdvertisement ad = new BLEAdvertisement(scanRecord);
synchronized (this) {
if (_connectionState != ConnectionState.Scanning) {
return;
}
DeviceInfo deviceInfo = devices.get(device.getAddress());
if (deviceInfo == null) {
deviceInfo = new DeviceInfo(device, ad);
devices.put(device.getAddress(), deviceInfo);
} else {
deviceInfo.advertisement = ad;
}
boolean shouldConnect = false;
if (_serviceId != null) {
//check for matching service Id
if (ad.getServiceData() == null || !
isServiceIdMatching(ad.getServiceData())) {
return;
}
//check for polled connection support in appearance bitfield
byte[] appearance = ad.getAppearance();
if (!Util.isBitSet(appearance[0],
POLLED_CONNECTION_MODE_SUPPORTED_BIT)) {
return;
}
shouldConnect = true;
} else {
shouldConnect = _signalAnalyzer.shouldConnect(device.getAddress(),
rssi, ad, _calibrationDiff );
}
if (!shouldConnect) {
return;
}
setState(ConnectionState.Connecting);
stopScanTimer();
workQueue.clear();
bluetoothAdapter.stopLeScan(this);
connectingDevice = deviceInfo;
bluetoothGatt = device.connectGatt(_context, false, new
BLEGattCallback());
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy