FreeCell

Card games and solitaire: rules and how to play

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

How to Make a Bluetooth Multiplayer Card Game

· 5 min read · FreeCell

This guide shows you how to build a simple multiplayer card game (like Uno or Crazy Eights) that works over Bluetooth on Android devices. You'll create a basic turn-based game where players can connect, send cards, and see the game state update in real time. We'll use Android Studio, Java/Kotlin, and the Bluetooth Classic API (not BLE, for simplicity). By the end, you'll have a functional prototype you can expand.

What You'll Need

  • Android Studio (latest version)
  • Two Android devices (physical, with Bluetooth enabled) or one device + emulator with Bluetooth support
  • Basic knowledge of Java or Kotlin and Android app development
  • Familiarity with XML layouts and Android activities
  • Patience for debugging Bluetooth (it's finicky)

Step-by-Step

  1. Create a new Android project in Android Studio with an Empty Activity. Name it something like "BluetoothCardGame". Set minimum SDK to 24 (Android 7.0) to simplify Bluetooth permissions.
  2. Add Bluetooth permissions to your AndroidManifest.xml. You'll need BLUETOOTH, BLUETOOTH_ADMIN, and for Android 12+ (API 31) also BLUETOOTH_CONNECT and BLUETOOTH_SCAN. Also request runtime permissions in your MainActivity.
  3. Design the game model in Java/Kotlin: create a Card class (suit, rank, color) and a GameState class (current player, deck, hands, discard pile). Keep it simple: a 52-card deck, no special actions.
  4. Set up the Bluetooth service: create a BluetoothConnectionService class that handles device discovery, pairing, and socket connections. Use a ServerSocket on one device and a client socket on the other. Use UUID "00001101-0000-1000-8000-00805F9B34FB" (standard SPP).
  5. Implement the server role: when a player taps "Host Game", start listening for a connection. Accept the socket and spawn a thread to read/write data. The host maintains the authoritative game state.
  6. Implement the client role: when a player taps "Join Game", scan for paired devices, pick the host, and connect. After connection, send a handshake message like "HELLO" to sync.
  7. Create a simple protocol: define text-based messages: "STATE:" to send full game state, "PLAY:" to play a card, "DRAW" to draw, "TURN" to indicate turn. Use JSON (e.g., Gson) to serialize GameState.
  8. Build the UI: in activity_main.xml, add a RecyclerView for your hand, a TextView for the discard pile, and buttons for "Host", "Join", "Play Card", "Draw". Update the UI on every state change.
  9. Wire up the game logic: in MainActivity, handle button clicks. When a player plays a card, send "PLAY:" to the other device. The receiving device updates its local copy of the state and re-renders.
  10. Handle turn switching: after a move, increment the current player index. The host broadcasts the new state to the client. The client can also send moves, but the host validates them.
  11. Test on two devices: install the APK on both. Pair them via Bluetooth settings first. Then host on one, join on the other. Start with a simple game: each player gets 5 cards, first to empty hand wins.

Common Mistakes

  • Forgetting to request runtime permissions on Android 6+ (API 23) – app crashes on start.
  • Using the wrong UUID – must match on both devices.
  • Doing Bluetooth I/O on the main thread – causes ANR (Application Not Responding). Always use background threads.
  • Not handling socket closure – memory leaks or crashes when disconnecting.
  • Sending large data in one chunk – split or use a length prefix.
  • Ignoring the fact that Bluetooth Classic is not reliable over long distances – keep devices close.

Tips

  • Use a fixed port number (e.g., 1) and a well-known UUID to simplify connection.
  • Test with an emulator and a physical device – the emulator can act as the host.
  • Add a timeout for connection attempts to avoid hanging.
  • Log everything with Log.d() – Bluetooth debugging is much easier with logs.
  • Start with a single-player version to test game logic before adding Bluetooth.
  • Use a simple protocol like JSON lines (each message on a new line) for easy parsing.

Troubleshooting

Connection fails: Ensure both devices are paired in Bluetooth settings. Check that the UUID matches. Restart Bluetooth on both devices. If using Android 12+, make sure you have the BLUETOOTH_CONNECT permission granted.

App crashes on start: Add runtime permission checks for Bluetooth. On Android 12+, you need to request BLUETOOTH_SCAN and BLUETOOTH_CONNECT separately.

Data not received: Verify your input stream is reading correctly. Use a BufferedReader and readLine(). Ensure you're not blocking the main thread.

Game state desyncs: Send the full state after every move, not just the delta. This makes it easier to recover from errors.

Device not discoverable: On Android 10+, you need to enable location services for Bluetooth scanning. Also, ensure your app has ACCESS_FINE_LOCATION permission.

Frequently Asked Questions

Can I use Bluetooth Low Energy (BLE) instead?

Yes, but BLE is more complex and requires a GATT server/client architecture. For a simple game, Bluetooth Classic is easier.

Can I have more than 2 players?

Yes, but you'd need to implement a mesh network or have one device act as a server that relays messages. That's more advanced.

How do I handle disconnections?

Detect socket closure in your read thread and show a message. You can also implement a heartbeat ping every few seconds.

Do I need a server for online play?

No, Bluetooth is local only. For online, you'd need a central server or use a library like Firebase.

Can I use this code for iOS?

No, iOS doesn't support Bluetooth Classic the same way. You'd use Multipeer Connectivity or CoreBluetooth.

How do I make the game more fun?

Add special cards (skip, reverse, wild), a scoring system, or animations. Start with the basics and iterate.