Bluetooth pair use command line

This article explains how to set up a bluetooth connection between your ubuntu laptop and phone and get PPP working with BT's mobile service in the UK. It probably isn't so hard to repeat this for other network providers. I'm sure that if you do a Yahoo search for the GPRS settings of your mobile telco, you'll be up and runnin with little more than a few mintues of poking around.

First you need to install the bluetooth packages on your linux machine:
$sudo apt-get install bluez-utils
$sudo apt-get install blues-pin

Now you need to make sure you have the ppp package installed

$sudo apt-get install ppp


At this point you already have enough software on your machine to do a scan of the local area and see what devices are available. this is done using hcitool. When I run this command, the output looks like this:
$chris@snackerjack-lx:~$ hcitool scan
Scanning ...
        00:07:3A:08:EE:74       n/a
        00:18:13:50:0C:EB       Christo Yahoo!
chris@snackerjack-lx:~$

That means that my laptop can see my phone - it's reporting the MAC address and the device name.

For more information on hcitool, you can run 'man hcitool'. Basically hcitool is a handy bluetooth utility which allows you to scan for and query local bluetooth devices. At this stage you can use hcitool to pair your bluetooth-enabled laptop with your phone. Use the following commands to achieve this:
$sudo hcitool cc 00:18:13:50:0C:EB

Note, you should use the MAC address of your own phone in this command - as reported by the hcitool scan command earlier. This creates a baseband connection to your phone. The next step is:
$sudo hcitool auth 00:18:13:50:0C:EB

This will request authentication with your phone - this is known as 'pairing' and will allow your computer to communicate with your bluetooth phone. Note again, you should replace the mac address with that of your phone. Don't use mine!


The next stage is to use the Service Discovery Protocol to ask your device what bluetooth services it is offering. At this point, if you're feeling really curious, you could re-run your hcitool scan and then run an SDP discovery search on all the listed devices. You never know - it might be interesting. However, this article is about setting up a Dial-up connection through your phone, so let's keep focussed.. The command you need to issue looks like this:
$sdptool browse 00:18:13:50:0C:EB

Again, use your own phone's MAC address - This will list all sorts of bluetooth services that your phone offers. This is what the output looks like when I run this command against my own phone.. I know it's a lot to paste, but it's worth seeing the kind of services which might be on offer:
chris@snackerjack-lx:~$ sdptool browse 00:18:13:50:0C:EB
Browsing 00:18:13:50:0C:EB ...
Service Description: Sony Ericsson K750
Service RecHandle: 0x10000
Service Class ID List:
  "PnP Information" (0x1200)

Service Name: Dial-up Networking
Service RecHandle: 0x10001
Service Class ID List:
  "Dialup Networking" (0x1103)
  "Generic Networking" (0x1201)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 1
Profile Descriptor List:
  "Dialup Networking" (0x1103)
    Version: 0x0100

Service Name: Serial Port
Service RecHandle: 0x10002
Service Class ID List:
  "Serial Port" (0x1101)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 2

Service Name: HF Voice Gateway
Service RecHandle: 0x10003
Service Class ID List:
  "Handfree Audio Gateway" (0x111f)
  "Generic Audio" (0x1203)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 3
Profile Descriptor List:
  "Handsfree" (0x111e)
    Version: 0x0101

Service Name: HS Voice Gateway
Service RecHandle: 0x10004
Service Class ID List:
  "Headset Audio Gateway" (0x1112)
  "Generic Audio" (0x1203)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 4
Profile Descriptor List:
  "Headset" (0x1108)
    Version: 0x0100

Service Name: OBEX Object Push
Service RecHandle: 0x10005
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 5
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Service Name: OBEX File Transfer
Service RecHandle: 0x10006
Service Class ID List:
  "OBEX File Transfer" (0x1106)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 6
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX File Transfer" (0x1106)
    Version: 0x0100

Service Name: OBEX SyncML Client
Service RecHandle: 0x10007
Service Class ID List:
  "Error: This is UUID-128" (0x00000002-0000-1000-8000-0002ee000002)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 7
  "OBEX" (0x0008)

Service Name: OBEX IrMC Sync Server
Service RecHandle: 0x10008
Service Class ID List:
  "IrMC Sync" (0x1104)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 8
  "OBEX" (0x0008)
Profile Descriptor List:
  "IrMC Sync" (0x1104)
    Version: 0x0100

Service Name: Mouse & Keyboard
Service Description: Remote Control
Service Provider: Sony Ericsson
Service RecHandle: 0x10009
Service Class ID List:
  "Human Interface Device" (0x1124)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 17
  "HIDP" (0x0011)
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "Human Interface Device" (0x1124)
    Version: 0x0100

chris@snackerjack-lx:~$

That's pretty cool - First you can see that I'm using a Sony Ericsson K750 phone and then on every bluetooth 'Service Name:' line, you can see the name of a service on offer. The one we are most interested in is the 'Dial-up Networking' service, however, we can also use this device as a voice gateway, and it will support OBEX transfer requests (ie the exchange of binary objects from other bluetooth devices). I can also use this phone as an input device (service 'Mouse & Keyboard') - note that this last service describes itself as 'Remote Control' and yes, it can be used in precisely that way - so the phone will suddenly become a pointer/mouse within the PAN. I will explain this in another article.

