Esempio n. 1
0
def cb_uuid_to_str(_uuid: CBUUID) -> str:
    """Converts a CoreBluetooth UUID to a Python string.

    If ``_uuid`` is a 16-bit UUID, it is assumed to be a Bluetooth GATT UUID
    (``0000xxxx-0000-1000-8000-00805f9b34fb``).

    Args
        _uuid: The UUID.

    Returns:
        The UUID as a lower case Python string (``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx``)
    """
    _uuid = _uuid.UUIDString()
    if len(_uuid) == 4:
        return "0000{0}-0000-1000-8000-00805f9b34fb".format(_uuid.lower())
    # TODO: Evaluate if this is a necessary method...
    # elif _is_uuid_16bit_compatible(_uuid):
    #    return _uuid[4:8].lower()
    else:
        return _uuid.lower()
Esempio n. 2
0
 def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
         self, manager, peripheral, data, rssi):
     if CBAdvertisementDataServiceDataKey not in data:
         return
     service_data = hexlify(data[CBAdvertisementDataServiceDataKey][
         CBUUID.UUIDWithString_("FD6F")].bytes().tobytes()).decode("ascii")
     encounter = Encounter(
         device_key=str(peripheral.identifier()),
         service_data=service_data,
         time=datetime.now(),
         rssi=rssi.intValue(),
     )
     for listener in self.listeners:
         listener.new_encounter(encounter=encounter)
Esempio n. 3
0
    async def add_new_service(self, uuid: str):
        """
        Add a service and all it's characteristics to be advertised

        Parameters
        ----------
        uuid : str
            The string representation of the UUID of the service to be added
        """
        logger.debug("Creating a new service with uuid: {}".format(uuid))

        service_uuid: CBUUID = CBUUID.alloc().initWithString_(uuid)
        cb_service: CBMutableService = (
            CBMutableService.alloc().initWithType_primary_(service_uuid, True))

        bleak_service: BleakGATTServiceCoreBluetooth = (
            BleakGATTServiceCoreBluetooth(obj=cb_service))

        self.services[uuid] = bleak_service
Esempio n. 4
0
    async def add_new_characteristic(self, service_uuid: str, char_uuid: str,
                                     properties: GattCharacteristicsFlags,
                                     value: Optional[bytearray],
                                     permissions: int):
        """
        Generate a new characteristic to be associated with the server

        Parameters
        ----------
        service_uuid: str
            The string representation of the UUID for the service associated
            with the characteristic to be added
        char_uuid : str
            The string representation of the UUID for the characteristic to be
            added
        properties : GattCharacteristicsFlags
            The flags for the characteristic
        value : Optional[bytearray]
            The initial value for the characteristic
        permissions : int
            The permissions for the characteristic
        """
        logger.debug(
            "Craeting a new characteristic with uuid: {}".format(char_uuid))
        cb_uuid: CBUUID = CBUUID.alloc().initWithString_(char_uuid)
        cb_characteristic: CBMutableCharacteristic = (
            CBMutableCharacteristic.alloc().
            initWithType_properties_value_permissions_(cb_uuid, properties,
                                                       value, permissions))
        bleak_characteristic: BlessGATTCharacteristicCoreBluetooth = (
            BlessGATTCharacteristicCoreBluetooth(obj=cb_characteristic))

        service: BleakGATTService = self.services[service_uuid]
        service.add_characteristic(bleak_characteristic)
        characteristics: List[CBMutableCharacteristic] = [
            characteristic.obj for characteristic in service.characteristics
        ]
        service.obj.setCharacteristics_(characteristics)
Esempio n. 5
0
def _convert_int_to_uuid(i: int) -> str:
    UUID_bytes = i.to_bytes(length=16, byteorder="big")
    UUID_data = NSData.alloc().initWithBytes_length_(UUID_bytes,
                                                     len(UUID_bytes))
    UUID_cb = CBUUID.alloc().initWithData_(UUID_data)
    return UUID_cb.UUIDString()
Esempio n. 6
0
def _convert_uuid_to_int(_uuid: str) -> int:
    UUID_cb = CBUUID.alloc().initWithString_(_uuid)
    UUID_data = UUID_cb.data()
    UUID_bytes = UUID_data.getBytes_length_(None, len(UUID_data))
    UUID_int = int.from_bytes(UUID_bytes, byteorder="big")
    return UUID_int
def string2uuid(uuid_str: str) -> CBUUID:
    """Convert a string to a uuid"""
    return CBUUID.UUIDWithString_(uuid_str)
Esempio n. 8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Originally from https://github.com/masato-ka/python-corebluetooth-sample.git

import struct

from Foundation import CBCentralManager, CBUUID
from PyObjCTools import AppHelper

from btleclassifier import BTLEAdvClassifier

from constants import C
import btleclassifier
import datetime

wx2_service = CBUUID.UUIDWithString_(u'0C4C3000-7700-46F4-AA96-D5E974E32A54')
wx2_characteristic_data = CBUUID.UUIDWithString_(
    u'0C4C3001-7700-46F4-AA96-D5E974E32A54')

EXIT_COUNT = 10


class MyBLE(object):
    def __init__(self, debug=False):
        self.seen = set()
        self.debug = debug
        self.count_advertisements = 0

    def centralManagerDidUpdateState_(self, manager):
        if self.debug:
            print("centralManagerDidUpdateState_")
Esempio n. 9
0
 def centralManagerDidUpdateState_(self, manager):
     self.manager = manager
     manager.scanForPeripheralsWithServices_options_(
         [CBUUID.UUIDWithString_(self.UUID)],
         {CBCentralManagerScanOptionAllowDuplicatesKey: objc.YES},
     )