예제 #1
0
 def ble_scan(self,
              duration,
              result_format=BleInterface.SCAN_RESULT_FORMAT_DEFAULT):
     """ Function defined in GapInterface. """
     if not self._enable_bluetooth():
         return None
     command = '+SRBLESCAN'
     args = [duration, int(result_format)]
     if result_format == BleInterface.SCAN_RESULT_FORMAT_DEFAULT:
         if self._write(command, args, duration * 2) is EUL_RESULT_SUCCESS:
             regex = r"\"([\w|:]{17})\",([0|1]),-(\d+),(\d+)(,\"(.+)\")?"
             responses = self._serial.serial_search_regex_all(regex)
             return [
                 BleInterface.ScanResult(
                     Euler._convert_euler_address_to_standard(resp[0]),
                     int(resp[1]), int(resp[2]), int(resp[3]), resp[4])
                 for resp in responses
             ]
     elif result_format == BleInterface.SCAN_RESULT_FORMAT_RAW_DATA:
         if self._write(command, args, duration * 2) is EUL_RESULT_SUCCESS:
             regex = r"\"([\w|:]{17})\",([0|1]),-(\d+),\"((\\[0-9A-F]{2})+)\""
             responses = self._serial.serial_search_regex_all(regex)
             return [BleInterface.ScanRawResult( \
                         Euler._convert_euler_address_to_standard(resp[0]),
                         int(resp[1]),
                         int(resp[2]),
                         Euler._convert_escaped_string_to_data(resp[3]))
                     for resp in responses]
     else:
         raise ValueError('invalid result_format')
     return None
예제 #2
0
 def ble_scan(self,
              duration,
              result_format=BleInterface.SCAN_RESULT_FORMAT_DEFAULT):
     """Function defined in GapInterface."""
     command = 'SCAN'
     args = [duration]
     if result_format == BleInterface.SCAN_RESULT_FORMAT_DEFAULT:
         if self._execute(
                 command, args, success_string='SCAN_OK',
                 timeout=duration + 1) is BC127_RESULT_SUCCESS:
             regex = r"SCAN (\w{12}) (0|1) <(.+)> ([0-9A-F]{2}) -(\d+)dBm" + BC127_EOL
             responses = self._serial.serial_search_regex_all(regex)
             return [BleInterface.ScanResult( \
                             Melody._convert_melody_address_to_standard(resp[0]),
                             int(resp[1]),
                             int(resp[4]),
                             int(resp[3], 16),
                             resp[2])
                     for resp in responses]
     elif result_format == BleInterface.SCAN_RESULT_FORMAT_RAW_DATA:
         args.append('ON')
         if self._execute(
                 command, args, success_string='SCAN_OK',
                 timeout=duration + 1) is BC127_RESULT_SUCCESS:
             scan_raw_regex = r"SCAN_RAW (\w{12}) (\d) -(\d+)dBm (\d+) ([0-9A-F| ]+)" + BC127_EOL
             responses = self._serial.serial_search_regex_all(
                 scan_raw_regex)
             return [BleInterface.ScanRawResult( \
                            Melody._convert_melody_address_to_standard(resp[0]),
                            int(resp[1]),
                            int(resp[2]),
                            [int(x, 16) for x in resp[4].split()])
                     for resp in responses]
     else:
         raise ValueError('invalid result_format')
     return None