-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathandroidhelpers.py
68 lines (48 loc) · 1.91 KB
/
androidhelpers.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
from jnius import PythonJavaClass, java_method, autoclass
# SERVICE = Autoclass('org.renpy.PythonService').mService
SERVICE = autoclass('org.renpy.android.PythonActivity').mActivity
Context = autoclass('android.content.Context')
Intent = autoclass('android.content.Intent')
BluetoothManager = SERVICE.getSystemService(Context.BLUETOOTH_SERVICE)
BTAdapter = autoclass('android.bluetooth.BluetoothAdapter')
ADAPTER = BluetoothManager.getAdapter()
REQUEST_ENABLE_BT = 0x100
INTENT_LOCK = False
from android import activity
def activity_result(request_code, result_code, data):
print("get result: %s, %s, %s" % (
request_code == REQUEST_ENABLE_BT, result_code, data))
global INTENT_LOCK
INTENT_LOCK = False
def start_scanning(callback):
global INTENT_LOCK
if not ADAPTER.isEnabled() and not INTENT_LOCK:
print "binding activity result"
activity.bind(on_activity_result=activity_result)
INTENT_LOCK = True
print "starting activity for result"
SERVICE.startActivityForResult(
Intent(BTAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT)
else:
ADAPTER.startLeScan(callback)
def stop_scanning(callback):
ADAPTER.stopLeScan(callback)
def restart_scanning(callback):
stop_scanning(callback)
start_scanning(callback)
class AndroidScanner(PythonJavaClass):
__javainterfaces__ = ['android.bluetooth.BluetoothAdapter$LeScanCallback']
callback = None
@java_method("(Landroid/bluetooth/BluetoothDevice;I[B)V")
def onLeScan(self, device, irssi, scan_record):
if self.callback:
self.callback(device.getName(),
device.getAddress(),
irssi,
scan_record)
else:
print "no callback set"
print "onLeScan"
print device.getName()
print irssi
print len(scan_record), scan_record