Okay, so where are we now? Well the next step is simply to tell your system which channel you want to talk RFCOMM. RFCOMM is a special bluetooth serial port emulation over radio frequency (hence the name rf communication). It is quite literally an implementation of the RS232 serial protocol over radio. Bluetooth can handle several rfcomm channels consecutively. We just have to decide which one we're going to use for this exercise.. This is dead easy. Just look at the output of the sdp search which you ran just now and look at the RFCOMM channel number in the 'Dial up Networking' service section. In the case above, it's channel 1.

All you do now is specify that channel in your rfcomm.conf - so on my system that looks like this:
rfcomm0 {
        bind yes;
        device 00:18:13:50:0C:EB;
        channel 1;
        comment "PPP connect";
}

At this point, you can set up /dev/rfcomm0 by running the following command:
$sudo /etc/init.d/bluez-utils restart

We're nearly there. Now that you have your rfcomm node fully configured and your phone and computer are paired, all you need to do is set up the PPP and chat settings on your computer. Chat sets up a ppp connection between your modem and a remote ppp service based on rules which you define in a chatscript.

First create the file /etc/ppp/peers/bluetoothconn and put the following into it:
debug
noauth
connect "/usr/sbin/chat -v -f /etc/chatscripts/bluetoothconn"
usepeerdns
/dev/rfcomm0 115200
defaultroute
crtscts
lcp-echo-failure 0

Now edit the file /etc/chatscripts/bluetoothconn and make sure it contains the following:
TIMEOUT 35
ECHO    ON
ABORT   'nBUSYr'
ABORT   'nERRORr'
ABORT   'nNO ANSWERr'
ABORT   'nNO CARRIERr'
ABORT   'nNO DIALTONEr'
ABORT   'nRINGINGrnrnRINGINGr'
''      rAT
OK      'AT+CGDCONT=2,"IP","btmobile.bt.com"'
OK      ATD*99***2#
CONNECT ""

It took me a while to get this configuration to work. I tried several settings for the data profile number, without any real idea of what it should be - and found that ***2 worked. You might have to play around with this for your own telco - try swapping the '2' for any integer between '1' and '4', or even remove the whole lot and just terminate with a '#'.

To bring the connection up, just run the command:
$pon bluetoothconn

and to turn it off again:
$poff Bluetoothconn

It's as simple as that. If you still have questions about your bluetooth and GPRS setup with linux on your laptop and trying to talk to BT, please reply to this and let me know.

Hope that helps.

christo

