-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbluet.py
52 lines (35 loc) · 1.3 KB
/
bluet.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
from kivy.utils import platform
if 'android' == platform:
from jnius import autoclass, cast
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
UUID = autoclass('java.util.UUID')
android = True
bt_adapter = BluetoothAdapter.getDefaultAdapter()
if not bt_adapter.isEnabled():
intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.startActivityForResult(intent, 1)
else:
android = False
import bluetooth
def discover():
if android:
return [x.getName() for x in bt_adapter.getBondedDevices().toArray()]
else:
return bluetooth.discover_devices()
def get_name(mac):
return bluetooth.lookup_name(mac)
def connect(bd_addr, port):
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
return sock
def discover_thread(queue):
discovered = discover()
dict = {}
for x in discovered:
dict[x] = get_name(x)
queue.put(dict)