Esempio n. 1
0
    def connect(self):
        if self.bt_interface:
            self.req = GATTRequester(self.device, False, self.bt_interface)
        else:
            self.req = GATTRequester(self.device, False)

        self.req.connect(True, 'random')
        connect_start_time = time.time()
        while not self.req.is_connected():
            if time.time() - connect_start_time >= self.timeout_secs:
                raise RuntimeError('Connection to {} timed out after {} seconds'
                                   .format(self.device, self.timeout_secs))
Esempio n. 2
0
    def _tryQueryMbService(self, uuid):
        conn = GATTRequester(uuid, False, self.devName)
        try:
            conn.connect(False, "random")
            max_time = time.time() + 5
            while not conn.is_connected():
                if time.time() > max_time:
                    return False
                time.sleep(0.5)

            DEV_NAME_SERVICE_UUID = "00002a00-0000-1000-8000-00805f9b34fb"  # 2A00, device name
            try:
                value = "".join(conn.read_by_uuid(DEV_NAME_SERVICE_UUID))
            except RuntimeError as err:
                msg = err.message.lower()
                if "no attribute found":
                    return False
                else:
                    raise
            value = value.lower()
            return ("mibp" in value or "mib-push" in value)
        finally:
            if conn.is_connected():
                conn.disconnect()
                while conn.is_connected():
                    time.sleep(0.5)
Esempio n. 3
0
 def __init__(self, address):
     self._requester = GATTRequester(address, False)
     self.led = Led(self)
     self.speaker = Speaker(self)
     self.microphone = Microphone(self)
     self.state = None
     self.name = None
     self.setup_datetime = None
Esempio n. 4
0
 def __init__(self, addr):
     self.req = ""
     if not addr:
         return
     self.req = GATTRequester(addr, False)
     print("Connecting...: {}".format(addr))
     self.req.connect(True, 'random')
     print("Connected: {}".format(addr))
Esempio n. 5
0
 def __init__(self, addr):
     self.req = ""
     if not addr:
         return
     self.req = GATTRequester(addr, False)
     print("Connecting...: {}".format(addr))
     self.req.connect(wait=True, channel_type="random")
     print("Connected: {}".format(addr))
Esempio n. 6
0
 def _connect(self, addr):
     self._log.info("BLE %s connecting ..." % addr)
     self.requester = GATTRequester(addr, False)
     self.requester.connect(True)
     chars = self.requester.discover_characteristics()
     self.characteristic = {}
     for char in chars:
         self.characteristic[char['uuid']] = char['value_handle']
     self._log.info("BLE %s connected OK" % self._addr)
Esempio n. 7
0
 def __init__(self, address):
     self.address = address
     self.service_uuid = arduino.service_uuid
     self.tx_command = arduino.tx_command
     self.rx_response = arduino.rx_response
     self.tx_handle = arduino.tx_handle
     self.rx_handle = arduino
     self.req = GATTRequester(address,
                              False)  # initialize req but don't connect
     self.response = ble_consts.not_ready
Esempio n. 8
0
def connect(device: str, bt_interface: str, timeout: float):
    if bt_interface:
        req = GATTRequester(device, False, bt_interface)
    else:
        req = GATTRequester(device, False)

    req.connect(False, 'random')
    connect_start_time = time.time()

    while not req.is_connected():
        if time.time() - connect_start_time >= timeout:
            raise ConnectionError(
                'Connection to {} timed out after {} seconds'.format(
                    device, timeout))
        time.sleep(0.1)

    yield req

    if req.is_connected():
        req.disconnect()
Esempio n. 9
0
    def __init__(self, mac="C4:C3:00:01:07:3F"):
        # Check for empty arg and correct MAC address format
        # Default MAC address is given otherwise
        if not re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()):
            print("Using default MAC: C4:C3:00:01:07:3F")
            self.mac = "C4:C3:00:01:07:3F"
        else:
            self.mac = mac

        self.service = DiscoveryService()
        self.devices = self.service.discover(2)
        self.requester = GATTRequester(self.mac, False)
def get_serial(bluetooth_address_s):
    global serial_map
    if bluetooth_address_s in serial_map:
        return serial_map[bluetooth_address_s]
    macaddress = ':'.join([bluetooth_address_s[i: i+2] for i in range(0, len(bluetooth_address_s), 2)])
    requester = GATTRequester(macaddress, False)
    try:
        requester.connect(True, channel_type="random")
        data = requester.read_by_uuid("00002a25-0000-1000-8000-00805f9b34fb")
        serial = data[0]
        serial_map[bluetooth_address_s] = serial
        requester.disconnect()
        return serial
    except:
        import traceback
        traceback.print_exc()
        return None
