def _ble_evt_attclient_find_information_found(self, args): """ Handles the event for characteristic discovery. Adds the characteristic to the dictionary of characteristics or adds the descriptor to the dictionary of descriptors in the current characteristic. These events will be occur in an order similar to the following: 1) primary service uuid 2) 0 or more descriptors 3) characteristic uuid 4) 0 or more descriptors 5) repeat steps 3-4 args -- dictionary containing the characteristic handle ('chrhandle'), and characteristic UUID ('uuid') """ raw_uuid = bytearray(reversed(args['uuid'])) # Convert 4-byte UUID shorthand to a full, 16-byte UUID uuid_type = self._get_uuid_type(raw_uuid) if uuid_type != UUIDType.custom: uuid = uuid16_to_uuid( int(bgapi_address_to_hex(args['uuid']).replace(':', ''), 16)) else: uuid = UUID(bytes=bytes(raw_uuid)) # TODO is there a way to get the characteristic from the packet instead # of having to track the "current" characteristic? if uuid_type == UUIDType.descriptor: log.info("Found descriptor %s" % uuid) if self._current_characteristic is not None: self._current_characteristic.add_descriptor( uuid, args['chrhandle']) elif self._current_service is not None: self._current_service.add_descriptor(uuid, args['chrhandle']) elif (uuid_type == UUIDType.custom or uuid_type == UUIDType.nonstandard or uuid_type == UUIDType.characteristic): if uuid_type == UUIDType.custom: log.info("Found custom characteristic %s" % uuid) elif uuid_type == UUIDType.characteristic: log.info("Found approved characteristic %s" % uuid) elif uuid_type == UUIDType.nonstandard: log.info("Found nonstandard 4-byte characteristic %s" % uuid) new_char = Characteristic(uuid, args['chrhandle']) self._current_characteristic = new_char if self._current_service is not None: self._current_service.add_characteristic(new_char)
def test_discover_characteristics(self): device = self._connect() uuid_char = UUID('01234567-0123-0123-0123-0123456789AB') handle_char = 0x1234 uuid_desc = '2902' handle_desc = 0x5678 self.mock_device.stage_discover_characteristics_packets( [str(uuid_char), handle_char, uuid_desc, handle_desc]) characteristics = device.discover_characteristics() eq_(characteristics[uuid_char].handle, handle_char) eq_(characteristics[uuid_char].descriptors[uuid16_to_uuid(0x2902)], handle_desc)
def test_read_nonstandard_4byte_char(self): device = self._connect() uuid_char = 0x03ea handle_char = 0x1234 uuid_desc = '2902' handle_desc = 0x5678 self.mock_device.stage_discover_characteristics_packets( ["03ea", handle_char, uuid_desc, handle_desc]) expected_value = [0xBE, 0xEF, 0x15, 0xF0, 0x0D] self.mock_device.stage_char_read_packets(handle_char, 0x00, expected_value) value = device.char_read(UUID(str(uuid16_to_uuid(uuid_char)))) eq_(bytearray(expected_value), value)
def test_discover_characteristics(self): device = self._connect() uuid_char = UUID('01234567-0123-0123-0123-0123456789AB') handle_char = 0x1234 uuid_desc = '2902' handle_desc = 0x5678 self.mock_device.stage_discover_characteristics_packets([ str(uuid_char), handle_char, uuid_desc, handle_desc]) characteristics = device.discover_characteristics() eq_(characteristics[uuid_char].handle, handle_char) eq_(characteristics[uuid_char].descriptors[uuid16_to_uuid(0x2902)], handle_desc)
def test_read_nonstandard_4byte_char(self): device = self._connect() uuid_char = 0x03ea handle_char = 0x1234 uuid_desc = '2902' handle_desc = 0x5678 self.mock_device.stage_discover_characteristics_packets([ "03ea", handle_char, uuid_desc, handle_desc]) expected_value = [0xBE, 0xEF, 0x15, 0xF0, 0x0D] self.mock_device.stage_char_read_packets( handle_char, 0x00, expected_value) value = device.char_read(UUID(str(uuid16_to_uuid(uuid_char)))) eq_(bytearray(expected_value), value)
def _ble_evt_attclient_find_information_found(self, args): """ Handles the event for characteritic discovery. Adds the characteristic to the dictionary of characteristics or adds the descriptor to the dictionary of descriptors in the current characteristic. These events will be occur in an order similar to the following: 1) primary service uuid 2) 0 or more descriptors 3) characteristic uuid 4) 0 or more descriptors 5) repeat steps 3-4 args -- dictionary containing the characteristic handle ('chrhandle'), and characteristic UUID ('uuid') """ raw_uuid = bytearray(reversed(args['uuid'])) # Convert 4-byte UUID shorthand to a full, 16-byte UUID uuid_type = self._get_uuid_type(raw_uuid) if uuid_type != UUIDType.custom: uuid = uuid16_to_uuid(int( bgapi_address_to_hex(args['uuid']).replace(':', ''), 16)) else: uuid = UUID(bytes=bytes(raw_uuid)) # TODO is there a way to get the characteristic from the packet instead # of having to track the "current" characteristic? if (uuid_type == UUIDType.descriptor and self._current_characteristic is not None): self._current_characteristic.add_descriptor(uuid, args['chrhandle']) elif (uuid_type == UUIDType.custom or uuid_type == UUIDType.nonstandard or uuid_type == UUIDType.characteristic): if uuid_type == UUIDType.custom: log.info("Found custom characteristic %s" % uuid) elif uuid_type == UUIDType.characteristic: log.info("Found approved characteristic %s" % uuid) elif uuid_type == UUIDType.nonstandard: log.info("Found nonstandard 4-byte characteristic %s" % uuid) new_char = Characteristic(uuid, args['chrhandle']) self._current_characteristic = new_char self._characteristics[ args['connection_handle']][uuid] = new_char
def _ble_evt_attclient_group_found(self, args): """ Handles the event for service discovery. Adds the characteristic to the dictionary of characteristics or adds the descriptor to the dictionary of descriptors in the current characteristic. These events will be occur in an order similar to the following: """ log.debug("Service found UUID = %s", args['uuid']) log.debug("Attribute handle start = %x", args['start']) log.debug("Attribute handle end = %x", args['end']) raw_uuid = bytearray(reversed(args['uuid'])) uuid_type = self._get_uuid_type(raw_uuid) if uuid_type != UUIDType.custom: uuid = uuid16_to_uuid( int(bgapi_address_to_hex(args['uuid']).replace(':', ''), 16)) else: uuid = UUID(bytes=bytes(raw_uuid)) new_srvc = Service(uuid, args['start'], args['end']) self._services[args['connection_handle']][uuid] = new_srvc
from __future__ import division import logging import ServiceAWS as aws from pygatt.util import uuid16_to_uuid from pygatt.exceptions import NotConnectedError from Utils import connect_ble from time import sleep logging.basicConfig() logging.getLogger('pygatt').setLevel(logging.DEBUG) uuid_heart_service = uuid16_to_uuid(0x2A37) uuid_battery_service = uuid16_to_uuid(0x2A19) uuid_set_sensor_location = uuid16_to_uuid(0x2A39) uuid_get_sensor_location = uuid16_to_uuid(0x2A38) device = None cf_control_point_turn_off = 0 cf_control_point_turn_on = 1 cf_control_point_short_coffee = 2 cf_control_point_long_coffee = 3 cf_control_point_level_water = 4 cf_control_point_level_coffee = 5 cf_control_point_glass_position = 6 cf_control_point_update = 7 ON = '1' OFF = '0' inUse = 2