Example #1
0
    def connect_to(self, chr: _Characteristic):
        _Characteristic.__init__(self, chr.peripheral, chr.uuid, chr.handle,
                                 chr.properties, chr.valHandle)

        if self.description_handle_offset is None:
            self._description_read = self.uuid.getCommonName
        else:
            self._description_desc = Descriptor(
                self.peripheral, self.uuid,
                self.valHandle + self.description_handle_offset)
            self._description_read = self._description_desc.read

        self._description_cache = self.description_read()
Example #2
0
    def test_notification_for_unexpected_handle(self):
        """Test Bluetooth notification for unexpected handle"""
        self.h = HW01('XX:XX:XX:XX:XX:XX')
        mock_char = Characteristic(self.h, UUIDS.CHARACTERISTIC_TX, 0x14,
                                   [0b00010000], 0x14)

        tx_delegate = TXDelegate(mock_char.getHandle(), self.h._log)
        self.h.setDelegate(tx_delegate)

        self.assertEqual(tx_delegate.data, None)

        # Trigger notification with expected handle
        self.h.delegate.handleNotification(0xff, 'AT+??')
        self.assertEqual(tx_delegate.data, None)
Example #3
0
    def test_service_setup(self, mock_getsrv, mock_getchar, mock_getdesc):
        """Test service setup"""
        self.h = HW01('XX:XX:XX:XX:XX:XX')

        services = []
        for uuid in [UUIDS.SERVICE_GENERIC, UUIDS.SERVICE_HW01_B]:
            services.append(Service(self.h, uuid, 0x0, 0xffff))
        mock_getsrv.side_effect = services

        characteristics = []
        for uuid in [
                UUIDS.CHARACTERISTIC_DEVICE_NAME, UUIDS.CHARACTERISTIC_TX,
                UUIDS.CHARACTERISTIC_RX
        ]:
            characteristics.append([
                Characteristic(self.h, uuid, 0x47, [0b00000010, 0b00001000],
                               0x47)
            ])
        mock_getchar.side_effect = characteristics

        mock_desc = [Descriptor(self.h, UUIDS.NOTIFICATION_DESCRIPTOR, 0x12)]
        mock_getdesc.return_value = mock_desc

        with mock.patch('bluepy.btle.Descriptor.read',
                        return_value=(b'\x01\x00')):
            with mock.patch('bluepy.btle.Descriptor.write'):
                with self.assertLogs('HW01') as log:
                    self.h.setup_services()

        self.assertEqual(log.output, ['INFO:HW01:Setup RX/TX communication'])
Example #4
0
    def test_device_name(self, mock_device_name):
        """Test device name"""
        self.h = HW01('XX:XX:XX:XX:XX:XX')

        mock_device_char = Characteristic(
            self.h, '00000000-0000-1000-8000-00805f9b34fb', 0xff,
            [0b00000010, 0b00001000], 0xff)
        self.h.device_char = mock_device_char

        self.assertEqual(self.h.get_device_name(), 'HW01')
Example #5
0
 def init_svc(self, name, svc_uuid, char_uuid):
     if (not hasattr(self, 'char_'+name)):
         svc_data = services_data[svc_uuid]
         char = svc_data["chars"][char_uuid]
         svc = Service(self, svc_uuid, svc_data["hndStart"], svc_data["hndEnd"])
         setattr(self, 'char_'+name, Characteristic(self, char_uuid, char["handle"], char["properties"], char["valHandle"]))
         if len(char["descs"].keys()) > 0:
             setattr(self, 'cccd_'+name, Descriptor(self, char["descs"].keys()[0], char["descs"].values()[0]["handle"]))
             print("Enabling %s notifications..." % name)
             getattr(self, 'cccd_'+name).write(b"\x01\x00", True)
             self.enabled_notifs.append(name)
Example #6
0
 def get_descriptor(self, characteristic: Characteristic, uuid: int
                    or str) -> Descriptor:
     return characteristic.getDescriptors(forUUID=uuid)[0]
Example #7
0
 def _removeCallback(self, characteristic: Characteristic):
     handle = characteristic.getHandle()
     del self._callbackMap[handle]
     self._disableNotification(characteristic)
Example #8
0
 def _registerCallback(self, characteristic: Characteristic, callback: Callable):
     handle = characteristic.getHandle()
     self._callbackMap[handle] = callback
     self._enableNotification(characteristic)
Example #9
0
 def _writeCharDescriptor(self, characteristic: Characteristic, data):
     notify_handle = characteristic.getHandle() + 1
     self.writeCharacteristic(notify_handle, data, withResponse=True)
Example #10
0
 def write(self, value, with_response=False):
     data = self.pack_data(value)
     return _Characteristic.write(self, data, with_response)
Example #11
0
 def read(self):
     data = _Characteristic.read(self)
     return self.unpack_data(data)