本文整理汇总了Python中bluepy.btle.Peripheral.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Peripheral.connect方法的具体用法?Python Peripheral.connect怎么用?Python Peripheral.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bluepy.btle.Peripheral
的用法示例。
在下文中一共展示了Peripheral.connect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RileyLink
# 需要导入模块: from bluepy.btle import Peripheral [as 别名]
# 或者: from bluepy.btle.Peripheral import connect [as 别名]
class RileyLink(PacketRadio):
def __init__(self):
self.peripheral = None
self.pa_level_index = PA_LEVELS.index(0x84)
self.data_handle = None
self.logger = getLogger()
self.address = None
if os.path.exists(RILEYLINK_MAC_FILE):
with open(RILEYLINK_MAC_FILE, "r") as stream:
self.address = stream.read()
self.service = None
self.response_handle = None
self.notify_event = Event()
self.initialized = False
def connect(self, force_initialize=False):
try:
if self.address is None:
self.address = self._findRileyLink()
if self.peripheral is None:
self.peripheral = Peripheral()
try:
state = self.peripheral.getState()
if state == "conn":
return
except BTLEException:
pass
self._connect_retry(3)
self.service = self.peripheral.getServiceByUUID(RILEYLINK_SERVICE_UUID)
data_char = self.service.getCharacteristics(RILEYLINK_DATA_CHAR_UUID)[0]
self.data_handle = data_char.getHandle()
char_response = self.service.getCharacteristics(RILEYLINK_RESPONSE_CHAR_UUID)[0]
self.response_handle = char_response.getHandle()
response_notify_handle = self.response_handle + 1
notify_setup = b"\x01\x00"
self.peripheral.writeCharacteristic(response_notify_handle, notify_setup)
while self.peripheral.waitForNotifications(0.05):
self.peripheral.readCharacteristic(self.data_handle)
if self.initialized:
self.init_radio(force_initialize)
else:
self.init_radio(True)
except BTLEException:
if self.peripheral is not None:
self.disconnect()
raise
def disconnect(self, ignore_errors=True):
try:
if self.peripheral is None:
self.logger.info("Already disconnected")
return
self.logger.info("Disconnecting..")
if self.response_handle is not None:
response_notify_handle = self.response_handle + 1
notify_setup = b"\x00\x00"
self.peripheral.writeCharacteristic(response_notify_handle, notify_setup)
except BTLEException:
if not ignore_errors:
raise
finally:
try:
if self.peripheral is not None:
self.peripheral.disconnect()
self.peripheral = None
except BTLEException:
if ignore_errors:
self.logger.exception("Ignoring btle exception during disconnect")
else:
raise
def get_info(self):
try:
self.connect()
bs = self.peripheral.getServiceByUUID(XGATT_BATTERYSERVICE_UUID)
bc = bs.getCharacteristics(XGATT_BATTERY_CHAR_UUID)[0]
bch = bc.getHandle()
battery_value = int(self.peripheral.readCharacteristic(bch)[0])
self.logger.debug("Battery level read: %d", battery_value)
version, v_major, v_minor = self._read_version()
return { "battery_level": battery_value, "mac_address": self.address,
"version_string": version, "version_major": v_major, "version_minor": v_minor }
except BTLEException as btlee:
raise PacketRadioError("Error communicating with RileyLink") from btlee
finally:
self.disconnect()
def _read_version(self):
version = None
try:
if os.path.exists(RILEYLINK_VERSION_FILE):
with open(RILEYLINK_VERSION_FILE, "r") as stream:
#.........这里部分代码省略.........
示例2: SBrickCommunications
# 需要导入模块: from bluepy.btle import Peripheral [as 别名]
# 或者: from bluepy.btle.Peripheral import connect [as 别名]
class SBrickCommunications(threading.Thread, IdleObject):
def __init__(self, sbrick_addr):
threading.Thread.__init__(self)
IdleObject.__init__(self)
self.lock = threading.RLock()
self.drivingLock = threading.RLock()
self.eventSend = threading.Event()
self.sBrickAddr = sbrick_addr
self.owner_password = None
self.brickChannels = [
SBrickChannelDrive(0, self.eventSend),
SBrickChannelDrive(1, self.eventSend),
SBrickChannelDrive(2, self.eventSend),
SBrickChannelDrive(3, self.eventSend),
]
self.SBrickPeripheral = None
self.stopFlag = False
self.characteristicRemote = None
self.need_authentication = False
self.authenticated = False
self.channel_config_ids = dict()
def set_channel_config_id(self, channel, config_id):
self.channel_config_ids[config_id] = channel
self.brickChannels[channel].set_config_id(config_id)
def terminate(self):
self.stopFlag = True
def is_driving(self):
locked = self.drivingLock.acquire(False)
if locked:
self.drivingLock.release()
return not locked
def connect_to_sbrick(self, owner_password):
self.owner_password = owner_password
self.start()
def run(self):
try:
monotime = 0.0
self.SBrickPeripheral = Peripheral()
self.SBrickPeripheral.connect(self.sBrickAddr)
service = self.SBrickPeripheral.getServiceByUUID('4dc591b0-857c-41de-b5f1-15abda665b0c')
characteristics = service.getCharacteristics('02b8cbcc-0e25-4bda-8790-a15f53e6010f')
for characteristic in characteristics:
if characteristic.uuid == '02b8cbcc-0e25-4bda-8790-a15f53e6010f':
self.characteristicRemote = characteristic
if self.characteristicRemote is None:
return
self.emit('sbrick_connected')
self.need_authentication = self.get_need_authentication()
self.authenticated = not self.need_authentication
if self.need_authentication:
if self.password_owner is not None:
self.authenticate_owner(self.password_owner)
while not self.stopFlag:
if self.authenticated:
if monotonic.monotonic() - monotime >= 0.05:
self.send_command()
monotime = monotonic.monotonic()
self.eventSend.wait(0.01)
for channel in self.brickChannels:
if channel.decrement_run_timer():
monotime = 0.0
self.drivingLock.release()
# print("stop run normal")
self.emit("sbrick_channel_stop", channel.channel)
if channel.decrement_brake_timer():
self.drivingLock.release()
# print("stop brake timer")
monotime = 0.0
self.emit("sbrick_channel_stop", channel.channel)
if self.authenticated:
self.stop_all()
self.send_command()
self.SBrickPeripheral.disconnect()
self.emit('sbrick_disconnected_ok')
except BTLEException as ex:
self.emit("sbrick_disconnected_error", ex.message)
def get_channel(self, channel):
if isinstance(channel, six.integer_types):
return self.brickChannels[channel]
if isinstance(channel, six.string_types):
return self.brickChannels[self.channel_config_ids[channel]]
return None
def drive(self, channel, pwm, reverse, time, brake_after_time=False):
with self.lock:
#.........这里部分代码省略.........