BlueZ版本 :2.25
arm linux版本 :2.6.17.8
bluez使用的重點就是配置問題,這裡主要講rfcomm的使用,就是藍芽類比串口的使用.
## HCI daemon configuration file.## HCId optionsoptions { # Automatically initialize new devices autoinit yes; # Security Manager mode # none - Security manager disabled # auto - Use local PIN for incoming connections # user - Always ask user for a PIN # security user; # Pairing mode # none - Pairing disabled # multi - Allow pairing with already paired devices # once - Pair once and deny successive attempts pairing multi; # PIN helper pin_helper /home/pin; # D-Bus PIN helper #dbus_pin_helper;}# Default settings for HCI devicesdevice { # Local device name # %d - device id # %h - host name name "Bluez (%d)"; # Local device class class 0x3e0100; # Default packet type #pkt_type DH1,DM1,HV1; # Inquiry and Page scan iscan enable; pscan enable; # Default link mode # none - no specific policy # accept - always accept incoming connections # master - become master on incoming connections, # deny role switch on outgoing connections lm accept; # Default link policy # none - no specific policy # rswitch - allow role switch # hold - allow hold mode # sniff - allow sniff mode # park - allow park mode lp rswitch,hold,sniff,park; # Authentication and Encryption (Security Mode 3) #auth enable; #encrypt enable;}
關鍵的修改是:
security auto; ---> security user;//auto極不穩定
pin_helper /usr/sbin/pin; --->pin_helper /home/pin;//只是便於自己修改
其中/home/pin就是個簡單的指令碼:
#!/bin/shecho "PIN:0000"
如果要修改密碼就修改PIN:後面的部分.
1.載入初始設定檔案:
hcid -f /etc/bluetooth/hcid.conf
2.設定SDP:
sdpdsdptool add SP
其中sdptool add SP預設使用的是channel 1,如果設定其他具體的channel就應該是 sdptool add --channel=x SP,x就是未使用的channel號.
是否添加成功是可以 sdptool browse local來查詢.
3.設定rfcomm後台監聽:
rfcomm listen /dev/rfcomm0 1&
如果你之前有設定channel就將這裡的1換成你的channel號.
以上是針對自己做slave的情況,如果做host,就需要先擷取對方的服務以及channel,初始化依然是上面的第一第二步,第二步也可以不要,第三部換成
sdptool browse XX:XX:XX:XX:XX
XX:XX:XX:XX:XX 是slave的Mac地址,然後可以瀏覽到具體的sever和channel,找到自己需要串連的channel號x,然後串連之:
rfcomm connect 0 XX:XX:XX:XX:XX 1&
其中的1是channel,可以換成其他任意存在的channel號,其中前面的0是指rfcomm0,也可以將其換成/dev/rfcomm0.
以上兩種情況任何一種串連ok,就可以操作rfcomm0口來進行正常串口通訊了.