Esempio n. 11
0
def server(addr, port, ble=False):
    global running
    print(f'Creating {"BLE" if ble else "BL"} connection')
    if not ble:
        sock = Socket(RFCOMM)
        sock.connect((addr, port))
    else:
        sock = GATTRequester(addr)
    while running:
        lock.acquire()
        data = b''.join((key_to_order(order, speed) for order in orders))
        data += b''.join((key_to_stop_order(order) for order in stop_orders))
        stop_orders.clear()
        lock.release()
        if not ble:
            sock.send(data)
        else:
            for b in data:
                sock.write_by_handle(port, bytes([b]))
        sleep(.02)
    sock.close()
Esempio n. 12
0
File: test.py Progetto: GY-Cha/wb
        elif keycode == '57':  #spacebar
            state = STOP
        elif keycode == '103':  #up
            state = F
        elif keycode == '108':  #down
            state = S
        elif keycode == '19':  #R
            state = Mon
        elif keycode == '1':  #esc
            state = Moff
        time.sleep(0.01)
    motor_OFF()
    time.sleep(2)


requester1 = GATTRequester("DD:43:89:16:43:81", False)  # right wheel
requester2 = GATTRequester("F4:82:B3:50:ED:55", False)  # left wheel'
response = GATTResponse()


def data_ON():
    requester1.write_by_handle(0x0d, b'\x54\x54\x57\x55\x0D\x0A')
    requester2.write_by_handle(0x0d, b'\x54\x54\x57\x55\x0D\x0A')


def data_OFF():
    requester1.write_by_handle(0x0d, b'\x54\x54\x46\x66\x0D\x0A')
    requester2.write_by_handle(0x0d, b'\x54\x54\x46\x66\x0D\x0A')


def motor_OFF():
Esempio n. 13
0
    def __init__(self, address):
        self.requester1 = GATTRequester("40:06:A0:97:74:A9", False)
        self.requester2 = GATTRequester("40:06:A0:94:FE:F7", False)

        self.connect()
        self.send_data()
Esempio n. 14
0
#!/usr/bin/env python

from bluetooth.ble import GATTRequester

address = "F8:EF:FE:C6:35:AD"
nrf = GATTRequester("F8:EF:FE:C6:35:AD", False)
#nrf.write_by_handle(0x10, str(bytearray([14, 4, 56])))
nrf.connect(wait=True, channel_type="public")
print("OK")

# -*- mode: python; coding: utf-8 -*-

# Copyright (C) 2014, Oscar Acena <*****@*****.**>
# This software is under the terms of Apache License v2 or later.

#from __future__ import print_function
#
#import sys
#from bluetooth.ble import GATTRequester

#
#class JustConnect(object):
#    def __init__(self, address):
#        self.requester = GATTRequester(address, False)
#        self.connect()
#
#    def connect(self):
##        print("Connecting...", end=' ')
##        sys.stdout.flush()
#
#        self.requester.connect(True)
Esempio n. 15
0
 def __init__(self, address):
     self.requester = GATTRequester(address, False)
     self.connect()
     self.request_data()
Esempio n. 16
0
from __future__ import print_function
from bluetooth.ble import DiscoveryService, GATTRequester
import sys
import time

service = DiscoveryService()
devices = service.discover(2)

for address, name in devices.items():
    print("name: {}, address: {}".format(name, address))
    r = GATTRequester(address)
    print("Connecting...", end=' ')
    sys.stdout.flush()
    print(dir(r), r.is_connected())
    time.sleep(2)
    r.connect(True)
Esempio n. 17
0
#    SM,2,0000
#    @TMR2
#    $VAR1=@,2
#    SHW,0072,$VAR1
#    SM,2,0010
#
# The characteristic handle (72 in the example above) must match the handle created for the service.
#

import time
from bluetooth.ble import GATTRequester

#
# the MAC address of the BLE device.  Replace 'D8:80:39:FC:7B:F5' with the address of your device.
#
grq = GATTRequester('D8:80:39:FC:7B:F5', False)

grq.connect()
print("Waiting to connect...")
while not grq.is_connected():
    time.sleep(1)
print("Connected.")

characteristics = grq.discover_characteristics()

#
# the UUID of the service on the BLE device.
#
sample_uuid = '59c889e0-5364-11e7-b114-b2f933d5fe66'

# find the handle for the characteristic.
Esempio n. 18
0
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
	t = threading.Thread(name=address,target=self.periodical_request)
        t.daemon = True
        t.start()