You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

基于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:

1. Verify BlueZ Service & Adapter Status

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 hci0 if your adapter uses a different ID): hciconfig hci0 up
  • Confirm the adapter is in working order: hciconfig (you should see UP RUNNING next to your adapter)
2. Configure Bluetooth Discoverability

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
3. Register the SPP Service (Critical for iPhone Recognition)

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:
    sdptool add --channel=1 SP
    
    (Channel 1 is the standard for SPP, but you can use any unused channel between 1-30—just remember the number for later steps)
  • Verify the service was added: sdptool browse local
    Look for a Serial Port entry in the output; this confirms the SPP service is visible to nearby devices.
4. Set Up RFCOMM Binding

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:
    rfcomm bind /dev/rfcomm0 hci0 1
    
    (Use the same channel number you set with sdptool)
  • 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.
5. Fix Pairing & Permissions

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 via hciconfig 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" to main.conf (replace 1234 with your preferred code)
6. Test the Connection
  • 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
Quick Troubleshooting Tips
  • If the iPhone can’t find your board: Double-check hciconfig hci0 shows PISCAN mode, and DiscoverableTimeout is set to 0 in main.conf.
  • If pairing succeeds but connection fails: Ensure sdptool added 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/rfcomm0 to rule out permission issues.

内容的提问来源于stack exchange,提问作者Subbu

火山引擎 最新活动