I want to use Python to get the RSSI value for Bluetooth.

Asked 2 years ago, Updated 2 years ago, 106 views

I don't know how to use Raspberry Pi2 to get RSSI values for smartphone devices in Python. Could you tell me if there is a good way?

The code below uses Bluetooth to record the time a user enters or leaves the room.
I would like to add a conditional branching function to this code to the terminal with the highest RSSI value when multiple smartphone terminals are in Bluetooth coverage.

My environment is Python 2.7.9, PyBluez 0.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)

python iphone bluetooth

2022-09-29 21:27

1 Answers

bluetooth.byte_to_signed_int()

It appears to be available in .

Why don't you refer to the code below here?

def device_quiry_with_with_rssi(sock):
    # save current filter
    old_filter=sock.getsockopt (bluez.SOL_HCI, bluez.HCI_FILTER, 14)

    # perform a device query on bluetooth device#0
    # The inquiry should last 8*1.28 = 10.24 seconds
    # before the query 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_pttype(flt,bluez.HCI_EVENT_PKT)
    sock.setsockopt(bluez.SOL_HCI,bluez.HCI_FILTER,flt)

    duration = 4
    max_responses = 255
    cmd_pkt = structure.pack ("BBBBBB", 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=structure.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))
        elifevent==bluez.EVT_INQUIRY_COMPLETE:
            done = True
        elifevent==bluez.EVT_CMD_STATUS:
            status, ncmd, opcode=struct.unpack("BBH", pkt [3:7])
            if status!=0:
                print("uhoh...")
                printpacket (pkt [3:7])
                done = True
        elifevent==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


2022-09-29 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.