Skip to content

Commit

Permalink
BLE initial
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeveid committed Jan 20, 2024
1 parent b5b9f81 commit 0450a38
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 88 deletions.
67 changes: 67 additions & 0 deletions res/layout-land/ble_scan_result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
>

<TextView
android:id="@+id/device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Wrist Band Name" />

<TextView
android:id="@+id/mac_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="@id/device_name"
app:layout_constraintStart_toStartOf="parent"
tools:text="MAC: XX:XX:XX:XX:XX" />

<Button
android:id="@+id/use_as_device_b"
style="@style/SBButton"
android:layout_width="44dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="B"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/use_as_device_a"
style="@style/SBButton"
android:layout_width="44dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="A"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/use_as_device_b" />
<TextView
android:id="@+id/signal_strength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/use_as_device_a"
tools:text="-58 dBm" />
<TextView
android:id="@+id/device_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/use_as_device_a"
tools:text="0.1 m" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions res/layout-land/constraint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
app:layout_constraintEnd_toEndOf="@id/btn_score2"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintVertical_bias="0.05"
android:contentDescription="showing if bluetooth connection is active"
android:gravity="center"
android:visibility="visible"
app:layout_constraintHeight_percent="@fraction/ls_fr_bluetooth_icon_height"
Expand Down
4 changes: 2 additions & 2 deletions res/layout/ble_scan_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/use_as_device_a"
app:layout_constraintTop_toBottomOf="@id/mac_address"
app:layout_constraintStart_toStartOf="@id/mac_address"
tools:text="-58 dBm" />
<TextView
android:id="@+id/device_distance"
Expand Down
2 changes: 1 addition & 1 deletion res/layout/constraint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center"
android:visibility="visible"
android:text="Some information about ble connected devices"
android:text="Some information about or from ble connected devices"
app:layout_constraintHeight_percent="0.04"
android:layout_width="0dp" android:layout_height="0dp"
android:textColor="@android:color/white"
Expand Down
7 changes: 7 additions & 0 deletions res/values/bluetooth.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

<string name="ble_start_scan">Start scan</string>
<string name="ble_stop_scan">Stop scan</string>
<string name="ble_scanning">Scanning</string>
<string name="ble_start_scoring_with_devices">Start scoring with devices</string>
<string name="ble_score_cancelled_by_opponent_x_by_pressing_y">Score cancelled by opponent %1$s by pressing %2$s</string>
<string name="ble_score_cancelled_by_initiator_x_by_pressing_y">Score cancelled by initiator %1$s by pressing %2$s</string>
<string name="ble_score_confirmed_by_opponent_x_by_pressing_y">Score confirmed by opponent %1$s by pressing %2$s</string>
<string name="ble_x_confirm_score_for_y_by_pressing_z">%1$s, confirm score for %2$s by pressing %3$s</string>
<string name="ble_score_for_X_changed_by_ble">Score for %1$s changed by BLE</string>
<plurals name="ble_ready_for_scoring_with_devices">
<item quantity="one">Ready for scoring with the BLE device</item>
<item quantity="other">Ready for scoring with both BLE devices</item>
</plurals>
<string name="ble_only_one_of_two_devices_connected">Only one of 2 devices connected</string>
<string name="ble_Lost_connection_to_a_device">Lost connection to a BLE device</string>

<string name="bt_disconnect">Disconnect</string>
<string name="bt_pull_or_push">Choose</string>
Expand Down
14 changes: 8 additions & 6 deletions src/com/doubleyellow/scoreboard/bluetooth_le/BLEHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.doubleyellow.scoreboard.bluetooth.BTMessage;
import com.doubleyellow.scoreboard.bluetooth.MessageSource;
import com.doubleyellow.scoreboard.main.ScoreBoard;
import com.doubleyellow.scoreboard.R;

/**
* Handler specific for BluetoothLE messages.
Expand Down Expand Up @@ -63,20 +64,21 @@ public BLEHandler(ScoreBoard scoreBoard) {
case CONNECTED_DiscoveringServices:
break;
case CONNECTED_TO_1_of_2:
sb.setBluetoothBLEIconVisibility(View.VISIBLE, 1);
sb.updateBLEConnectionStatus(View.VISIBLE, 1, sb.getString(R.string.ble_only_one_of_two_devices_connected), -1);
break;
case CONNECTED_ALL:
sb.setBluetoothBLEIconVisibility(View.VISIBLE, iNrOfDevices);
String sUIMsg = sb.getResources().getQuantityString(R.plurals.ble_ready_for_scoring_with_devices, iNrOfDevices);
sb.updateBLEConnectionStatus(View.VISIBLE, iNrOfDevices, sUIMsg, 10);
break;
case DISCONNECTED:
sb.setBluetoothBLEIconVisibility(View.INVISIBLE, -1);
sb.updateBLEConnectionStatus(View.INVISIBLE, -1, "Oeps 3", -1);
break;
case CONNECTING:
sb.setBluetoothBLEIconVisibility(View.VISIBLE, iNrOfDevices);
sb.updateBLEConnectionStatus(View.VISIBLE, iNrOfDevices, null, -1);
break;
case DISCONNECTED_Gatt:
// if one of the two devices
sb.setBluetoothBLEIconVisibility(View.VISIBLE, iNrOfDevices);
// if one of the two devices disconnects
sb.updateBLEConnectionStatus(View.VISIBLE, iNrOfDevices, sb.getString(R.string.ble_Lost_connection_to_a_device), -1);
break;
default:
Toast.makeText(sb, sMsg, Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ private class MyBluetoothGattCallback extends BluetoothGattCallback
}
void translateToBTMessageAndSendToMain(BluetoothGattCharacteristic characteristic, byte[] value) {
if ( false ) {
Log.d(TAG, "iValue : " + value[0]);
Log.d(TAG, "sValue : " + new String(value));
Log.d(TAG, "value[0] : " + value[0]);
Log.d(TAG, "sValue : " + new String(value));
for (int i = 0; i < value.length; i++) {
Log.d(TAG, "b[" + i + "] : " + value[i]);
}
Expand Down
9 changes: 9 additions & 0 deletions src/com/doubleyellow/scoreboard/bluetooth_le/BLEUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public class BLEUtil
{
private final static String TAG = "SB.BLEUtil";

@NonNull
public static String[] getPermissions() {
String[] permissions = {Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.S ) {
permissions = new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION };
}
return permissions;
}

public enum Keys {
DeviceNameMustMatch,
DeviceNameStartsWith,
Expand Down
Loading

0 comments on commit 0450a38

Please sign in to comment.