<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from openxc . controllers . base import Controller <EOL> from . socket import SocketDataSource <EOL> from . base import DataSourceError <EOL> LOG = logging . getLogger ( __name__ ) <EOL> try : <EOL> import bluetooth <EOL> except ImportError : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> bluetooth = None <EOL> class BluetoothVehicleInterface ( SocketDataSource , Controller ) : <EOL> """<STR_LIT>""" <EOL> OPENXC_DEVICE_NAME_PREFIX = "<STR_LIT>" <EOL> def __init__ ( self , address = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( BluetoothVehicleInterface , self ) . __init__ ( ** kwargs ) <EOL> self . address = address <EOL> if bluetooth is None : <EOL> raise DataSourceError ( "<STR_LIT>" ) <EOL> while self . address is None : <EOL> self . scan_for_bluetooth_device ( ) <EOL> self . connect ( ) <EOL> def connect ( self ) : <EOL> port = <NUM_LIT:1> <EOL> connected = False <EOL> while not connected : <EOL> self . socket = bluetooth . BluetoothSocket ( bluetooth . RFCOMM ) <EOL> try : <EOL> self . socket . connect ( ( self . address , port ) ) <EOL> except IOError as e : <EOL> LOG . warn ( "<STR_LIT>" % self . address , e ) <EOL> else : <EOL> LOG . info ( "<STR_LIT>" , port ) <EOL> connected = True <EOL> def scan_for_bluetooth_device ( self ) : <EOL> nearby_devices = bluetooth . discover_devices ( ) <EOL> self . address = None <EOL> device_name = None <EOL> for address in nearby_devices : <EOL> device_name = bluetooth . lookup_name ( address ) <EOL> if ( device_name is not None and <EOL> device_name . startswith ( self . OPENXC_DEVICE_NAME_PREFIX ) ) : <EOL> self . address = address <EOL> break <EOL> if self . address is not None : <EOL> LOG . info ( "<STR_LIT>" % ( device_name , self . address ) ) <EOL> else : <EOL> LOG . info ( "<STR_LIT>" ) </s>