Follow me on twitter to catch all my tech updates
http://www.twitter.com/planet_guru
<think>我们注意到用户遇到了'pair: command not found'错误,这表明用户可能直接在终端中输入了`pair`命令,但该命令并不是一个独立的可执行程序。实际上,`pair`是`bluetoothctl`交互模式中的一个子命令。因此,正确的做法是先进入`bluetoothctl`交互环境,再使用`pair`命令。 下面我们将详细说明如何在树莓派上正确使用`pair`命令连接Xbox手柄,并解决上述错误: ### 步骤1:确保蓝牙服务正常运行 在开始之前,请确保树莓派的蓝牙服务已启动: ```bash sudo systemctl start bluetooth sudo systemctl enable bluetooth # 设置开机自启 ``` ### 步骤2:进入bluetoothctl交互环境 在终端中输入以下命令进入蓝牙控制台: ```bash bluetoothctl ``` 此时提示符会变为 `[bluetooth]#`,表示已进入交互模式。 ### 步骤3:启用代理并扫描设备 在`bluetoothctl`环境中执行: ```bash power on # 确保蓝牙适配器开启 agent on # 启用代理处理配对请求 default-agent # 设置默认代理 scan on # 开始扫描设备 ``` ### 步骤4:让手柄进入配对模式 长按Xbox手柄顶部的**配对按钮**(位于USB接口旁边)3秒钟,直到手柄上的Xbox标志灯开始快速闪烁。 ### 步骤5:获取手柄MAC地址 在`bluetoothctl`的扫描结果中查找类似以下的行: ``` [NEW] Device AA:BB:CC:DD:EE:FF Xbox Wireless Controller ``` 记录其中的MAC地址(示例中的`AA:BB:CC:DD:EE:FF`)。 ### 步骤6:执行配对命令 ```bash pair AA:BB:CC:DD:EE:FF # 替换为实际MAC地址 ``` 如果提示配对成功(`Pairing successful`),继续下一步。 ### 步骤7:信任并连接设备 ```bash trust AA:BB:CC:DD:EE:FF # 设置设备为受信任 connect AA:BB:CC:DD:EE:FF # 连接设备 ``` ### 步骤8:退出并验证 ```bash quit # 退出bluetoothctl ``` 验证手柄是否被识别: ```bash ls /dev/input/js* # 应出现类似/dev/input/js0的设备节点 ``` ### 解决'pair: command not found'错误的关键点 - **错误原因**:`pair`命令只能在`bluetoothctl`交互环境中使用,不能直接在终端中运行。 - **正确用法**:先运行`bluetoothctl`进入交互模式,再使用`pair <mac>`。 ### 驱动支持 如果手柄连接后无响应,可能需要安装专用驱动: ```bash # 安装xpadneo驱动(支持Xbox One及更新手柄) sudo apt install dkms git clone https://github.com/atar-axis/xpadneo.git cd xpadneo sudo ./install.sh ``` ### 故障排除表 | 问题现象 | 解决方案 | |----------|----------| | 扫描不到设备 | 确保手柄处于配对模式(指示灯快速闪烁) | | 配对被拒绝 | 运行`remove AA:BB:CC:DD:EE:FF`清除旧记录后重试 | | 连接后无输入 | 检查驱动:`dmesg \| grep -i xpad` | > 💡 **注意**:Xbox Series X/S手柄首次蓝牙配对需通过USB线连接一次主机(非树莓派)激活[^1]。 ### 技术原理简析 配对过程通过蓝牙协议栈的SDP(服务发现协议)完成手柄的HID(人机接口设备)服务注册,建立两条L2CAP通道: - **控制通道**(PSM=0x11):传输HID控制命令 - **中断通道**(PSM=0x13):实时传输输入事件 数据传输采用HID over GATT规范(HOGP)[^2]。 §§相关问题§§ 1. 如何设置树莓派开机自动连接已配对的手柄? 2. 如何测试手柄的按键和摇杆输入? 3. 多个手柄同时连接时如何区分设备顺序?</think>### 解决树莓派连接 XBOX 手柄时 "pair: command not found" 错误 您遇到的错误是因为 `pair` 命令不是独立的终端命令,而是需要在 `bluetoothctl` 交互环境中使用的子命令。以下是完整的解决方案: --- ### 🔧 正确连接步骤(解决命令错误) 1. **启动蓝牙控制台** 在终端输入以下命令进入交互模式: ```bash bluetoothctl ``` 此时提示符会变成 `[bluetooth]#` 2. **启用必要功能** 在 `bluetoothctl` 环境中依次执行: ```bash power on # 开启蓝牙适配器 agent on # 启用配对代理 default-agent # 设为默认代理 scan on # 开始扫描设备 ``` 3. **激活手柄配对模式** 长按 Xbox 手柄顶部的 **配对按钮**(USB 接口旁),直到 Xbox 标志灯快速闪烁。 4. **获取 MAC 地址** 在扫描结果中查找类似输出: ``` [NEW] Device AA:BB:CC:DD:EE:FF Xbox Wireless Controller ``` 记录 `AA:BB:CC:DD:EE:FF` 格式的 MAC 地址 5. **执行配对命令** ```bash pair AA:BB:CC:DD:EE:FF # 替换为你的MAC trust AA:BB:CC:DD:EE:FF # 设为信任设备 connect AA:BB:CC:DD:EE:FF ``` 6. **验证连接** ```bash info AA:BB:CC:DD:EE:FF ``` 检查输出中是否包含: ``` Paired: yes Connected: yes ``` --- ### ⚠️ 关键注意事项 1. **命令使用场景** `pair` 只能在 `bluetoothctl` 环境中使用,直接终端输入会报错: ```bash # 错误方式(导致command not found) $ pair AA:BB:CC:DD:EE:FF # 正确方式 $ bluetoothctl [bluetooth]# pair AA:BB:CC:DD:EE:FF ``` 2. **驱动兼容性** Xbox Series X/S 手柄需要额外驱动: ```bash sudo apt install linux-firmware git clone https://github.com/atar-axis/xpadneo.git cd xpadneo && sudo ./install.sh ``` 3. **权限问题** 如果出现 "Access denied" 错误: ```bash sudo usermod -aG bluetooth $USER # 添加用户到蓝牙组 sudo reboot # 重启生效 ``` --- ### 🔍 连接流程图解 ```mermaid sequenceDiagram 用户->>终端: 输入 bluetoothctl 终端-->>蓝牙服务: 启动交互环境 用户->>手柄: 长按配对键3秒 手柄-->>树莓派: 广播配对信号 用户->>bluetoothctl: pair [MAC] 蓝牙服务-->>手柄: 建立加密连接 手柄-->>树莓派: 返回配对成功 蓝牙服务-->>终端: 显示"Pairing successful" ``` --- ### 🛠️ 故障排除表 | 问题现象 | 解决方案 | |------------------------|-----------------------------------| | 扫描不到设备 | 运行 `sudo hciconfig hci0 reset` | | 配对后立即断开 | 执行 `trust [MAC]` 设为信任设备 | | 手柄已连接但无输入 | 检查 `/dev/input/js0` 文件权限 | | 灯闪烁但无法配对 | 更新固件:`sudo apt upgrade bluez-firmware` | > 💡 **技术原理**:配对过程使用 SMP(安全管理协议)进行 LE Secure Connections 认证,通过椭圆曲线 Diffie-Hellman (ECDH) 交换密钥[^1]。Xbox 手柄的 HID 服务 UUID 为 `00001124-0000-1000-8000-00805f9b34fb`[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值