This project demonstrates how to use a Bluetooth Low Energy (BLE) enabled orientation sensor (peripheral) to control a player character in a game built with Flutter and the Flame engine (client).
TThe application runs on the client and, after a button press, scans for a specific BLE peripheral device, connects to it, and subscribes to a characteristic that provides orientation data (pitch, roll, and yaw). This data is then used to control the movement of a player on the screen in real-time.
- Manual BLE Device Scanning & Connection: Scans for and connects to a predefined BLE peripheral after the user presses a scan button.
- Real-time Player Control: Uses live orientation data from the sensor to move the player character.
- State Management with BLoC: Manages the BLE connection and orientation data state using the
flutter_bloc
package. - Game Logic with Flame: Implements the game loop, rendering, and component management using the
Flame engine
. - Clear Separation of Concerns: The project is structured to separate BLE communication, state management, and game logic.
- flutter_ble_sensor_broadcaster
- use a second mobile phone to broadcast orientation data via BLE.
- use the same UUIDs as defined in
lib/constants/ble_constants
.dart.
- TODO: Add example code for broadcasting orientation data from a microcontroller with BLE sensors.
The project is organized into several directories to maintain a clean architecture:
lib/
├── constants/
│ └── ble_constants.dart # UUIDs and device name for the BLE sensor
├── cubit/
│ ├── ble_cubit.dart # BLoC for managing BLE state (scanning, connecting, data)
│ ├── ble_state.dart # States for the BleCubit
│ ├── orientation_cubit.dart # BLoC for managing orientation data
│ └── orientation_state.dart # States for the OrientationCubit
├── game/
│ ├── components/
│ │ ├── ble_controller.dart # Flame component to interact with BleCubit
│ │ ├── motion_controller.dart# Flame component to move the player based on orientation
│ │ ├── player.dart # The player character component
│ │ ├── scan_button.dart # Button component to initiate BLE scanning
│ │ └── status.dart # Text component to display connection status
│ ├── ble_motion_control_game.dart # Main Flame game class
│ └── game.dart # Main game page widget
├── repositories/
│ └── ble_repository.dart # Handles all BLE interactions
└── main.dart # Main entry point of the application
Platform | Supported | Tested |
---|---|---|
Android | ✅ | ✅ |
iOS | ✅ | ❌ |
- Flutter SDK installed.
- A physical Android/iOS device with Bluetooth capabilities (client).
- A companion BLE orientation sensor (peripheral) configured to broadcast with the service and characteristic UUIDs defined in
lib/constants/ble_constants.dart
. The default device name is "FSen".
-
Clone the repository:
git clone https://github.com/IoT-gamer/flutter_flame_ble_motion_control_demo.git cd flutter_flame_ble_controller_demo
-
Install dependencies:
flutter pub get
-
Configure BLE Constants:
Openlib/constants/ble_constants.dart
and update theserviceUuid
,characteristicUuid
, and deviceName
to match your BLE sensor.class BleConstants { static const String serviceUuid = "YOUR_SERVICE_UUID"; static const String characteristicUuid = "YOUR_CHARACTERISTIC_UUID"; static const String deviceName = "YOUR_DEVICE_NAME"; }
- Ensure your BLE orientation sensor (peripheral) is turned on and discoverable.
- Connect your Android/iOS device (client) to your computer.
- Run the app from your terminal:
flutter run
The app will launch and display a "Start Scan" button. Press the button to start scanning for your device and connect to it automatically. Once connected, tilting the peripheral sensor will move the blue circle on the screen.
- Initialization: The main.dart file initializes the app, sets it to fullscreen and landscape mode, and launches the
GamePage
. - State Management Setup: The
GamePage
widget usesMultiBlocProvider
to make theOrientationCubit
andBleCubit
available to the widget tree. - BLE Communication:
- The
BleRepository
handles the low-level BLE operations using theflutter_blue_plus
package. - The
BleCubit
coordinates the BLE logic, such as starting a scan, connecting to a device, and handling incoming data. - The
BleController
component within the Flame game listens to state changes from theBleCubit
. When a device is connected, it discovers the correct service/characteristic and tells the cubit to subscribe.
- The
- Game Logic:
BleMotionControlGame
is the main game class.- When the
BleCubit
receives data, it passes it to theOrientationCubit
. - The
MotionController
component listens for new orientation data from theOrientationCubit
. - It then calculates the player's velocity based on the sensor's pitch and roll, moving the
Player
component accordingly. - The
Status
component displays the current BLE connection status.