def _VoidPtrToCBUUID(ptr, len):
    try:
        ptr = _VoidPtrToByteArray(ptr, len)
        ptr = binascii.hexlify(ptr)
        ptr = ptr[:8] + '-' + ptr[8:12] + '-' + ptr[12:16] + '-' + ptr[16:20] + '-' + ptr[20:]
        ptr = CBUUID.UUIDWithString_(ptr)
    except:
        print "ERROR: failed to convert void * to CBUUID"
        ptr = None

    return ptr
 def WriteBleCharacteristic(self, connObj, svcId, charId, buffer, length):
     self.logger.debug("write start")
     result = False
     if self.target and self.target.Connected:
         converted_data = str(_VoidPtrToByteArray(buffer, length))
         self.charId_tx = bytearray(
             uuid.UUID(str(_VoidPtrToUUIDString(charId, 16))).bytes)
         self.svcId_tx = bytearray(
             uuid.UUID(str(_VoidPtrToUUIDString(svcId, 16))).bytes)
         self.tx.WriteValue(
             dbus.Array([dbus.Byte(ord(i)) for i in converted_data], 'y'),
             options="",
             reply_handler=self.WriteCharactertisticSuccessCB,
             error_handler=self.WriteCharactertisticErrorCB,
             timeout=bleWriteCharacteristicTimeoutSec)
         result = True
     else:
         self.logger.warning("WARNING: peripheral is no longer connected.")
     return result
    def WriteBleCharacteristic(self, connObj, svcId, charId, buffer, length):
        """ Called by WeaveDeviceMgr.py to satisfy a request by Weave to transmit a packet over BLE."""
        result = False

        bytes = _VoidPtrToByteArray(buffer, length)
        bytes = NSData.dataWithBytes_length_(bytes, len(bytes)) # convert bytearray to NSData

        svcId = _VoidPtrToCBUUID(svcId, 16)
        charId = _VoidPtrToCBUUID(charId, 16)

        if self.peripheral and self.peripheral.state() != BlePeripheralState_Disconnected:
            for char in self.characteristics[svcId]:
                if char.UUID() == charId:
                    self.peripheral.writeValue_forCharacteristic_type_(bytes, char, CBCharacteristicWriteWithResponse)
                    result = True
                    break
        else:
            self.logger.warning("WARNING: peripheral is no longer connected.")

        return result