Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Ble Distance Problem
#10
I refactored your code
also added check that txpower is not 0 (this check is not in your original javascript code)
import blescan
import bluetooth._bluetooth as bluez


def calculate_accuracy(txpower, rssi):
    if rssi == 0 or txpower == 0:
        return -1
    else:
        ratio = rssi/txpower
        if ratio < 1:
            return ratio**10
        else:
            return 0.89976 * ratio**7.7095 + 0.111


def scan_sock(sock):
    blescan.hci_le_set_scan_parameters(sock)
    blescan.hci_enable_le_scan(sock)
    
    while True:
        returnedList = blescan.parse_events(sock, 10)
        print "----------"
        for beacon in returnedList:
            print beacon
            beacon = beacon.split(',')
            beaconid = beacon[0]
            txpower = float(beacon[4])
            rssi = float(beacon[5])
            if 0<= calculate_accuracy(txpower, rssi)calculate_accuracy < 2:
                print beaconid

if __name__ == '__main__':
    dev_id = 0
    try:
        sock = bluez.hci_open_dev(dev_id)
        print "ble thread started"
        scan(sock)
    except:
        print "error accessing bluetooth device..."
Reply


Messages In This Thread
Python Ble Distance Problem - by aleynasarcanli - Feb-09-2018, 01:16 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 02:53 PM
RE: Python Ble Distance Problem - by aleynasarcanli - Feb-09-2018, 08:30 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 08:34 PM
RE: Python Ble Distance Problem - by aleynasarcanli - Feb-09-2018, 09:57 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 10:03 PM
RE: Python Ble Distance Problem - by aleynasarcanli - Feb-09-2018, 10:09 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 10:10 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 10:12 PM
RE: Python Ble Distance Problem - by buran - Feb-09-2018, 10:24 PM
RE: Python Ble Distance Problem - by aleynasarcanli - Feb-09-2018, 10:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Visualize Geo Map/Calculate distance zarize 1 1,954 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  euclidean distance jenya56 3 2,920 Mar-29-2019, 02:56 AM
Last Post: scidam
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,330 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  organizing by distance gonzo620 7 4,085 Oct-16-2018, 01:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020