use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.
the class BluetoothScan method onListItemClick.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "Item Clicked");
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null || device.getName() == null)
return;
Toast.makeText(this, R.string.connecting_to_device, Toast.LENGTH_LONG).show();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
synchronized (ActiveBluetoothDevice.table_lock) {
ActiveBluetoothDevice btDevice = new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle();
prefs.edit().putString("last_connected_device_address", device.getAddress()).apply();
Blukon.clearPin();
if (btDevice == null) {
ActiveBluetoothDevice newBtDevice = new ActiveBluetoothDevice();
newBtDevice.name = device.getName();
newBtDevice.address = device.getAddress();
newBtDevice.save();
} else {
btDevice.name = device.getName();
btDevice.address = device.getAddress();
btDevice.save();
}
startWatchUpdaterService(this, WatchUpdaterService.ACTION_SYNC_ACTIVEBTDEVICE, TAG);
}
// automatically set or unset the option for "Transmiter" device
boolean using_transmiter = false;
// Experimental support for rfduino from Tomasz Stachowicz
// automatically set or unset the option for "RFDuino" device
boolean using_rfduino = false;
try {
if (device.getName().toLowerCase().contains("limitter") && (adverts.containsKey(device.getAddress()) && ((new String(adverts.get(device.getAddress()), "UTF-8").contains("eLeR")) || (new String(adverts.get(device.getAddress()), "UTF-8").contains("data")))) || device.getName().toLowerCase().contains("limitterd")) {
String msg = "Auto-detected transmiter_pl device!";
Log.e(TAG, msg);
JoH.static_toast_long(msg);
using_transmiter = true;
}
prefs.edit().putBoolean("use_transmiter_pl_bluetooth", using_transmiter).apply();
// Experimental support for rfduino from Tomasz Stachowicz
if (device.getName().toLowerCase().contains("xbridge") && (adverts.containsKey(device.getAddress()) && (new String(adverts.get(device.getAddress()), "UTF-8").contains("rfduino")))) {
String msg = "Auto-detected rfduino device!";
Log.e(TAG, msg);
JoH.static_toast_long(msg);
using_rfduino = true;
}
prefs.edit().putBoolean("use_rfduino_bluetooth", using_rfduino).apply();
if (device.getName().toLowerCase().contains("dexcom")) {
if (!CollectionServiceStarter.isBTShare(getApplicationContext())) {
prefs.edit().putString("dex_collection_method", "DexcomShare").apply();
prefs.edit().putBoolean("calibration_notifications", false).apply();
}
if (prefs.getString("share_key", "SM00000000").compareTo("SM00000000") == 0 || prefs.getString("share_key", "SM00000000").length() < 10) {
requestSerialNumber(prefs);
} else
returnToHome();
} else if (device.getName().toLowerCase().contains("bridge")) {
if (!CollectionServiceStarter.isDexBridgeOrWifiandDexBridge())
prefs.edit().putString("dex_collection_method", "DexbridgeWixel").apply();
if (prefs.getString("dex_txid", "00000").compareTo("00000") == 0 || prefs.getString("dex_txid", "00000").length() < 5) {
requestTransmitterId(prefs);
} else
returnToHome();
} else if (device.getName().toLowerCase().contains("drip")) {
if (!(CollectionServiceStarter.isBTWixelOrLimiTTer(getApplicationContext()) || CollectionServiceStarter.isWifiandBTWixel(getApplicationContext())) || CollectionServiceStarter.isLimitter()) {
prefs.edit().putString("dex_collection_method", "BluetoothWixel").apply();
}
returnToHome();
} else if (device.getName().toLowerCase().contains("limitter")) {
if (!CollectionServiceStarter.isLimitter()) {
prefs.edit().putString("dex_collection_method", "LimiTTer").apply();
}
returnToHome();
} else if (device.getName().toLowerCase().contains("bluereader")) {
if (!CollectionServiceStarter.isLimitter()) {
prefs.edit().putString("dex_collection_method", "LimiTTer").apply();
}
returnToHome();
} else if ((device.getName().toLowerCase().contains("miaomiao")) || (device.getName().toLowerCase().startsWith("watlaa"))) {
if (!(CollectionServiceStarter.isLimitter() || CollectionServiceStarter.isWifiandBTLibre())) {
prefs.edit().putString("dex_collection_method", "LimiTTer").apply();
}
returnToHome();
} else if (device.getName().toLowerCase().contains("sweetreader")) {
if (!CollectionServiceStarter.isLimitter()) {
prefs.edit().putString("dex_collection_method", "LimiTTer").apply();
}
returnToHome();
} else if (device.getName().matches("^BLU[0-9][0-9][0-9][0-9][0-9].*$")) {
Blukon.doPinDialog(this, new Runnable() {
@Override
public void run() {
if (!CollectionServiceStarter.isLimitter()) {
prefs.edit().putString("dex_collection_method", "LimiTTer").apply();
}
returnToHome();
}
});
} else if (device.getName().matches("MT")) {
if (Medtrum.saveSerialFromLegacy(adverts.get(device.getAddress()))) {
JoH.static_toast_long("Set Medtrum serial number");
CollectionServiceStarter.restartCollectionServiceBackground();
returnToHome();
} else {
JoH.static_toast_long("Failed to find Medtrum serial number");
}
} else {
returnToHome();
}
} catch (UnsupportedEncodingException | NullPointerException e) {
Log.d(TAG, "Got exception in listitemclick: " + Arrays.toString(e.getStackTrace()));
}
}
use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.
the class SystemStatus method setCurrentDevice.
public void setCurrentDevice() {
if (activeBluetoothDevice != null) {
current_device.setText(activeBluetoothDevice.name);
} else {
current_device.setText("None Set");
}
String collection_method = prefs.getString("dex_collection_method", "BluetoothWixel");
if (collection_method.compareTo("DexcomG5") == 0) {
Transmitter defaultTransmitter = new Transmitter(prefs.getString("dex_txid", "ABCDEF"));
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter != null) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if ((pairedDevices != null) && (pairedDevices.size() > 0)) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName() != null) {
String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId);
String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName());
if (transmitterIdLastTwo.equals(deviceNameLastTwo)) {
current_device.setText(defaultTransmitter.transmitterId);
}
}
}
}
} else {
current_device.setText("No Bluetooth");
}
}
}
use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.
the class DexCollectionService method checkConnection.
synchronized void checkConnection() {
status("Attempting connection");
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null) {
status("No bluetooth manager");
setRetryTimer();
return;
}
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
status("No bluetooth adapter");
setRetryTimer();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
// can't be connected if BT is disabled
mConnectionState = STATE_DISCONNECTED;
if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) {
Log.i(TAG, "Turning bluetooth on as appears disabled");
status("Turning bluetooth on");
JoH.setBluetoothEnabled(getApplicationContext(), true);
} else {
Log.d(TAG, "Not automatically turning on bluetooth due to preferences");
}
}
if (device != null) {
boolean found = false;
for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
if (bluetoothDevice.getAddress().equals(device.getAddress())) {
found = true;
if (mConnectionState != STATE_CONNECTED) {
UserError.Log.d(TAG, "Detected state change by checking connected devices");
handleConnectedStateChange();
}
break;
}
}
if (!found) {
if (mConnectionState == STATE_CONNECTED) {
UserError.Log.d(TAG, "Marking disconnected as not in list of connected devices");
// not in connected list so should be disconnected we think
mConnectionState = STATE_DISCONNECTED;
}
}
} else {
UserError.Log.d(TAG, "Device is null in checkConnection");
// can't be connected if we don't know the device
mConnectionState = STATE_DISCONNECTED;
}
Log.i(TAG, "checkConnection: Connection state: " + getStateStr(mConnectionState));
if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
final ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
if (btDevice != null) {
final String deviceAddress = btDevice.address;
mDeviceAddress = deviceAddress;
try {
if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(deviceAddress) != null) {
if (useScanning()) {
status(gs(R.string.scanning) + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
scanMeister.setAddress(deviceAddress).addCallBack(this, TAG).scan();
} else {
status("Connecting" + (Home.get_engineering_mode() ? ": " + deviceAddress : ""));
connect(deviceAddress);
}
mStaticState = mConnectionState;
return;
}
} catch (IllegalArgumentException e) {
Log.e(TAG, "IllegalArgumentException: " + e);
}
}
} else if (mConnectionState == STATE_CONNECTING) {
mStaticState = mConnectionState;
if (JoH.msSince(last_connect_request) > (getTrustAutoConnect() ? Constants.SECOND_IN_MS * 3600 : Constants.SECOND_IN_MS * 30)) {
Log.i(TAG, "Connecting for too long, shutting down");
retry_backoff = 0;
close();
}
} else if (mConnectionState == STATE_CONNECTED) {
// WOOO, we are good to go, nothing to do here!
status("Last Connected");
Log.i(TAG, "checkConnection: Looks like we are already connected, ready to receive");
retry_backoff = 0;
mStaticState = mConnectionState;
if (use_polling && (JoH.msSince(lastPacketTime) >= POLLING_PERIOD)) {
pollForData();
}
return;
}
setRetryTimer();
}
use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip by NightscoutFoundation.
the class DexShareCollectionService method attemptConnection.
public void attemptConnection() {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager != null) {
if (device != null) {
mConnectionState = STATE_DISCONNECTED;
for (BluetoothDevice bluetoothDevice : mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) {
if (bluetoothDevice.getAddress().compareTo(device.getAddress()) == 0) {
mConnectionState = STATE_CONNECTED;
}
}
}
Log.i(TAG, "Connection state: " + mConnectionState);
if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
ActiveBluetoothDevice btDevice = ActiveBluetoothDevice.first();
if (btDevice != null) {
mDeviceName = btDevice.name;
mDeviceAddress = btDevice.address;
mBluetoothAdapter = mBluetoothManager.getAdapter();
try {
if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getRemoteDevice(mDeviceAddress) != null) {
connect(mDeviceAddress);
return;
} else {
Log.w(TAG, "Bluetooth is disabled or BT device cant be found");
setRetryTimer();
return;
}
} catch (IllegalArgumentException e) {
if (JoH.ratelimit("dex-share-error-log", 180)) {
Log.wtf(TAG, "Error connecting: " + e);
}
}
} else {
Log.w(TAG, "No bluetooth device to try and connect to");
setRetryTimer();
return;
}
} else if (mConnectionState == STATE_CONNECTED) {
Log.i(TAG, "Looks like we are already connected, going to read!");
attemptRead();
return;
} else {
setRetryTimer();
return;
}
} else {
setRetryTimer();
return;
}
}
use of com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice in project xDrip-plus by jamorham.
the class ShareTest method attemptConnection.
public void attemptConnection() {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (device != null) {
details.append("\nConnection state: " + " Device is not null");
mConnectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
}
Log.i(TAG, "Connection state: " + mConnectionState);
details.append("\nConnection state: " + mConnectionState);
if (mConnectionState == STATE_DISCONNECTED || mConnectionState == STATE_DISCONNECTING) {
ActiveBluetoothDevice btDevice = new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle();
if (btDevice != null) {
details.append("\nBT Device: " + btDevice.name);
mDeviceName = btDevice.name;
mDeviceAddress = btDevice.address;
mBluetoothAdapter = mBluetoothManager.getAdapter();
boolean newConnection = true;
if (newConnection) {
is_connected = connect(mDeviceAddress);
details.append("\nConnecting...: ");
}
}
}
}
Aggregations