基于BlueZ栈的Linux嵌入式板SPP蓝牙连接配置需求
Alright, let's get your embedded Linux board set up for SPP (Serial Port Profile) Bluetooth communication with an iPhone. I’ve tackled this exact scenario multiple times with BlueZ, so here’s a step-by-step breakdown that should get you up and running smoothly:
First, make sure your Bluetooth stack is active:
- Check if the Bluetooth service is running:
systemctl status bluetooth - If it’s not running, start it with:
systemctl start bluetooth - Set it to auto-start on boot:
systemctl enable bluetooth - Enable your Bluetooth adapter (replace
hci0if your adapter uses a different ID):hciconfig hci0 up - Confirm the adapter is in working order:
hciconfig(you should seeUP RUNNINGnext to your adapter)
iPhones can only pair with devices that are visible, so tweak these settings:
- Open the main BlueZ config file:
nano /etc/bluetooth/main.conf - Update these parameters:
DiscoverableTimeout = 0(keeps the device permanently discoverable; adjust to a non-zero value if you want auto-disable)PairableTimeout = 0(allows permanent pairing capability)AutoEnable = true(ensures the adapter turns on automatically)
- Restart the Bluetooth service to apply changes:
systemctl restart bluetooth - Force the adapter into discoverable/pairable mode:
hciconfig hci0 piscan
iPhones won’t recognize your device as a serial port unless the SPP service is registered in the Bluetooth service database:
- Add the SPP profile to your local service record:
(Channel 1 is the standard for SPP, but you can use any unused channel between 1-30—just remember the number for later steps)sdptool add --channel=1 SP - Verify the service was added:
sdptool browse local
Look for aSerial Portentry in the output; this confirms the SPP service is visible to nearby devices.
Map the Bluetooth serial port to a system device file so you can interact with it like a regular serial port:
- Bind the RFCOMM device to your adapter and channel:
(Use the same channel number you set withrfcomm bind /dev/rfcomm0 hci0 1sdptool) - Check if the device was created:
ls -l /dev/rfcomm0 - To make this binding persistent across reboots, add the command to
/etc/rc.local(if your system uses it) or create a simple systemd service.
iPhones sometimes fail to pair due to permission issues—here’s how to resolve that:
- Edit the RFCOMM config file:
nano /etc/bluetooth/rfcomm.conf - Add this section (replace
<YOUR_BOARD_MAC>with your adapter’s MAC, found viahciconfig hci0):rfcomm0 { bind yes; device <YOUR_BOARD_MAC>; channel 1; comment "Embedded SPP Serial Port"; } - Give your user access to the serial device:
usermod -aG dialout <YOUR_USERNAME> - (Optional) Set a custom pairing PIN by adding
PinCode = "1234"tomain.conf(replace 1234 with your preferred code)
- Pairing: On your iPhone, open Settings > Bluetooth, find your board’s name (customize it with
hciconfig hci0 name "My SPP Board"if needed), and tap to pair. Enter the PIN you set (or default 0000/1234 if you didn’t customize). - Data Transfers: Use an iPhone app that supports Bluetooth serial (like Serial Bluetooth Terminal). Once connected:
- On your board, receive data with:
cat /dev/rfcomm0 - Send data to the iPhone with:
echo "Hello from Linux!" > /dev/rfcomm0
- On your board, receive data with:
- If the iPhone can’t find your board: Double-check
hciconfig hci0showsPISCANmode, andDiscoverableTimeoutis set to 0 inmain.conf. - If pairing succeeds but connection fails: Ensure
sdptooladded the SPP service correctly, and the RFCOMM channel matches the one in the service record. - If data isn’t sending/receiving: Verify your user has dialout group access, or test with
sudo cat /dev/rfcomm0to rule out permission issues.
内容的提问来源于stack exchange,提问作者Subbu




