[45bc06]: tools / EventClients / Clients / J2ME Client / j2me_remote.py Maximize Restore History

Repo status: analyzing...

Download this file

j2me_remote.py    176 lines (157 with data), 5.8 kB

  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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
#
# XBoxMediaCenter
# J2ME Remote
# Copyright (c) 2008 topfs2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
try:
from xbmc.xbmcclient import *
from xbmc.bt.bt import *
from xbmc.defs import *
except:
sys.path.append('../../lib/python')
from xbmcclient import *
from bt.bt import *
ICON_PATH = "../../icons/"
from socket import *
import os
from threading import Thread
host = "localhost"
port = 9777
addr = (host, port)
sock = socket(AF_INET,SOCK_DGRAM)
def send_key(key):
packet = PacketBUTTON(map_name="J2ME", button_name=key, queue=1)
packet.send(sock, addr)
def send_message(caption, msg):
packet = PacketNOTIFICATION(caption,
msg,
ICON_PNG,
icon_file=ICON_PATH + "/phone.png")
packet.send(sock, addr)
def send_ImageToBluetooth(Image):
imageData = file( Image ).read()
print "Data len ", len( imageData )
client_sock.send( format.uint32( len( imageData) ) )
client_sock.sendall(imageData)
print "Sent file"
class Ping(Thread):
def __init__ (self):
Thread.__init__(self)
def run(self):
import time
while 1: # ping the server every 19s
packet = PacketPING()
packet.send(sock, addr)
time.sleep (19)
def main():
import time
# connect to localhost, port 9777 using a UDP socket
# this only needs to be done once.
# by default this is where XBMC will be listening for incoming
# connections.
server_sock=bt_create_rfcomm_socket()
server_sock.listen(2)
portBT = server_sock.getsockname()[1]
uuid = "ACDC"
bt_advertise(socket=server_sock, name="XBMC Remote", uuid=uuid)
print "Waiting for connection on RFCOMM channel %d" % portBT
packet = PacketHELO(devicename="J2ME Remote",
icon_type=ICON_PNG,
icon_file=ICON_PATH + "/phone.png")
packet.send(sock, addr)
Ping().start()
while(True):
try:
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
client_addr, client_port = client_info
runFlag = True
firstRun = True
print "Accepted connection from ", client_addr
except KeyboardInterrupt:
packet = PacketLOG(LOGNOTICE, "J2ME: User interrupted")
packet.send(sock, addr)
bt_stop_advertising(socket = server_sock)
server_sock.close()
sys.exit(0)
try:
while runFlag:
if firstRun:
client_name = client_sock.recv(1024)
firstRun = False
send_message("J2ME: Phone Connected", client_name)
packet = PacketLOG(LOGNOTICE, "Phone Connected")
packet.send(sock, addr)
data = client_sock.recv(3)
if(data[0] == "0"):
if(data[1] == "0"):
runFlag = False
packet = PacketLOG(LOGNOTICE, "J2ME: Recieved disconnect command")
packet.send(sock, addr)
client_sock.close()
if(data[1] == "1"):
print "Shuting down server"
client_sock.close()
bt_stop_advertising(socket = server_sock)
server_sock.close()
packet = PacketBYE()
packet.send(sock, addr)
sys.exit(0)
elif(data[0] == "2"):
print "<button id=\"%i\">" % ord( data[1] )
send_key(str( ord( data[1] ) ))
else:
print "Unkown received data [%s]" % data
except IOError:
print "Lost connection too %s" % client_name
runFlag = False
send_message("Phone Disconnected", client_name)
pass
except KeyboardInterrupt:
packet = PacketLOG(LOGNOTICE, "J2ME: User interrupted")
packet.send(sock, addr)
client_sock.close()
bt_stop_advertising(socket = server_sock)
server_sock.close()
packet = PacketBYE() # PacketPING if you want to ping
packet.send(sock, addr)
sys.exit(0)
def usage():
print """
Java Bluetooth Phone Remote Control Client for XBMC v0.1
Usage for xbmcphone.py
--address ADDRESS (default: localhost)
--port PORT (default: 9777)
--help (Brings up this message)
"""
if __name__=="__main__":
try:
i = 0
while (i < len(sys.argv)):
if (sys.argv[i] == "--address"):
host = sys.argv[i + 1]
elif (sys.argv[i] == "--port"):
port = sys.argv[i + 1]
elif (sys.argv[i] == "--help"):
usage()
sys.exit(0)
i = i + 1
main()
except:
sys.exit(0)