Raspberry Pi 2を使って、スマートフォン端末のRSSI値をPythonで取得する方法が分かりません。よい方法があればお教え頂けませんでしょうか？

下記のコードは、Bluetoothを利用してユーザの入退室の時間を記録するものです。  
このコードに、複数のスマートフォン端末がBluetoothの通信範囲に入っている場合、RSSI値が最も高い端末に条件分岐する機能を追加したいです。

環境はPython2.7.9、PyBluez0.2.2を使用しています。

```

    import bluetooth
    import time
    
    print "In/Out Board"
    
    while True:
        print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
    
        result = bluetooth.lookup_name('00:00:00:00:00:00', timeout=5) 
        if (result != None):
            print "John: in"
        else:
            print "John: out"
    
        result = bluetooth.lookup_name('00:00:00:00:00:00', timeout=5)
        if (result != None):
            print "Paul: in"
        else:
            print "Paul: out"
    
        time.sleep(60)
    
```
CC BY-SA 3.0
2016-12-04T07:29:54.757
30882
2022-07-12T18:08:00.850
2016-12-05T03:27:34.000
19798
19798
question
python iphone bluetooth
Pythonを使用して、BluetoothのRSSI値を取得したい。
```

    bluetooth.byte_to_signed_int()
    
```

で取得できるようです。

[こちら](https://github.com/karulis/pybluez/blob/master/examples/advanced/inquiry-
with-rssi.py)の下記のコードを参考にしてみてはいかがでしょうか

```

    def device_inquiry_with_with_rssi(sock):
        # save current filter
        old_filter = sock.getsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, 14)
    
        # perform a device inquiry on bluetooth device #0
        # The inquiry should last 8 * 1.28 = 10.24 seconds
        # before the inquiry is performed, bluez should flush its cache of
        # previously discovered devices
        flt = bluez.hci_filter_new()
        bluez.hci_filter_all_events(flt)
        bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT)
        sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, flt )
    
        duration = 4
        max_responses = 255
        cmd_pkt = struct.pack("BBBBB", 0x33, 0x8b, 0x9e, duration, max_responses)
        bluez.hci_send_cmd(sock, bluez.OGF_LINK_CTL, bluez.OCF_INQUIRY, cmd_pkt)
    
        results = []
    
        done = False
        while not done:
            pkt = sock.recv(255)
            ptype, event, plen = struct.unpack("BBB", pkt[:3])
            if event == bluez.EVT_INQUIRY_RESULT_WITH_RSSI:
                pkt = pkt[3:]
                nrsp = bluetooth.get_byte(pkt[0])
                for i in range(nrsp):
                    addr = bluez.ba2str( pkt[1+6*i:1+6*i+6] )
                    rssi = bluetooth.byte_to_signed_int(
                            bluetooth.get_byte(pkt[1+13*nrsp+i]))
                    results.append( ( addr, rssi ) )
                    print("[%s] RSSI: [%d]" % (addr, rssi))
            elif event == bluez.EVT_INQUIRY_COMPLETE:
                done = True
            elif event == bluez.EVT_CMD_STATUS:
                status, ncmd, opcode = struct.unpack("BBH", pkt[3:7])
                if status != 0:
                    print("uh oh...")
                    printpacket(pkt[3:7])
                    done = True
            elif event == bluez.EVT_INQUIRY_RESULT:
                pkt = pkt[3:]
                nrsp = bluetooth.get_byte(pkt[0])
                for i in range(nrsp):
                    addr = bluez.ba2str( pkt[1+6*i:1+6*i+6] )
                    results.append( ( addr, -1 ) )
                    print("[%s] (no RRSI)" % addr)
            else:
                print("unrecognized packet type 0x%02x" % ptype)
            print("event ", event)
    
    
        # restore old filter
        sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter )
    
        return results
    
```
CC BY-SA 4.0
2017-04-09T15:32:44.283
33890
2018-08-28T03:05:37.283
2018-08-28T03:05:37.283
29826
21437
30882
answer