Code Connect
Code Connect
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);
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;
}
_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());
}
}