-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrf_ble_read_write.py
65 lines (52 loc) · 2.05 KB
/
nrf_ble_read_write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Library for BLE Radio Advertising
#######################################
import _bleio
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
# Libray For grenerating a new Service
########################################
from adafruit_ble.services import Service
from adafruit_ble.uuid import StandardUUID
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.int import Uint8Characteristic
class myDataService(Service):
uuid = StandardUUID(0xA000) # (0xA000 - my/Custom Service ID)
myOutData = Uint8Characteristic(
max_value=255,
properties=Characteristic.READ | Characteristic.NOTIFY ,
uuid=StandardUUID(0xA001), #0ls -alcat xA002 -my/ID Write-to-Leds ID
)
myInData = Uint8Characteristic(
max_value=255,
properties=Characteristic.WRITE ,
uuid=StandardUUID(0xA002), #0ls -alcat xA002 -my/ID Write-to-Leds ID
)
#CircuitPython Bug (removes old 0xA000 - service)
_bleio.adapter.enabled=0
_bleio.adapter.enabled=1
# Create Service width Characteristics
dataService=myDataService()
#Start BLE-Radio
ble = BLERadio()
ble.name = "RunesBLE"
#Fortell verden hvem du er og hva du kan gjøre
advertisement = ProvideServicesAdvertisement(dataService)
ble.start_advertising(advertisement)
print("Venter på tilkobling/bonding ")
while not ble.connected:
pass
print("Bluetooth enhet er tilkoblet/bondet ")
#Ikke vis verden lengere hvem du er :)
ble.stop_advertising()
oldVal=0
firstTime=True
while ble.connected:
if(firstTime):
firstTime=False
dataService.myOutData=0
newValue=dataService.myInData
if(oldVal != newValue) :
print("Data skrevet til Characteristic 0xA002 (myInData) " + str(newValue))
oldVal=newValue # Lagrer ny-verdi
dataService.myOutData=0x10+oldVal # Lagrer ny-verdi + 0x10 i Characteristic 0xA001
print("Bluetooth enhet er frakoblet")