def get_all_handles(): peripheral = btle.Peripheral(MAC) service = peripheral.getServiceByUUID(BEEP_SERVICE_UUID) char_dict = get_characteristics_dict(service) handles = sorted(char_dict) peripheral.disconnect() return handles
def __init__(self, mac): try: self.p = btle.Peripheral(mac) self.p.setDelegate(heartDelegate()) except: self.p = 0 print("Not connected")
def connect_write_disconnect(handle, msg_bytes): peripheral = btle.Peripheral(MAC) service = peripheral.getServiceByUUID(BEEP_SERVICE_UUID) char_dict = get_characteristics_dict(service) print '{} <- {}'.format(handle, to_hex_str(msg_bytes)) char_dict[handle].write(msg_bytes) peripheral.disconnect()
def handleNotification(self, cHandle, data): # ... perhaps check cHandle # ... process 'data' # Initialisation ------- p = btle.Peripheral(address) p.setDelegate(MyDelegate(params))
def fuzzle(): peripheral = btle.Peripheral(MAC) service = peripheral.getServiceByUUID(BEEP_SERVICE_UUID) char_dict = get_characteristics_dict(service) for i in range(100): print i handle = random.choice(char_dict) char_dict[handle].write(random_string(8)) peripheral.disconnect()
def setup(addr): p = None while p is None: try: p = btle.Peripheral(addr, btle.ADDR_TYPE_RANDOM) except btle.BTLEException as e: print e time.sleep(1) d = MyDelegate() p.setDelegate(d) p.wr = p.writeCharacteristic return p
def find_max_bytes_for_characteristic(): peripheral = btle.Peripheral(MAC) service = peripheral.getServiceByUUID(BEEP_SERVICE_UUID) char_dict = get_characteristics_dict(service) handles = sorted(char_dict.keys()) peripheral.disconnect() for h in handles: print 'handle: ', h for n in range(1, 1023): try: connect_write_disconnect(h, random_string(n)) except: print 'broke: ', n break
def ble_connect(devAddr): global ble_conn global sub_thread global ble_op global state if not devAddr is None and ble_conn is None: ble_conn = btle.Peripheral(devAddr, btle.ADDR_TYPE_PUBLIC) ble_conn.setDelegate(MyDelegate(ble_conn)) sub_thread = async_thread(ble_conn) sub_thread.start() state = "[c]" rprint("connected")
def __init__(self, deviceAddr=None): if deviceAddr is not None: self.__conn = btle.Peripheral(deviceAddr) self.ntfy = self.notificationHandler() self.__conn.setDelegate(self.ntfy) #Turn On Notifications for IMU????----------------------------------------------- self.__conn.writeCharacteristic(0x001d, struct.pack('<bb', 0x01, 0x00)) #Event self.__conn.writeCharacteristic(0x0020, struct.pack('<bb', 0x02, 0x00)) #Classifier self.__conn.writeCharacteristic(0x0024, struct.pack('<bb', 0x02, 0x00)) #--------------------------------------------------------------------------------- #Ask for data (No EMG but acc, gyr...) self.__conn.writeCharacteristic(0x0019, struct.pack('<bbbbb', 1, 3, 0, 3, 1)) time.sleep(1) self.stayAwake()
def __init__(self): self.periph = btle.Peripheral("F5:F2:09:FC:41:FC","random") # Promoty3 # self.periph = btle.Peripheral("E6:2E:9B:39:53:2E","random") # Promoty4 # self.periph = btle.Peripheral("F0:12:84:D8:04:53","random") # Heblina print("Setup delegate") self.periph.setDelegate( NeblinaDelegate('Neblina') ) services = self.periph.getServices() for x in xrange(1,len(services)): print(str(services[x])) try: print("Trying to get Descriptors") desc = self.periph.getDescriptors(1,7) for descriptor in desc: print(descriptor) except btle.BTLEException as e: print("Exception =>",e)
'--new', action='store_true', help='Display only new adv responses, by default show new + updated') parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity') arg = parser.parse_args(sys.argv[1:]) btle.Debugging = arg.verbose scanner = btle.Scanner().withDelegate(ScanPrint()) print(ANSI_RED + "Scanning for devices..." + ANSI_OFF) devices = scanner.scan(arg.timeout) if arg.discover: print(ANSI_RED + "Discovering services..." + ANSI_OFF) for d in devices: if not d.connectable: continue print(" Connecting to", ANSI_WHITE + d.addr + ANSI_OFF + ":") dev = btle.Peripheral(d) dump_services(dev) dev.disconnect() print
def scanThread(): print("hello BLE scanner") scanner = btle.Scanner() global count global iface while True: print("still scanning... count:", len(conn_list)) with lock: devcies = scanner.scan(timeout=5) reminder_list = [] for dev in devcies: for (sdid, desc, val) in dev.getScanData(): # print("Devcies %s ,%s , UUID = %s " %(dev.addr ,desc , val)) if val == CharteristicUUID: # print("devcies %s ,descriptor: %s , UUID = %s " %(dev.addr ,desc , val)) reminder_list.append(dev) for dev in reminder_list: with lock: try: conn = btle.Peripheral(dev.addr, dev.addrType, iface=iface) conn.setDelegate(btle.DefaultDelegate()) count += 1 if iface == numOfDongle: iface = 1 else: iface += 1 # print("test connection successfully") except: print("connection failed") try: # 0000fed0-0000-1000-8000-00805f9b34fb service = conn.getServiceByUUID( "0000fff6-0000-1000-8000-00805f9b34fb") char = service.getCharacteristics( "0000fffb-0000-1000-8000-00805f9b34fb")[0] conn.writeCharacteristic(char.handle + 2, struct.pack('<bb', 0x01, 0x00), True) conn_list.append(conn) #check if the node exist on DB , if not creat on DB except: try: conn_list.remove(dev) except: lulala = 0 print( "Set notification failed , Remove the device from conn_list" ) try: objectId = checkDataExist("macAddress", dev.addr) if objectId == False: uploadData(dev.addr) except: print("CheckDataExist failed")
def listen(): peripheral = btle.Peripheral(MAC) peripheral.disconnect()
def __init__(self, macAddress): os.system("sudo hciconfig hci0 up") self.macAddress = macAddress self.connection = btle.Peripheral(self.macAddress, "random") btle.DefaultDelegate.__init__(self) self.delegate = self.connection.setDelegate(self) self.toe = 0 self.ball = 0 self.arch = 0 self.heel = 0 self.accelerationX = 0.0 self.accelerationY = 0.0 self.accelerationZ = 0.0 self.rotationX = 0.0 self.rotationY = 0.0 self.rotationZ = 0.0 self.orientationX = 0.0 self.orientationY = 0.0 self.orientationZ = 0.0 self.temperatureC = 0.0 self.battery = "" self.FORCE = 0 self.ACCELERATION = 1 self.ROTATION = 2 self.ORIENTATION = 3 self.TEMPERATURE = 4 self.GENERIC_COMMUNICATION = 5 self.forceCCCHandle = 0x0010 self.accelerationCCCHandle = 0x0014 self.rotationCCCHandle = 0x0018 self.orientationCCCHandle = 0x001c self.temperatureCCCHandle = 0x0020 self.batteryCCCHandle = 0x0026 self.bodySensorLocationCCCHandle = 0x0034 self.genericCommunicationCCCHandle = 0x002e self.forceValueHandle = 0x000e self.accelerationValueHandle = 0x0012 self.rotationValueHandle = 0x0016 self.orientationValueHandle = 0x001a self.temperatureValueHandle = 0x001e self.batteryValueHandle = 0x0025 self.bodySensorLocationValueHandle = 0x0022 self.genericCommunicationValueHandle = 0x002c self.SYNC_STEP_1_RESPONSE_CODE = 2 self.SYNC_STEP_2_RESPONSE_CODE = 4 self.SYNC_STEP_3_RESPONSE_CODE = 6 self.SYNC_DONE_RESPONSE_CODE = 7 self.SET_TIME_HEADER_CODE = 0x00 self.SYNC_STEP_1_HEADER_CODE = chr(1) self.SYNC_STEP_2_HEADER_CODE = chr(3) self.SYNC_STEP_3_HEADER_CODE = chr(5) self.lastReadingPopped = False self.synchronizing = False print btle.Peripheral.status(self.connection) self.GENERIC_COMMUNICATION_SLEEP_PERIOD = 100 self.SENSOR_SERVICE_SLEEP_PERIOD = 50 #self.logger = BoogioLogger(macAddress) #self.logger.connect() self.readingsSynchronized = 0
def collect_EKG(device="Bluetooth Connected Device", duration): if (device == "Bluetooth Connected Device"): ecg_uuid = btle.UUID(0x2A37) p = btle.Peripheral("C4:45:2F:37:D4:4C", "random") svc = p.getServiceByUUID(0x180D) ch = svc.getCharacteristics(ecg_uuid)[0] for i in range(0,200): values.append(1000) t0 = time.time() t1 = time.time() values = [] while ((t1-t0) <= duration): print("readline()") valueRead = ch.read() try: valueInInt = valueRead[3:] pm_value = htosi(valueInInt) fmtValue = (valueInInt/1000)+5000 if fmtValue <= 11000: if fmtValue >= -7000: values.append(fmtValue) t1=time.time() else: print("Invalid! Out of range value.") else: print("Invalid! Value too large.") except ValueError: print("Invalid! Cannot cast.") sps = int((len(values)-200)/duration) rawvalues = values[200:] # plt.plot(rawvalues, alpha=0.5, color='blue', label="raw signal") # plt.show() trace_data = pd.DataFrame(data=rawvalues, columns=['rawdata']) print(sps) print(trace_data) else: print('Device not supported.') return trace_data, sps # target_name = "TruCOR Bluefruit ECG Monitor" # target_address = None # nearby_devices = bluetooth.discover_devices() # for bdaddr in nearby_devices: # if target_name == bluetooth.lookup_name( bdaddr ): # target_address = bdaddr # break # if target_address is not None: # print("Found target bluetooth device with address ", target_address # else: # print ("Could not find target bluetooth device nearby")