Bluetooth 使用rfcomm检查连接是否成功

Bluetooth 使用rfcomm检查连接是否成功,bluetooth,raspberry-pi,wireless,rfcomm,dongle,Bluetooth,Raspberry Pi,Wireless,Rfcomm,Dongle,我正试图用蓝牙加密狗将我的手机连接到我的RaspberryPi上(不想做任何惊天动地的事情,只想确定我的手机何时在该地区)。如果我打开手机的蓝牙并发出以下命令,我会得到以下输出(在有人开始向我鼓吹这是如何违反安全之前,让我提醒你,这不是我的实际手机蓝牙id): 命令: sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10 echo $? Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10 Press

我正试图用蓝牙加密狗将我的手机连接到我的RaspberryPi上(不想做任何惊天动地的事情,只想确定我的手机何时在该地区)。如果我打开手机的蓝牙并发出以下命令,我会得到以下输出(在有人开始向我鼓吹这是如何违反安全之前,让我提醒你,这不是我的实际手机蓝牙id):

命令:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10
Press CTRL-C for hangup
0
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Can't connect RFCOMM socket: Host is down
0
#!/bin/bash

phone1="AA:BB:CC:DD:EE:FF" #Address of phone
inside=1  # Whether the phone is 'inside' the house (0) or 'outside (1)

phoneDetected ()
{
   # Search for phone
   hcitool rssi $phone1 &>/dev/null
   ret=$?

   # If search was unsuccessful,
   if [ $ret -ne 0 ]
   then
      # Add phone
      sudo rfcomm connect 0 $phone1 10 &>/dev/null &

      # Note: the return code of rfcomm will almost always be 0,
      # so don't rely on it if you are looking for failed connections,
      # instead wait 5 seconds for rfcomm to connect, then check
      # connection again. Note this is not fool proof as an rfcomm
      # command taking longer than 5 seconds could break this program,
      # however, it generally only takes 2 seconds.
      sleep 5
      hcitool rssi $phone1 &>/dev/null
      ret=$?
   fi;

   # Case 1) we are now connected (ret=0) and we were previously outside (inside=1)
   if [ $ret -eq 0 ] && [ $inside -eq 1 ]
   then
      # change state to inside and do something (I am playing a song)
      inside=0
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/1.mp3 &>/dev/null
   # Case 2) we are no longer connected (ret=1) but we were previously inside (inside=0)
   elif [ $ret -eq 1 ] && [ $inside -eq 0 ]
   then
      # change state to outside and do something (I am playing another song)
      inside=1
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/2.mp3 &>/dev/null
   fi;
}

# run an infinite loop
while :
do
   phoneDetected $phone1
done
输出:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10
Press CTRL-C for hangup
0
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Can't connect RFCOMM socket: Host is down
0
#!/bin/bash

phone1="AA:BB:CC:DD:EE:FF" #Address of phone
inside=1  # Whether the phone is 'inside' the house (0) or 'outside (1)

phoneDetected ()
{
   # Search for phone
   hcitool rssi $phone1 &>/dev/null
   ret=$?

   # If search was unsuccessful,
   if [ $ret -ne 0 ]
   then
      # Add phone
      sudo rfcomm connect 0 $phone1 10 &>/dev/null &

      # Note: the return code of rfcomm will almost always be 0,
      # so don't rely on it if you are looking for failed connections,
      # instead wait 5 seconds for rfcomm to connect, then check
      # connection again. Note this is not fool proof as an rfcomm
      # command taking longer than 5 seconds could break this program,
      # however, it generally only takes 2 seconds.
      sleep 5
      hcitool rssi $phone1 &>/dev/null
      ret=$?
   fi;

   # Case 1) we are now connected (ret=0) and we were previously outside (inside=1)
   if [ $ret -eq 0 ] && [ $inside -eq 1 ]
   then
      # change state to inside and do something (I am playing a song)
      inside=0
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/1.mp3 &>/dev/null
   # Case 2) we are no longer connected (ret=1) but we were previously inside (inside=0)
   elif [ $ret -eq 1 ] && [ $inside -eq 0 ]
   then
      # change state to outside and do something (I am playing another song)
      inside=1
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/2.mp3 &>/dev/null
   fi;
}

# run an infinite loop
while :
do
   phoneDetected $phone1
done
现在,如果我关闭手机的蓝牙并发出相同的命令,我会得到以下输出(同样,所有id都已更改以保护无辜者)

命令:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10
Press CTRL-C for hangup
0
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Can't connect RFCOMM socket: Host is down
0
#!/bin/bash

phone1="AA:BB:CC:DD:EE:FF" #Address of phone
inside=1  # Whether the phone is 'inside' the house (0) or 'outside (1)

phoneDetected ()
{
   # Search for phone
   hcitool rssi $phone1 &>/dev/null
   ret=$?

   # If search was unsuccessful,
   if [ $ret -ne 0 ]
   then
      # Add phone
      sudo rfcomm connect 0 $phone1 10 &>/dev/null &

      # Note: the return code of rfcomm will almost always be 0,
      # so don't rely on it if you are looking for failed connections,
      # instead wait 5 seconds for rfcomm to connect, then check
      # connection again. Note this is not fool proof as an rfcomm
      # command taking longer than 5 seconds could break this program,
      # however, it generally only takes 2 seconds.
      sleep 5
      hcitool rssi $phone1 &>/dev/null
      ret=$?
   fi;

   # Case 1) we are now connected (ret=0) and we were previously outside (inside=1)
   if [ $ret -eq 0 ] && [ $inside -eq 1 ]
   then
      # change state to inside and do something (I am playing a song)
      inside=0
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/1.mp3 &>/dev/null
   # Case 2) we are no longer connected (ret=1) but we were previously inside (inside=0)
   elif [ $ret -eq 1 ] && [ $inside -eq 0 ]
   then
      # change state to outside and do something (I am playing another song)
      inside=1
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/2.mp3 &>/dev/null
   fi;
}

# run an infinite loop
while :
do
   phoneDetected $phone1
done
输出:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10
Press CTRL-C for hangup
0
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Can't connect RFCOMM socket: Host is down
0
#!/bin/bash

phone1="AA:BB:CC:DD:EE:FF" #Address of phone
inside=1  # Whether the phone is 'inside' the house (0) or 'outside (1)

phoneDetected ()
{
   # Search for phone
   hcitool rssi $phone1 &>/dev/null
   ret=$?

   # If search was unsuccessful,
   if [ $ret -ne 0 ]
   then
      # Add phone
      sudo rfcomm connect 0 $phone1 10 &>/dev/null &

      # Note: the return code of rfcomm will almost always be 0,
      # so don't rely on it if you are looking for failed connections,
      # instead wait 5 seconds for rfcomm to connect, then check
      # connection again. Note this is not fool proof as an rfcomm
      # command taking longer than 5 seconds could break this program,
      # however, it generally only takes 2 seconds.
      sleep 5
      hcitool rssi $phone1 &>/dev/null
      ret=$?
   fi;

   # Case 1) we are now connected (ret=0) and we were previously outside (inside=1)
   if [ $ret -eq 0 ] && [ $inside -eq 1 ]
   then
      # change state to inside and do something (I am playing a song)
      inside=0
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/1.mp3 &>/dev/null
   # Case 2) we are no longer connected (ret=1) but we were previously inside (inside=0)
   elif [ $ret -eq 1 ] && [ $inside -eq 0 ]
   then
      # change state to outside and do something (I am playing another song)
      inside=1
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/2.mp3 &>/dev/null
   fi;
}

# run an infinite loop
while :
do
   phoneDetected $phone1
done
因为我试图确定手机何时在房间里,何时离开,所以我需要某种方法(其他方法)来检测加密狗何时可以连接到房间,何时不能连接到房间。我如何才能做到这一点?(注意:我试着把手机从大楼里拿出来,甚至把它完全关掉)

EDIT:我考虑捕获
stderr
消息并进行测试

error=$`sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10 >/dev/null` &
if [ $error=="Can't connect RFCOMM socket: Host is down" ]
then
    ...
fi;

但问题是rfcomm必须在后台运行。

我还没有完全弄清楚如何做到这一点,但我就是这样解决的。在执行
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
命令后,我只需等待5秒钟,然后检查是否存在连接。我怀疑这实际上是完美的,因为下一次迭代将捕获任何错误,但不要引用我的话。也许是更多的经验。我已经包括了最小工作示例(MWE),因此您可以遵循它

MWE:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Connected /dev/rfcomm0 to AA:BB:CC:DD:EE:FF on channel 10
Press CTRL-C for hangup
0
sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 10
echo $?
Can't connect RFCOMM socket: Host is down
0
#!/bin/bash

phone1="AA:BB:CC:DD:EE:FF" #Address of phone
inside=1  # Whether the phone is 'inside' the house (0) or 'outside (1)

phoneDetected ()
{
   # Search for phone
   hcitool rssi $phone1 &>/dev/null
   ret=$?

   # If search was unsuccessful,
   if [ $ret -ne 0 ]
   then
      # Add phone
      sudo rfcomm connect 0 $phone1 10 &>/dev/null &

      # Note: the return code of rfcomm will almost always be 0,
      # so don't rely on it if you are looking for failed connections,
      # instead wait 5 seconds for rfcomm to connect, then check
      # connection again. Note this is not fool proof as an rfcomm
      # command taking longer than 5 seconds could break this program,
      # however, it generally only takes 2 seconds.
      sleep 5
      hcitool rssi $phone1 &>/dev/null
      ret=$?
   fi;

   # Case 1) we are now connected (ret=0) and we were previously outside (inside=1)
   if [ $ret -eq 0 ] && [ $inside -eq 1 ]
   then
      # change state to inside and do something (I am playing a song)
      inside=0
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/1.mp3 &>/dev/null
   # Case 2) we are no longer connected (ret=1) but we were previously inside (inside=0)
   elif [ $ret -eq 1 ] && [ $inside -eq 0 ]
   then
      # change state to outside and do something (I am playing another song)
      inside=1
      mplayer /home/pi/documents/rasbpi/raspi1/media/audio/2.mp3 &>/dev/null
   fi;
}

# run an infinite loop
while :
do
   phoneDetected $phone1
done