File: bluetooth.py

package info (click to toggle)
gtk-meshtastic-client 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,040 kB
  • sloc: python: 4,759; xml: 58; makefile: 3
file content (52 lines) | stat: -rw-r--r-- 1,585 bytes parent folder | download | duplicates (2)
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
"""Simple program to demo how to use meshtastic library.
   To run: python examples/info.py
"""

import meshtastic
import meshtastic.serial_interface
import json
import base64
import os
from gi.repository import GLib

import meshtastic.ble_interface

#meshtastic_config_dir = GLib.get_user_config_dir() + '/gtk_meshtastic_client'
meshtastic_config_dir = GLib.get_tmp_dir()
bluetooth_scanned_device_path = meshtastic_config_dir + '/bluetooth.json'
print("Save file: " + str(bluetooth_scanned_device_path))

if os.path.exists(bluetooth_scanned_device_path):
    with open(bluetooth_scanned_device_path, 'r+') as json_file:
        content = json_file.read().strip()
        if content:
            try:
                data = json.loads(content)
            except:
                os.remove(bluetooth_scanned_device_path)
            else:
                for i in data:
                    print("name: " + str(i["name"]))
                    print("adddress: " + str(i["address"]))

                exit()

print("Scanning takes 10 seconds")
device_list =  meshtastic.ble_interface.BLEInterface.scan()

device_json_string = '[\n'

for x in device_list:
    device_json_string = (device_json_string + " {\"name\": \""+ str(x.name) + "\", \"address\": \"" + str(x.address) + "\"},")

device_json_string = device_json_string[:-1]

device_json_string = (device_json_string + "\n]")

if not os.path.exists(meshtastic_config_dir):
    os.makedirs(meshtastic_config_dir)

with open(bluetooth_scanned_device_path, 'w') as json_file:
    json_file.write(device_json_string)

json_file.close()