Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ubluetooth
- import ubinascii
- import uos
- import time
- from ucryptolib import aes
- from micropython import const
- _IRQ_SCAN_RESULT = const(5)
- _IRQ_SCAN_DONE = const(6)
- _IRQ_PERIPHERAL_CONNECT = const(7)
- _IRQ_PERIPHERAL_DISCONNECT = const(8)
- _IRQ_GATTC_SERVICE_RESULT = const(9)
- _IRQ_GATTC_SERVICE_DONE = const(10)
- _IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
- _IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
- _IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
- _IRQ_GATTC_DESCRIPTOR_DONE = const(14)
- _IRQ_GATTC_READ_RESULT = const(15)
- _IRQ_GATTC_READ_DONE = const(16)
- _IRQ_GATTC_WRITE_DONE = const(17)
- _IRQ_GATTC_NOTIFY = const(18)
- _IRQ_GATTC_INDICATE = const(19)
- _IRQ_GATTS_INDICATE_DONE = const(20)
- mac = None
- connected = False
- def on_scan_result(data):
- global mac
- addr_type, addr, adv_type, rssi, adv_data = data
- mac = addr
- print('type:{} addr:{} rssi:{} data:{}'.format(
- addr_type, ubinascii.hexlify(addr), rssi, ubinascii.hexlify(adv_data)))
- def on_connection():
- global connected
- print("CONNECTED!")
- ble.gattc_discover_services(0)
- ble.gattc_discover_characteristics(0, 9, 65535)
- ble.gattc_discover_descriptors(0, 9, 65535)
- connected = True
- def blelock_get_token():
- key = ubinascii.unhexlify('3A60432A5C01211F291E0F4E0C132825') #Known key supposted to work.
- req_token_data = ubinascii.unhexlify('060101012D1A683D48271A18316E471A') #Instruction supposted to get token.
- cipher = aes(key, 1) # MODE_ECB = 1
- global mac
- print('Connecting to address:', mac)
- ble.gap_connect(1, mac)
- while not(connected):
- time.sleep(1)
- print('…')
- # print(req_token_data)
- # encrypted = cipher.encrypt(req_token_data)
- # print(encrypted)
- # ble.gattc_write(0, 11, b'\x01\x00') #subscribe to notifications?
- # ble.gattc_write(0, 14, encrypted) #send token request? Listen for response, use it to unlock.
- print('All done for now.')
- def bt_irq(event, data):
- if event == _IRQ_SCAN_RESULT:
- on_scan_result(data)
- elif event == _IRQ_SCAN_DONE:
- print("Scan complete.")
- elif event == _IRQ_PERIPHERAL_CONNECT:
- on_connection()
- elif event == _IRQ_PERIPHERAL_DISCONNECT:
- print("DISCONNECTED!")
- elif event == _IRQ_GATTC_SERVICE_RESULT:
- conn_handle, start_handle, end_handle, uuid = data
- print('S: ', data)
- elif event == _IRQ_GATTC_SERVICE_DONE:
- conn_handle, status = data
- elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
- conn_handle, status = data
- elif event == _IRQ_GATTC_DESCRIPTOR_DONE:
- conn_handle, status = data
- elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
- conn_handle, def_handle, value_handle, properties, uuid = data
- print('CH: ', data)
- elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
- conn_handle, dsc_handle, uuid = data
- print('Ds: ', data)
- elif event == _IRQ_GATTC_READ_RESULT:
- conn_handle, value_handle, char_data = data
- print('Dat:', data)
- elif event == _IRQ_GATTC_READ_DONE:
- conn_handle, value_handle, status = data
- elif event == _IRQ_GATTC_WRITE_DONE:
- conn_handle, value_handle, status = data
- else:
- print("Unhandled event!")
- print(event)
- ble = ubluetooth.BLE()
- ble.irq(handler=bt_irq)
- ble.active(True)
- ble.gap_scan(10000, 30000, 30000)
- time.sleep(10)
- blelock_get_token()
- # blelock_unlock() #TODO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement