Skip to content

Commit

Permalink
Add support for passive bluetooth scan
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRisch committed Jan 28, 2021
1 parent 39183cf commit 5970a39
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/config_template.yaml
Expand Up @@ -16,6 +16,7 @@ basestation:

# ==== FOR V2 BASE STATIONS ONLY ====
attempt_count_scan: 5 # Int >0. Maximum number of attempts to find Base Stations.
scan_type: 'active' # Enum(active, passive). Type of bluetooth scan to use (see https://github.com/DavidRisch/steamvr_utils/issues/3#issuecomment-768580652).

# ==== FOR V1 BASE STATIONS ONLY ====
# (change the 'type' (a few lines up) to 'v1')
Expand Down
16 changes: 15 additions & 1 deletion scripts/basestation_interface/v2_interface.py
Expand Up @@ -42,7 +42,10 @@ def handleDiscovery(self, dev, is_new_dev, is_new_data):
delegate = Delegate()
scanner.withDelegate(delegate)
try:
scanner.scan(2)
scanner.scan(
timeout=2,
passive=self.config.basestation_scan_type() == 'passive'
)
except bluepy.btle.BTLEManagementError as e:
log.e(e)
if 'code: 20, error: Permission Denied' in str(e):
Expand All @@ -55,6 +58,17 @@ def handleDiscovery(self, dev, is_new_dev, is_new_data):
sudo setcap 'cap_net_raw,cap_net_admin+eip' /home/$USER/.local/lib/python3.?/site-packages/bluepy/bluepy-helper
see: https://github.com/IanHarvey/bluepy/issues/313#issuecomment-428324639
''')

elif 'Failed to execute management command \'pasvend\'' in str(e):
raise RuntimeError('''
Passive bluetooth scan failed. Consider editing your config to set basestation:scan_type to 'active'.
Original error: {}'''.format(e))

elif 'Failed to execute management command \'scanend\'' in str(e):
raise RuntimeError('''
Active bluetooth scan failed. Consider editing your config to set basestation:scan_type to 'passive'.
Original error: {}'''.format(e))

else:
raise e

Expand Down
16 changes: 13 additions & 3 deletions scripts/config.py
Expand Up @@ -51,7 +51,7 @@ def basestation_type(self):
basestation_type = self.data['basestation']['type']
valid_types = ['v1', 'v2', 'cmd']
if basestation_type not in valid_types:
raise RuntimeError('Invalid value for basestation.type, valid options: {}'.format(valid_types))
raise RuntimeError('Invalid value for basestation:type, valid options: {}'.format(valid_types))
return basestation_type

return 'v2'
Expand All @@ -68,6 +68,16 @@ def basestation_attempt_count_scan(self):

return 5

def basestation_scan_type(self):
if 'basestation' in self.data and 'scan_type' in self.data['basestation']:
basestation_scan_type = self.data['basestation']['scan_type']
valid_types = ['active', 'passive']
if basestation_scan_type not in valid_types:
raise RuntimeError('Invalid value for basestation:scan_type, valid options: {}'.format(valid_types))
return basestation_scan_type

return 'active'

def basestation_attempt_count_set(self):
if 'basestation' in self.data and 'attempt_count_set' in self.data['basestation']:
return int(self.data['basestation']['attempt_count_set'])
Expand All @@ -84,7 +94,7 @@ def basestation_mac_address(self, mode):
if len(self.data['basestation'][field_name]) > 0:
return self.data['basestation'][field_name]

raise RuntimeError('Value basestation.{} must be set when using V1 Base Stations'.format(field_name))
raise RuntimeError('Value basestation:{} must be set when using V1 Base Stations'.format(field_name))

def basestation_id(self, mode):
if mode not in ['b', 'c']:
Expand All @@ -96,7 +106,7 @@ def basestation_id(self, mode):
if len(self.data['basestation'][field_name]) > 0:
return self.data['basestation'][field_name]

raise RuntimeError('Value basestation.{} must be set when using V1 Base Stations'.format(field_name))
raise RuntimeError('Value basestation:{} must be set when using V1 Base Stations'.format(field_name))

def basestation_command(self, target):
if target not in ['on', 'off']:
Expand Down

0 comments on commit 5970a39

Please sign in to comment.