Created
December 23, 2019 05:16
-
-
Save HiCraigChen/d3cdd3a1f6631e069cd1105a746fcbe0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bluetooth | |
import subprocess | |
class BluetoothComm: | |
def __init__(self): | |
self.server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) | |
port = 1 | |
self.server_socket.bind(("",port)) | |
self.server_socket.listen(1) | |
self.client_socket,address = self.server_socket.accept() | |
print("Accepted connection from ",address) | |
def read_comm(self): | |
res = self.client_socket.recv(1024) | |
if len(res): | |
return res | |
else: | |
return None | |
def send_comm(self, text): | |
self.client_socket.send(text) | |
def main(): | |
blue_comm = BluetoothComm() | |
buff_msg = b'' | |
while True: | |
get = blue_comm.read_comm() | |
blue_comm.send_comm(get) | |
if get != b'\r': | |
buff_msg += get | |
else: | |
print(buff_msg) | |
p = subprocess.Popen([b'bash',b'-c',buff_msg], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) | |
shell_out = p.communicate() | |
print('shell:',shell_out) | |
blue_comm.send_comm('\n') | |
if shell_out[0] != b'': | |
blue_comm.send_comm(shell_out[0]) | |
else: | |
blue_comm.send_comm(shell_out[1]) | |
blue_comm.send_comm('\r') | |
buff_msg = b'' | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment