Tidied up BLE scan activity a bit
This commit is contained in:
@@ -32,6 +32,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
@@ -80,6 +81,14 @@ public class BLEScanActivity extends ListActivity {
|
|||||||
// Stops scanning after 10 seconds.
|
// Stops scanning after 10 seconds.
|
||||||
private static final long SCAN_PERIOD = 10000;
|
private static final long SCAN_PERIOD = 10000;
|
||||||
|
|
||||||
|
private int okColour = Color.BLUE;
|
||||||
|
private int warnColour = Color.MAGENTA;
|
||||||
|
private int alarmColour = Color.RED;
|
||||||
|
private int okTextColour = Color.WHITE;
|
||||||
|
private int warnTextColour = Color.WHITE;
|
||||||
|
private int alarmTextColour = Color.BLACK;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -158,61 +167,61 @@ public class BLEScanActivity extends ListActivity {
|
|||||||
String bleAddr = SP.getString("BLE_Device_Addr", "none");
|
String bleAddr = SP.getString("BLE_Device_Addr", "none");
|
||||||
String bleName = SP.getString("BLE_Device_Name", "none");
|
String bleName = SP.getString("BLE_Device_Name", "none");
|
||||||
tv.setText("Current Device=" + bleName + " (" + bleAddr + ")");
|
tv.setText("Current Device=" + bleName + " (" + bleAddr + ")");
|
||||||
|
tv.setTextColor(okTextColour);
|
||||||
|
tv.setBackgroundColor(okColour);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
tv.setText("Current Device=" + "none" + " (" + "none" + ")");
|
tv.setText("Current Device=" + "none" + " (" + "none" + ")");
|
||||||
|
tv.setTextColor(warnTextColour);
|
||||||
|
tv.setBackgroundColor(warnColour);
|
||||||
}
|
}
|
||||||
|
|
||||||
tv = (TextView) findViewById(R.id.ble_present_tv);
|
tv = (TextView) findViewById(R.id.ble_present_tv);
|
||||||
if (mBluetoothAdapter == null) {
|
if (mBluetoothAdapter == null) {
|
||||||
tv.setText("ERROR - Bluetooth Adapter Not Present");
|
tv.setText("ERROR - Bluetooth Adapter Not Present");
|
||||||
|
tv.setTextColor(alarmTextColour);
|
||||||
|
tv.setBackgroundColor(alarmColour);
|
||||||
} else {
|
} else {
|
||||||
tv.setText("Bluetooth Adapter Present - OK");
|
tv.setText("Bluetooth Adapter Present - OK");
|
||||||
|
tv.setTextColor(okTextColour);
|
||||||
|
tv.setBackgroundColor(okColour);
|
||||||
}
|
}
|
||||||
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
|
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
|
||||||
// fire an intent to display a dialog asking the user to grant permission to enable it.
|
// fire an intent to display a dialog asking the user to grant permission to enable it.
|
||||||
tv = (TextView) findViewById(R.id.ble_adapter_tv);
|
tv = (TextView) findViewById(R.id.ble_adapter_tv);
|
||||||
if (!mBluetoothAdapter.isEnabled()) {
|
if (!mBluetoothAdapter.isEnabled()) {
|
||||||
tv.setText("ERROR - Bluetoot NOT Enabled");
|
tv.setText("ERROR - Bluetooth NOT Enabled");
|
||||||
|
tv.setTextColor(alarmTextColour);
|
||||||
|
tv.setBackgroundColor(alarmColour);
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
||||||
} else {
|
} else {
|
||||||
tv.setText("Bluetooth Adapter Enabled OK");
|
tv.setText("Bluetooth Adapter Enabled OK");
|
||||||
|
tv.setTextColor(okTextColour);
|
||||||
|
tv.setBackgroundColor(okColour);
|
||||||
}
|
}
|
||||||
|
|
||||||
requestBTPermissions(this);
|
requestBTPermissions(this);
|
||||||
|
|
||||||
|
boolean permissionsOk = true;
|
||||||
for (int i = 0; i < REQUIRED_PERMISSIONS.length; i++) {
|
for (int i = 0; i < REQUIRED_PERMISSIONS.length; i++) {
|
||||||
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[i]) == PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[i]) == PERMISSION_GRANTED) {
|
||||||
Log.i(TAG, "Permission " + REQUIRED_PERMISSIONS[i] + " OK");
|
Log.i(TAG, "Permission " + REQUIRED_PERMISSIONS[i] + " OK");
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Permission " + REQUIRED_PERMISSIONS[i] + " NOT GRANTED");
|
Log.e(TAG, "Permission " + REQUIRED_PERMISSIONS[i] + " NOT GRANTED");
|
||||||
|
permissionsOk = false;
|
||||||
Toast.makeText(this, "ERROR - Permission " + REQUIRED_PERMISSIONS[i] + " not Granted - this will not work!!!!!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "ERROR - Permission " + REQUIRED_PERMISSIONS[i] + " not Granted - this will not work!!!!!", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tv = (TextView) findViewById(R.id.ble_perm1_tv);
|
tv = (TextView) findViewById(R.id.ble_perm1_tv);
|
||||||
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[0]) == PERMISSION_GRANTED) {
|
if (permissionsOk) {
|
||||||
tv.setText("Permission " + REQUIRED_PERMISSIONS[0] + " OK");
|
tv.setText("Permissions required for Bluetooth Granted OK");
|
||||||
|
tv.setBackgroundColor(okColour);
|
||||||
|
tv.setTextColor(okTextColour);
|
||||||
} else {
|
} else {
|
||||||
tv.setText("ERROR: Permission " + REQUIRED_PERMISSIONS[0] + " NOT GRANTED");
|
tv.setText("ERROR: one or more permissions not granted - this may not work!");
|
||||||
}
|
tv.setBackgroundColor(warnColour);
|
||||||
tv = (TextView) findViewById(R.id.ble_perm2_tv);
|
tv.setTextColor(warnTextColour);
|
||||||
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[1]) == PERMISSION_GRANTED) {
|
|
||||||
tv.setText("Permission " + REQUIRED_PERMISSIONS[1] + " OK");
|
|
||||||
} else {
|
|
||||||
tv.setText("ERROR: Permission " + REQUIRED_PERMISSIONS[1] + " NOT GRANTED");
|
|
||||||
}
|
|
||||||
tv = (TextView) findViewById(R.id.ble_perm3_tv);
|
|
||||||
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[2]) == PERMISSION_GRANTED) {
|
|
||||||
tv.setText("Permission " + REQUIRED_PERMISSIONS[2] + " OK");
|
|
||||||
} else {
|
|
||||||
tv.setText("ERROR: Permission " + REQUIRED_PERMISSIONS[2] + " NOT GRANTED");
|
|
||||||
}
|
|
||||||
tv = (TextView) findViewById(R.id.ble_perm4_tv);
|
|
||||||
if (ContextCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[3]) == PERMISSION_GRANTED) {
|
|
||||||
tv.setText("Permission " + REQUIRED_PERMISSIONS[3] + " OK");
|
|
||||||
} else {
|
|
||||||
tv.setText("ERROR: Permission " + REQUIRED_PERMISSIONS[3] + " NOT GRANTED");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -322,6 +331,10 @@ public class BLEScanActivity extends ListActivity {
|
|||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
TextView tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
|
TextView tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
|
||||||
tv.setText("Stopped");
|
tv.setText("Stopped");
|
||||||
|
tv.setTextColor(okTextColour);
|
||||||
|
tv.setBackgroundColor(okColour);
|
||||||
|
|
||||||
|
|
||||||
Button b = (Button) findViewById(R.id.startScanButton);
|
Button b = (Button) findViewById(R.id.startScanButton);
|
||||||
b.setEnabled(true);
|
b.setEnabled(true);
|
||||||
|
|
||||||
@@ -331,6 +344,9 @@ public class BLEScanActivity extends ListActivity {
|
|||||||
startScan();
|
startScan();
|
||||||
tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
|
tv = (TextView) (findViewById(R.id.ble_scan_status_tv));
|
||||||
tv.setText("Scanning");
|
tv.setText("Scanning");
|
||||||
|
tv.setTextColor(warnTextColour);
|
||||||
|
tv.setBackgroundColor(warnColour);
|
||||||
|
|
||||||
Button b = (Button) findViewById(R.id.startScanButton);
|
Button b = (Button) findViewById(R.id.startScanButton);
|
||||||
b.setEnabled(false);
|
b.setEnabled(false);
|
||||||
|
|
||||||
|
|||||||
@@ -6,25 +6,19 @@
|
|||||||
android:paddingLeft="8dp"
|
android:paddingLeft="8dp"
|
||||||
android:paddingRight="8dp">
|
android:paddingRight="8dp">
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/ble_scan_status_tv"
|
android:id="@+id/ble_scan_title"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:textSize="16pt"
|
android:textSize="16pt"
|
||||||
android:text="ble_scan_status_tv" />
|
android:text="Scan for Bluetooth Data Sources" />
|
||||||
|
|
||||||
<Button
|
<TextView
|
||||||
android:id="@+id/startScanButton"
|
android:id="@+id/ble_connection_warning_tv"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:onClick="onScanButtonClick"
|
android:text="@string/ble_connected_warning_text" />
|
||||||
android:text="Start Scan"/>
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/ble_present_tv"
|
android:id="@+id/ble_present_tv"
|
||||||
@@ -47,21 +41,26 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="ble_perm1_tv" />
|
android:text="ble_perm1_tv" />
|
||||||
<TextView
|
|
||||||
android:id="@+id/ble_perm2_tv"
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="ble_perm2_tv" />
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/ble_perm3_tv"
|
android:id="@+id/ble_scan_status_tv"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="ble_perm3_tv" />
|
android:textSize="16pt"
|
||||||
<TextView
|
android:text="ble_scan_status_tv" />
|
||||||
android:id="@+id/ble_perm4_tv"
|
|
||||||
android:layout_width="match_parent"
|
<Button
|
||||||
|
android:id="@+id/startScanButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="ble_perm4_tv" />
|
android:onClick="onScanButtonClick"
|
||||||
|
android:text="Start Scan"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
|||||||
@@ -568,4 +568,5 @@
|
|||||||
<string name="algorithms">"Algorithms: "</string>
|
<string name="algorithms">"Algorithms: "</string>
|
||||||
<string name="battery_history">Battery History</string>
|
<string name="battery_history">Battery History</string>
|
||||||
<string name="watch_batt_hist">Watch Battery History (%)</string>
|
<string name="watch_batt_hist">Watch Battery History (%)</string>
|
||||||
|
<string name="ble_connected_warning_text">NOTE: Devices will not appear on this list if they are already connected - disconnect device from GadgetBridge etc. to allow it to be selected here</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user