-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBleData3.py
137 lines (94 loc) · 3.13 KB
/
getBleData3.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
#--*--coding=utf-8--*--
from __future__ import print_function
import sys
import binascii
from bluepy import btle
import os
import struct
import cv2
ble_conn = None
class MyDelegate(btle.DefaultDelegate):
def __init__(self, conn):
btle.DefaultDelegate.__init__(self)
self.conn = conn
def handleNotification(self, cHandle, data):
data = binascii.b2a_hex(data)
print("Notification:", str(cHandle), " data ", data)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
pass
elif isNewData:
print("\\nDiscovery:", "MAC:", dev.addr, " Rssi ", str(dev.rssi))
def ble_connect(devAddr):
global ble_conn
if not devAddr is None and ble_conn is None:
# ble_conn = btle.Peripheral(devAddr, btle.ADDR_TYPE_PUBLIC)
ble_conn = btle.Peripheral(devAddr, btle.ADDR_TYPE_RANDOM)
ble_conn.setDelegate(MyDelegate(ble_conn))
print("connected")
def ble_disconnect():
global ble_conn
ble_conn = None
print("disconnected")
if __name__ == '__main__':
ble_mac = "cc:50:98:e9:2a:b9"
# scan
scanner = btle.Scanner().withDelegate(MyDelegate(None))
timeout = 10.0
devices = scanner.scan(timeout)
for dev in devices:
if dev.addr == ble_mac:
print("\\nDiscovery:", "MAC:", dev.addr, " Rssi ", str(dev.rssi))
for (adtype, desc, value) in dev.getScanData():
print (" %s(0x%x) = %s" % (desc, int(adtype), value))
break
# connect
ble_connect(ble_mac)
# write , set listen
ch = ""
for item in ble_conn.getServices():
# print("services:",item.uuid)
if item.uuid == "be940000-7333-be46-b7ae-689e71722bd5":
# print(item.uuid)
for iitem in item.getCharacteristics("be940001-7333-be46-b7ae-689e71722bd5"):
ch = iitem
print(ch)
break
# ch.write(val = b"0x05060700010007",withResponse=True)
# ch.peripheral.waitForNotifications(10.0)
i=0
ble_conn.writeCharacteristic(ch.getHandle(), b"0x05060700010007",withResponse=True)
while(True):
if(ble_conn.waitForNotifications(2.0)):
i=i+1
if(i>1000):
break
continue
print("Waiting...")
time.sleep(0.5)
# ble_conn.waitForNotifications(2.0)
# wait notification
# ble_conn.waitForNotifications(10.0)
# disconnect
ble_disconnect()
# p = btle.Peripheral("cc:50:98:e9:2a:b9", "random")
# p.setDelegate( MyDelegate(None) )
# services=p.getServices()
# for service in services:
# print(service)
# s = p.getServiceByUUID("be940000-7333-be46-b7ae-689e71722bd5")
# c = s.getCharacteristics()[0]
# print(c)
# c.write(b"0x05060700010007", "utf-8")
# print(c)
# i=0
# while(True):
# if(p.waitForNotifications(1)):
# i=i+1
# if(i>1000):
# break
# continue
# print("Waiting...")
# time.sleep(0.5)
# p.disconnect()