Compare commits
5 Commits
main
...
53-als-geb
Author | SHA1 | Date | |
---|---|---|---|
|
bead6a5a13 | ||
|
49f97b57dd | ||
|
0153b70578 | ||
|
39f3d4bb1d | ||
|
a4907f00c8 |
3
.vscode/arduino.json
vendored
3
.vscode/arduino.json
vendored
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"port": "COM3",
|
"port": "COM3",
|
||||||
"board": "esp32:esp32:esp32s3"
|
"board": "esp32:esp32:esp32s3",
|
||||||
|
"sketch": "code\\arduino\\Position-tracking\\Position-tracking.ino"
|
||||||
}
|
}
|
1
.vscode/c_cpp_properties.json
vendored
1
.vscode/c_cpp_properties.json
vendored
@@ -225,7 +225,6 @@
|
|||||||
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\tools\\sdk\\esp32s3\\qio_qspi\\include",
|
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\tools\\sdk\\esp32s3\\qio_qspi\\include",
|
||||||
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\cores\\esp32",
|
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\cores\\esp32",
|
||||||
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\variants\\esp32s3",
|
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\variants\\esp32s3",
|
||||||
"C:\\Users\\sebas\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.16\\libraries\\BLE\\src",
|
|
||||||
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0",
|
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0",
|
||||||
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0\\xtensa-esp32s3-elf",
|
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0\\xtensa-esp32s3-elf",
|
||||||
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0\\backward",
|
"c:\\users\\sebas\\appdata\\local\\arduino15\\packages\\esp32\\tools\\xtensa-esp32s3-elf-gcc\\esp-2021r2-patch5-8.4.0\\xtensa-esp32s3-elf\\include\\c++\\8.4.0\\backward",
|
||||||
|
12
code/arduino/Position-tracking/Position-tracking.ino
Normal file
12
code/arduino/Position-tracking/Position-tracking.ino
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "PositionSensor.h"
|
||||||
|
|
||||||
|
PositionSensor sensor(15); // Sensor Pin
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
sensor.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
sensor.Measure();
|
||||||
|
delay(1000);
|
||||||
|
}
|
13
code/arduino/Position-tracking/PositionSensor.cpp
Normal file
13
code/arduino/Position-tracking/PositionSensor.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "PositionSensor.h"
|
||||||
|
|
||||||
|
PositionSensor::PositionSensor(int pin) : _pin(pin) {}
|
||||||
|
|
||||||
|
void PositionSensor::begin() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
pinMode(_pin, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PositionSensor::Measure() {
|
||||||
|
int value = analogRead(_pin);
|
||||||
|
Serial.println(value);
|
||||||
|
}
|
15
code/arduino/Position-tracking/PositionSensor.h
Normal file
15
code/arduino/Position-tracking/PositionSensor.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef PositionSensor_h
|
||||||
|
#define PositionSensor_h
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class PositionSensor {
|
||||||
|
public:
|
||||||
|
PositionSensor(int pin);
|
||||||
|
void begin();
|
||||||
|
void Measure();
|
||||||
|
private:
|
||||||
|
int _pin;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
0
code/arduino/Position-tracking/SendData.cpp
Normal file
0
code/arduino/Position-tracking/SendData.cpp
Normal file
0
code/arduino/Position-tracking/SendData.h
Normal file
0
code/arduino/Position-tracking/SendData.h
Normal file
46
code/arduino/Position-tracking/test/test.ino
Normal file
46
code/arduino/Position-tracking/test/test.ino
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include <BLEDevice.h>
|
||||||
|
#include <BLEUtils.h>
|
||||||
|
#include <BLEServer.h>
|
||||||
|
|
||||||
|
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
||||||
|
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
||||||
|
|
||||||
|
#define LDR 15
|
||||||
|
|
||||||
|
BLECharacteristic *pCharacteristic;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
pinMode(LDR, INPUT);
|
||||||
|
|
||||||
|
Serial.println("Starting BLE work!");
|
||||||
|
|
||||||
|
BLEDevice::init("ESP32-Pepper");
|
||||||
|
BLEServer *pServer = BLEDevice::createServer();
|
||||||
|
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||||
|
pCharacteristic = pService->createCharacteristic(
|
||||||
|
CHARACTERISTIC_UUID,
|
||||||
|
BLECharacteristic::PROPERTY_READ |
|
||||||
|
BLECharacteristic::PROPERTY_WRITE
|
||||||
|
);
|
||||||
|
|
||||||
|
pCharacteristic->setValue("Hello World");
|
||||||
|
pService->start();
|
||||||
|
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||||
|
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||||
|
pAdvertising->setScanResponse(true);
|
||||||
|
pAdvertising->setMinPreferred(0x06);
|
||||||
|
pAdvertising->setMinPreferred(0x12);
|
||||||
|
BLEDevice::startAdvertising();
|
||||||
|
Serial.println("Waiting a client connection to notify...");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
int ldrValue = analogRead(LDR);
|
||||||
|
Serial.println(ldrValue);
|
||||||
|
char ldrValueChar[50];
|
||||||
|
sprintf(ldrValueChar, "%d", ldrValue);
|
||||||
|
pCharacteristic->setValue(ldrValueChar);
|
||||||
|
delay(2000);
|
||||||
|
}
|
@@ -1,33 +0,0 @@
|
|||||||
#include <BLEDevice.h>
|
|
||||||
#include <BLEServer.h>
|
|
||||||
|
|
||||||
// Define the service UUID
|
|
||||||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
|
||||||
|
|
||||||
// Define the characteristic UUID
|
|
||||||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Create a BLE server
|
|
||||||
BLEServer *pServer = BLEDevice::createServer();
|
|
||||||
|
|
||||||
// Create a BLE service
|
|
||||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
|
||||||
|
|
||||||
// Create a BLE characteristic
|
|
||||||
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
|
|
||||||
CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ);
|
|
||||||
|
|
||||||
// Set the characteristic value
|
|
||||||
pCharacteristic->setValue("Hello, Bluetooth!");
|
|
||||||
|
|
||||||
// Start the service
|
|
||||||
pService->start();
|
|
||||||
|
|
||||||
// Start advertising the service
|
|
||||||
pServer->getAdvertising()->start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// Nothing to do here
|
|
||||||
}
|
|
@@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2008 Nedim Jackman
|
|
||||||
|
|
||||||
This file is part of Wiiboard Simple
|
|
||||||
|
|
||||||
Wiiboard Simple is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Wiiboard Simple is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
|
||||||
along with Wiiboard Simple. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.example.fitbot.bluetooth;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* In very basic structure, all bluetooth devices have an address that the connection
|
|
||||||
* is associated with.
|
|
||||||
*/
|
|
||||||
public interface BluetoothDevice {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The fixed address of the device.
|
|
||||||
* Constant throughout the connection of the device.
|
|
||||||
*/
|
|
||||||
public String getBluetoothAddress();
|
|
||||||
|
|
||||||
}
|
|
@@ -1,133 +0,0 @@
|
|||||||
package com.example.fitbot.bluetooth;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class BluetoothManager {
|
|
||||||
|
|
||||||
private BluetoothAdapter bluetoothAdapter;
|
|
||||||
private Context context;
|
|
||||||
|
|
||||||
public BluetoothManager(Context context) {
|
|
||||||
this.context = context;
|
|
||||||
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBluetoothSupported() {
|
|
||||||
return bluetoothAdapter != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBluetoothEnabled() {
|
|
||||||
return bluetoothAdapter.isEnabled();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startDiscovery() {
|
|
||||||
if (!isBluetoothEnabled()) {
|
|
||||||
Log.e("BluetoothManager", "Bluetooth is not enabled");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bluetoothAdapter.isDiscovering()) {
|
|
||||||
bluetoothAdapter.cancelDiscovery();
|
|
||||||
Log.d("BluetoothManager", "Cancelling current discovery process...");
|
|
||||||
}
|
|
||||||
|
|
||||||
IntentFilter filter = new IntentFilter();
|
|
||||||
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
|
||||||
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
|
||||||
context.registerReceiver(receiver, filter);
|
|
||||||
|
|
||||||
bluetoothAdapter.startDiscovery();
|
|
||||||
Log.d("BluetoothManager", "Starting discovery process...");
|
|
||||||
}
|
|
||||||
|
|
||||||
public final BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String action = intent.getAction();
|
|
||||||
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
|
||||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
||||||
if (device.getName() != null && device.getName().contains("Nintendo RVL-WBC-01")) {
|
|
||||||
Log.d("BluetoothManager", "Wii Balance Board found: " + device.getName() + " [" + device.getAddress() + "]");
|
|
||||||
connectToDevice(device);
|
|
||||||
}
|
|
||||||
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
|
||||||
context.unregisterReceiver(this);
|
|
||||||
Log.d("BluetoothManager", "Discovery finished.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public void connectToDevice(BluetoothDevice device) {
|
|
||||||
new Thread(() -> {
|
|
||||||
BluetoothSocket socket = null;
|
|
||||||
final int MAX_RETRIES = 3;
|
|
||||||
final long RETRY_DELAY_MS = 1000;
|
|
||||||
boolean isConnected = false;
|
|
||||||
|
|
||||||
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
|
||||||
try {
|
|
||||||
UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
|
||||||
socket = device.createRfcommSocketToServiceRecord(SPP_UUID);
|
|
||||||
bluetoothAdapter.cancelDiscovery();
|
|
||||||
|
|
||||||
byte[] pinBytes = getPinBytes(device.getAddress());
|
|
||||||
try {
|
|
||||||
Method setPinMethod = device.getClass().getDeclaredMethod("setPin", byte[].class);
|
|
||||||
setPinMethod.invoke(device, pinBytes);
|
|
||||||
Log.d("BluetoothManager", "PIN set successfully");
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e("BluetoothManager", "Failed to set PIN", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
socket.connect();
|
|
||||||
Log.d("BluetoothManager", "Connected to Wii Balance Board");
|
|
||||||
isConnected = true;
|
|
||||||
break;
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e("BluetoothManager", "Connection attempt " + attempt + " failed", e);
|
|
||||||
if (attempt < MAX_RETRIES) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(RETRY_DELAY_MS);
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (!isConnected && socket != null) {
|
|
||||||
try {
|
|
||||||
socket.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Log.e("BluetoothManager", "Could not close the client socket", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isConnected) {
|
|
||||||
Log.e("BluetoothManager", "All connection attempts failed.");
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] getPinBytes(String address) {
|
|
||||||
String[] addrParts = address.split(":");
|
|
||||||
byte[] pin = new byte[addrParts.length];
|
|
||||||
for (int i = 0; i < addrParts.length; i++) {
|
|
||||||
pin[i] = (byte) Integer.parseInt(addrParts[addrParts.length - 1 - i], 16);
|
|
||||||
}
|
|
||||||
Log.i("BluetoothManager", "PIN: " + pin);
|
|
||||||
return pin;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -0,0 +1,139 @@
|
|||||||
|
package com.example.fitbot.bluetooth;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.bluetooth.BluetoothGatt;
|
||||||
|
import android.bluetooth.BluetoothGattCallback;
|
||||||
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
|
import android.bluetooth.BluetoothGattService;
|
||||||
|
import android.bluetooth.BluetoothProfile;
|
||||||
|
import android.bluetooth.le.BluetoothLeScanner;
|
||||||
|
import android.bluetooth.le.ScanCallback;
|
||||||
|
import android.bluetooth.le.ScanResult;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class DeviceScanner {
|
||||||
|
private Context context;
|
||||||
|
private static final UUID CORRECT_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"); // ESP UUID characteristic
|
||||||
|
private BluetoothAdapter bluetoothAdapter;
|
||||||
|
private BluetoothLeScanner bluetoothLeScanner;
|
||||||
|
private boolean scanning;
|
||||||
|
private Handler handler = new Handler();
|
||||||
|
|
||||||
|
// Stops scanning after 10 seconds.
|
||||||
|
private static final long SCAN_PERIOD = 10000;
|
||||||
|
|
||||||
|
public DeviceScanner(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scanLeDevice() {
|
||||||
|
if (!scanning) {
|
||||||
|
handler.postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
scanning = false;
|
||||||
|
bluetoothLeScanner.stopScan(leScanCallback);
|
||||||
|
Log.i("DeviceScanner", "Stopped scanning after scan period");
|
||||||
|
}
|
||||||
|
}, SCAN_PERIOD);
|
||||||
|
scanning = true;
|
||||||
|
bluetoothLeScanner.startScan(leScanCallback);
|
||||||
|
Log.i("DeviceScanner", "Started scanning");
|
||||||
|
} else {
|
||||||
|
scanning = false;
|
||||||
|
bluetoothLeScanner.stopScan(leScanCallback);
|
||||||
|
Log.i("DeviceScanner", "Stopped scanning");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stops scanning for devices.
|
||||||
|
public void stopScan() {
|
||||||
|
if (scanning) {
|
||||||
|
scanning = false;
|
||||||
|
bluetoothLeScanner.stopScan(leScanCallback);
|
||||||
|
Log.i("DeviceScanner", "Stopped scanning");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device scan callback to find the ESP32
|
||||||
|
private ScanCallback leScanCallback =
|
||||||
|
new ScanCallback() {
|
||||||
|
@Override
|
||||||
|
public void onScanResult(int callbackType, ScanResult result) {
|
||||||
|
super.onScanResult(callbackType, result);
|
||||||
|
BluetoothDevice device = result.getDevice();
|
||||||
|
if (device.getName() != null && device.getName().equals("ESP32-Pepper")) {
|
||||||
|
Log.i("DeviceScanner", "Device found: " + device.getName() + " (" + device.getAddress() + ")");
|
||||||
|
connectToDevice(device);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// GATT callback to connect to the ESP32 and read the characteristic
|
||||||
|
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
|
||||||
|
@Override
|
||||||
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
||||||
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
|
Log.i("DeviceScanner", "Connected to GATT server");
|
||||||
|
gatt.discoverServices();
|
||||||
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
|
Log.i("DeviceScanner", "Disconnected from GATT server");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discover services and characteristics
|
||||||
|
@Override
|
||||||
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
||||||
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
for (BluetoothGattService service : gatt.getServices()) {
|
||||||
|
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
|
||||||
|
if (isCorrectCharacteristic(characteristic)) {
|
||||||
|
gatt.setCharacteristicNotification(characteristic, true);
|
||||||
|
gatt.readCharacteristic(characteristic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the characteristic
|
||||||
|
@Override
|
||||||
|
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
||||||
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
Log.i("DeviceScanner", "Characteristic read: " + new String(characteristic.getValue(), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Characteristic changed
|
||||||
|
@Override
|
||||||
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||||||
|
Log.i("DeviceScanner", "Characteristic changed: " + new String(characteristic.getValue(), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Connect to the ESP32
|
||||||
|
public void connectToDevice(BluetoothDevice device) {
|
||||||
|
BluetoothGatt gatt = device.connectGatt(context, false, gattCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the characteristic has the correct UUID
|
||||||
|
private boolean isCorrectCharacteristic(BluetoothGattCharacteristic characteristic) {
|
||||||
|
// Log the UUID of the characteristic
|
||||||
|
Log.i("DeviceScanner", String.valueOf(characteristic.getUuid()));
|
||||||
|
|
||||||
|
// Check if the characteristic has the correct UUID
|
||||||
|
if (characteristic.getUuid().equals(CORRECT_CHARACTERISTIC_UUID)) {
|
||||||
|
Log.i("DeviceScanner", "Correct characteristic found");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@@ -19,74 +19,37 @@ import android.content.pm.PackageManager;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.example.fitbot.bluetooth.BluetoothDevice;
|
|
||||||
import com.example.fitbot.wiiboard.WiiBoard;
|
|
||||||
import com.example.fitbot.wiiboard.WiiBoardDiscoverer;
|
|
||||||
import com.example.fitbot.wiiboard.WiiBoardDiscoveryListener;
|
|
||||||
import com.example.fitbot.R;
|
import com.example.fitbot.R;
|
||||||
|
import com.example.fitbot.bluetooth.DeviceScanner;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final int REQUEST_LOCATION_PERMISSION = 1;
|
|
||||||
private WiiBoardDiscoverer wiiBoardDiscoverer;
|
|
||||||
|
|
||||||
//Variables
|
//Variables
|
||||||
DrawerLayout drawerLayout;
|
DrawerLayout drawerLayout;
|
||||||
NavigationView navigationView;
|
NavigationView navigationView;
|
||||||
Toolbar toolbar;
|
Toolbar toolbar;
|
||||||
Button startButton;
|
Button startButton;
|
||||||
|
|
||||||
|
private DeviceScanner deviceScanner;
|
||||||
|
|
||||||
@SuppressLint("WrongViewCast")
|
@SuppressLint("WrongViewCast")
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate (Bundle savedInstanceState) {
|
protected void onCreate (Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main );
|
setContentView(R.layout.activity_main );
|
||||||
|
|
||||||
// Check if the device supports Bluetooth
|
// Check if we have the necessary permissions, if not, request them
|
||||||
if (BluetoothAdapter.getDefaultAdapter() == null) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
Log.i("WiiBoardDiscoverer", "Device doesn't support Bluetooth. Exiting.");
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request location permissions for Bluetooth discovery on Android 6.0 and above
|
deviceScanner = new DeviceScanner(this);
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
deviceScanner.scanLeDevice();
|
||||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_PERMISSION);
|
|
||||||
} else {
|
|
||||||
// Initialize WiiBoardDiscoverer
|
|
||||||
wiiBoardDiscoverer = new WiiBoardDiscoverer(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
setUpUi();
|
setUpUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
||||||
if (requestCode == REQUEST_LOCATION_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
// Permission granted, initialize WiiBoardDiscoverer
|
|
||||||
wiiBoardDiscoverer = new WiiBoardDiscoverer(this);
|
|
||||||
} else {
|
|
||||||
// Handle the case where the user denies the location permission
|
|
||||||
Log.i("WiiBoardDiscoverer", "Location permission is required for Bluetooth discovery.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
if (wiiBoardDiscoverer != null) {
|
|
||||||
wiiBoardDiscoverer.startWiiBoardSearch();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
if (wiiBoardDiscoverer != null) {
|
|
||||||
wiiBoardDiscoverer.stopWiiBoardSearch();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setUpUi() {
|
private void setUpUi() {
|
||||||
/*---Hooks---*/
|
/*---Hooks---*/
|
||||||
drawerLayout = findViewById(R.id.drawer_layout);
|
drawerLayout = findViewById(R.id.drawer_layout);
|
||||||
|
@@ -1,48 +0,0 @@
|
|||||||
package com.example.fitbot.wiiboard;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public class WiiBoard {
|
|
||||||
private static final String TAG = "WiiBoard";
|
|
||||||
|
|
||||||
public static void connectToExtension(String address) {
|
|
||||||
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
|
|
||||||
BluetoothSocket socket = null;
|
|
||||||
final int maxTries = 3;
|
|
||||||
int currentTry = 0;
|
|
||||||
|
|
||||||
while (currentTry < maxTries) {
|
|
||||||
try {
|
|
||||||
// Use reflection to create a BluetoothSocket without UUID
|
|
||||||
Method m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
|
|
||||||
socket = (BluetoothSocket) m.invoke(device, 1);
|
|
||||||
|
|
||||||
bluetoothAdapter.cancelDiscovery();
|
|
||||||
socket.connect();
|
|
||||||
|
|
||||||
Log.i(TAG, "Connected to WiiBoard");
|
|
||||||
// Handle your communication here
|
|
||||||
|
|
||||||
return; // Exit the method upon successful connection
|
|
||||||
} catch (Exception e) { // Catching Exception to handle reflection exceptions too
|
|
||||||
Log.e(TAG, "Attempt " + (currentTry + 1) + " failed to connect", e);
|
|
||||||
currentTry++;
|
|
||||||
try {
|
|
||||||
// Wait a bit before retrying
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
Log.e(TAG, "Interrupted while waiting to retry connection", ie);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.e(TAG, "Could not connect to WiiBoard after " + maxTries + " attempts.");
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,66 +0,0 @@
|
|||||||
package com.example.fitbot.wiiboard;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
public class WiiBoardDiscoverer {
|
|
||||||
private BluetoothAdapter bluetoothAdapter;
|
|
||||||
private Context context;
|
|
||||||
private String discoveredAddress;
|
|
||||||
private boolean isSearching;
|
|
||||||
|
|
||||||
private final BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
String action = intent.getAction();
|
|
||||||
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
|
||||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
||||||
String name = device.getName();
|
|
||||||
if ("Nintendo RVL-WBC-01".equals(name)) {
|
|
||||||
discoveredAddress = device.getAddress();
|
|
||||||
Log.i("WiiBoardDiscoverer", "Discovered " + name + " " + discoveredAddress + ".");
|
|
||||||
isSearching = false;
|
|
||||||
bluetoothAdapter.cancelDiscovery();
|
|
||||||
context.unregisterReceiver(this); // Important to unregister
|
|
||||||
WiiBoard.connectToExtension(discoveredAddress);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public WiiBoardDiscoverer(Context context) {
|
|
||||||
this.context = context;
|
|
||||||
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
if (bluetoothAdapter == null) {
|
|
||||||
Log.i("WiiBoardDiscoverer", "Device doesn't support Bluetooth. Exiting.");
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startWiiBoardSearch() {
|
|
||||||
if (!isSearching && !bluetoothAdapter.isDiscovering()) {
|
|
||||||
isSearching = true;
|
|
||||||
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
|
|
||||||
context.registerReceiver(receiver, filter);
|
|
||||||
bluetoothAdapter.startDiscovery();
|
|
||||||
Log.i("WiiBoardDiscoverer", "WiiBoard Discovery Started");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopWiiBoardSearch() {
|
|
||||||
if (bluetoothAdapter.isDiscovering()) {
|
|
||||||
bluetoothAdapter.cancelDiscovery();
|
|
||||||
}
|
|
||||||
isSearching = false;
|
|
||||||
try {
|
|
||||||
context.unregisterReceiver(receiver);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// This can happen if the receiver was not registered or already unregistered
|
|
||||||
Log.i("WiiBoardDiscoverer", "Receiver was not registered or already unregistered");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,13 +0,0 @@
|
|||||||
package com.example.fitbot.wiiboard;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implement this interface to be notified of WiiBoards that are connected to. Register your
|
|
||||||
* listener with an instance of WiiBoardDiscoverer.
|
|
||||||
*/
|
|
||||||
public interface WiiBoardDiscoveryListener {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Is called by a WiiBoardDiscoverer when a WiiBoard has been found and successfully connected to.
|
|
||||||
*/
|
|
||||||
public void wiiBoardDiscovered(WiiBoard wiiboard);
|
|
||||||
}
|
|
BIN
docs/documentation/diagrams/positionTracking.fzz
Normal file
BIN
docs/documentation/diagrams/positionTracking.fzz
Normal file
Binary file not shown.
@@ -2,25 +2,34 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
For this project we want to design an embedded system that can track a users position. We want to track their current position on the ground and see how they are shifting their weight. This system will be used to track their position to determine if a user is doing the exercises correctly.
|
For this project we want to design an embedded system that can track a users position. We want to track their current position on the ground. This system will be used to track their position to determine if a user is doing the exercises correctly.
|
||||||
|
|
||||||
## Objectives
|
## Objectives
|
||||||
|
|
||||||
* Design an embedded system that can track user position.
|
- Design an embedded system that can track user position.
|
||||||
* Develop an algorithm to process the data from the Wii Fit Board and determine the user's position.
|
- Develop an algorithm to process the data from the sensor and determine the user's position.
|
||||||
|
- Sync the code to the current task for the user.
|
||||||
|
|
||||||
## Research and Analysis
|
## Research and Analysis
|
||||||
|
|
||||||
### Choosing the Wii Fit Board
|
### Choosing the sensor
|
||||||
|
|
||||||
For this project we have chosen the Wii Fit Board as our primary sensor. The Wii Fit Board is a balance board that can measure a user's weight and center of balance. It is a low-cost sensor that is easy to interface with a microcontroller. The Wii Fit Board communicates over Bluetooth, which makes it easy to connect to a microcontroller with Bluetooth capabilities.
|
For this project we have chosen LDR's as our primary sensor. The LDR's will be placed on the ground in a board and the user will stand on top of the board. The LDR's will be used to track the user's position. The LDR's will be connected to the esp32s3 microcontroller and the data will be processed to determine the user's position.
|
||||||
|
|
||||||
|
We have chosen this sensor since it's one of the easiest and cheapest solutions to our problem. Other sensors like pressure sensors, accelerometers, and Wii Balance Board are either too expensive, not the most optimal for the task, or hard to integrate with other systems.
|
||||||
|
|
||||||
### Alternative Solutions
|
### Alternative Solutions
|
||||||
|
|
||||||
There are other sensors that can be used for position tracking, such as pressure sensors or accelerometers. However, these sensors are more expensive and may require additional processing to determine the user's position. The Wii Fit Board provides a simple and cost-effective solution for position tracking.
|
There are other sensors that can be used for position tracking, such as pressure sensors, Wii Balance Board or accelerometers. However, these sensors are either too expensive, not the most optimal for the task or hard to integrate with other systems.
|
||||||
|
|
||||||
Example of other sensors that can be used for position tracking:
|
Example of other sensors that can be used for position tracking:
|
||||||
|
|
||||||
|
Wii Balance Board:
|
||||||
|
- Description: The Wii Balance Board is a balance board that can measure a user's weight and center of balance.
|
||||||
|
- Pros: Low-cost.
|
||||||
|
- Cons: Very hard to intergrate with other systems.
|
||||||
|
- Cost: ~ 20 euros (https://www.amazon.nl/Nintendo-Wii-Balance-Board-Wii/dp/B0013E9HP6)
|
||||||
|
|
||||||
Pressure sensors:
|
Pressure sensors:
|
||||||
- Description: Pressure sensors can be used to measure the force applied by the user on the ground. By measuring the pressure distribution, the user's position can be determined.
|
- Description: Pressure sensors can be used to measure the force applied by the user on the ground. By measuring the pressure distribution, the user's position can be determined.
|
||||||
- Pros: High accuracy, can measure force applied by the user.
|
- Pros: High accuracy, can measure force applied by the user.
|
||||||
@@ -42,36 +51,31 @@ To be added
|
|||||||
### Hardware
|
### Hardware
|
||||||
|
|
||||||
The hardware of the system will consist of the following components:
|
The hardware of the system will consist of the following components:
|
||||||
- Wii Fit Board: The primary sensor for position tracking.
|
- LDR: The sensor that will be used to track the user's position based on the light intensity.
|
||||||
- Pepper: The controller that will process the data from the Wii Fit Board.
|
- ESP32S3: The microcontroller that will process the data from the LDR.
|
||||||
|
- Pepper: The controller that will recieve the processed data from the ESP32S3 and will sync the data to the current task for the user.
|
||||||
|
|
||||||
|
#### Connection diagram
|
||||||
|
|
||||||
|
To be added
|
||||||
|
|
||||||
### Software
|
### Software
|
||||||
|
|
||||||
The software of the system will consist of the following:
|
To be added
|
||||||
- Wiiboard-simple: A library that will be used to transfer data from the Wii Fit Board to pepper.
|
|
||||||
- Position Tracking Algorithm: An algorithm that will process the sensor data and determine the user's position.
|
|
||||||
|
|
||||||
### Integration
|
### Integration
|
||||||
|
|
||||||
The Wii Fit Board will be connected to Pepper using the Wiiboard-simple library. The library will be used to read the sensor data from the Wii Fit Board and transfer it to Pepper. The position tracking algorithm will process the sensor data and determine the user's position.
|
To be added
|
||||||
|
|
||||||
Challenge:
|
|
||||||
- Connecting to the wii fit board. It is not possible to connect directly to the Wii Fit Board, it is necessary to use a library that can interpret the data sent by the Wii Fit Board.
|
|
||||||
- The Wii Fit Balance Board sends data in a specific format. To interpret this data, it's necessary to understand the format and how to convert it to a usable format.
|
|
||||||
- The Wii Fit Balance Board uses Bluetooth 2.0 to communicate. Pepper uses Bluetooth 4.0 this means that there might be compatibility issues/latancy issues.
|
|
||||||
|
|
||||||
## Implementation
|
## Implementation
|
||||||
|
|
||||||
### Prototyping
|
### Prototyping
|
||||||
|
|
||||||
To start the implementation of the system, we will create a prototype that will read the sensor data from the Wii Fit Board and send it to your computer. Once we have the data, we will develop the position tracking algorithm to determine the user's position. After that, the algorithm will be integrated with pepper.
|
To be added
|
||||||
|
|
||||||
### Testing and Validation
|
### Testing and Validation
|
||||||
|
|
||||||
Tests:
|
To be added
|
||||||
- Test the prototype to ensure that it can read the sensor data from the Wii Fit Board.
|
|
||||||
- Test the position tracking algorithm to ensure that it can determine the user's position accurately.
|
|
||||||
- Test the integrated system to ensure that it can track the user's position in real-time.
|
|
||||||
|
|
||||||
## Conclusion
|
## Conclusion
|
||||||
|
|
||||||
@@ -79,9 +83,8 @@ To be added
|
|||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
[Wiiboard lib](https://code.google.com/archive/p/wiiboard-simple/wikis/Documentation.wiki)
|
[Bluetooth Discovery](https://developer.android.com/develop/connectivity/bluetooth/find-bluetooth-devices)
|
||||||
[BlueSoil](https://advanti-lab.sb.dfki.de/?page_id=64)
|
|
||||||
[FitScales](https://github.com/paulburton/fitscales)
|
## Appendices
|
||||||
[WiiRemoteJ](https://github.com/micromu/WiiRemoteJ)
|
|
||||||
[Wiibrew Wiimote](https://wiibrew.org/wiki/Wiimote)
|
To be added
|
||||||
[Wiibrew Balance Board](https://wiibrew.org/wiki/Wii_Balance_Board)
|
|
Reference in New Issue
Block a user