Skip to content

Instantly share code, notes, and snippets.

@typicalfo
Created June 22, 2025 18:32
Show Gist options
  • Select an option

  • Save typicalfo/5db70de87f930e7ac043aeb321be142c to your computer and use it in GitHub Desktop.

Select an option

Save typicalfo/5db70de87f930e7ac043aeb321be142c to your computer and use it in GitHub Desktop.
Flipper zero connect to raspberry pi
let bluetooth = require("bluetooth");
let textbox = require("textbox");
let dialog = require("dialog");
let deviceName = "RaspberryPi-Zero2";
let serviceUUID = "12345678-1234-1234-1234-123456789abc";
function connectToRaspberryPi() {
textbox.setConfig("end", "text");
textbox.addText("Flipper Bluetooth Client\n");
textbox.addText("Scanning for devices...\n");
textbox.show();
let devices = bluetooth.scan();
let targetDevice = null;
for (let i = 0; i < devices.length; i++) {
if (devices[i].name === deviceName) {
targetDevice = devices[i];
break;
}
}
if (!targetDevice) {
textbox.addText("Pi not found. Make sure it's discoverable.\n");
delay(3000);
return;
}
textbox.addText("Found Pi: " + targetDevice.address + "\n");
textbox.addText("Connecting...\n");
let connection = bluetooth.connect(targetDevice.address, serviceUUID);
if (connection) {
textbox.addText("Connected successfully!\n");
handleConnection(connection);
} else {
textbox.addText("Connection failed.\n");
}
}
function handleConnection(connection) {
textbox.addText("Ready to communicate.\n");
textbox.addText("Press OK to send hello message.\n");
let choice = dialog.message("Send Message", "Send 'Hello from Flipper'?");
if (choice === "Yes") {
let message = "Hello from Flipper Zero!";
bluetooth.write(connection, message);
textbox.addText("Sent: " + message + "\n");
let response = bluetooth.read(connection);
if (response) {
textbox.addText("Received: " + response + "\n");
}
}
textbox.addText("Press Back to disconnect.\n");
delay(5000);
bluetooth.disconnect(connection);
textbox.addText("Disconnected.\n");
}
connectToRaspberryPi();
#!/usr/bin/env python3
import bluetooth
import threading
import time
import sys
SERVICE_UUID = "12345678-1234-1234-1234-123456789abc"
DEVICE_NAME = "RaspberryPi-Zero2"
class BluetoothServer:
def __init__(self):
self.server_socket = None
self.client_socket = None
self.running = False
def setup_server(self):
try:
self.server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = bluetooth.PORT_ANY
self.server_socket.bind(("", port))
self.server_socket.listen(1)
bluetooth.advertise_service(
self.server_socket,
DEVICE_NAME,
service_id=SERVICE_UUID,
service_classes=[SERVICE_UUID, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE]
)
print(f"Bluetooth server started on port {self.server_socket.getsockname()[1]}")
print(f"Device name: {DEVICE_NAME}")
print(f"Service UUID: {SERVICE_UUID}")
print("Waiting for Flipper Zero connection...")
return True
except Exception as e:
print(f"Error setting up server: {e}")
return False
def handle_client(self):
try:
while self.running:
data = self.client_socket.recv(1024)
if not data:
break
message = data.decode('utf-8').strip()
print(f"Received from Flipper: {message}")
response = f"Pi received: {message}"
self.client_socket.send(response.encode('utf-8'))
print(f"Sent response: {response}")
except Exception as e:
print(f"Error handling client: {e}")
finally:
self.disconnect_client()
def wait_for_connection(self):
try:
self.client_socket, client_info = self.server_socket.accept()
print(f"Flipper Zero connected from {client_info}")
self.running = True
client_thread = threading.Thread(target=self.handle_client)
client_thread.daemon = True
client_thread.start()
return True
except Exception as e:
print(f"Error accepting connection: {e}")
return False
def disconnect_client(self):
if self.client_socket:
try:
self.client_socket.close()
print("Client disconnected")
except:
pass
self.client_socket = None
self.running = False
def shutdown(self):
self.running = False
self.disconnect_client()
if self.server_socket:
try:
self.server_socket.close()
print("Server shutdown complete")
except:
pass
def main():
server = BluetoothServer()
if not server.setup_server():
print("Failed to setup Bluetooth server")
sys.exit(1)
try:
while True:
if server.wait_for_connection():
print("Connection established. Press Ctrl+C to stop server.")
while server.running:
time.sleep(1)
else:
print("Failed to accept connection. Retrying...")
time.sleep(2)
except KeyboardInterrupt:
print("\nShutting down server...")
server.shutdown()
sys.exit(0)
except Exception as e:
print(f"Unexpected error: {e}")
server.shutdown()
sys.exit(1)
if __name__ == "__main__":
main()
@typicalfo

Copy link
Copy Markdown
Author

Anyone see the issue?

If you want to connect these two devices to use their bluetooth capabilities, maybe don't connect them with bluetooth. This script is 100% wrong for the original idea.

@typicalfo

Copy link
Copy Markdown
Author

And completely unnecessary, to boot. Flipper and Pi zero 2 both have full bluetooth stacks. Now, if you want 2 bluetooth adapters, that is a different story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment