RaspberryPiを使い、アドバタイズ信号を送信している特定のBluetooth端末のRSSIだけを読み出す以下のプログラムをpythonで書こうとしていますが、上手く動作しません。
*RaspberryPiから直接sudo btmonを実行した状態で、sudo hcitool lescanを実行すると、周辺のBLEの情報は見つけることができています。
*プログラム中のMACアドレスの部分には実際には目的のアドレスを記述しています
python
1import subprocess, time, os, sys 2 3MAC_ADDR = "XX:XX:XX:XX:XX:XX" 4 5thecommand = "sudo btmon" 6thecommand2 = "sudo hcitool lescan" 7last_seen = 0 8last_rssi = 0 9lastn = 0 10 11cmd = thecommand.split(" ") 12cmd2 = thecommand2.split(" ") 13 14p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) 15 16p2 = subprocess.Popen(cmd2,stdout=subprocess.PIPE,stderr=subprocess.PIPE) 17 18n = 0 19for line in iter(p.stdout.readline, ''): 20 n = n + 1 21 g = 0 22 23 if(MAC_ADDR in line.rstrip()): 24 last_seen = time.time() 25 lastn = n 26 if("RSSI:" in line.rstrip() and (n-lastn)<20): 27 a = line.rstrip() 28 last_rssi = int(a.split("RSSI: ")[1].split("dBm")[0]) 29 g = 1 30 31 if(g==1): 32 print last_rssi
症状
* 上記プログラムを実行すると、btmonの結果が画面に流れた状態でストップする
* プログラムを停止すると、以下のメッセージが出ます
Traceback (most recent call last):
File "testRSSI.py", line 25, in <module>
for line in iter(p.stdout.readline,''):
サブプロセスの扱い方や、出力の取扱の経験が浅いのでご教示よろしくお願いいたします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/25 04:49
2018/04/25 07:16
2018/04/26 02:56
2018/04/26 20:56 編集