In App Inventor Make TCP IP Client - android

Hi Guy's I'm new to programming Android device's I do have python, java, C#, C, C++, PHP, Bash and Visual Basic Experience but I'm new to this block programming, and I haven't done much work with forms. I'm trying to get make an application that posts data to an external IP. I have successsfully wrote a server and a windows based client, clicking buttons in my windows client posts data to the ip 192.168.1.9 port 9999. This is just in the testing phase to remote control a bunch of beaglebone gpio's. So far I've had great success with the windows side. In app inventor for android, However, I've created a series of buttons and tabs, different buttons post text or post and poll for response. The problem I have is that I can directly attach the web connector to 192.168.1.9 but when I add in the port 9999 it tells me the address is incorrect. The method I'm using is
when Screen1.initialize
do set Web1.Url to "http://192.168.1.9:9999"
when Button1.Click
do call Web1.PostText
text > 0
Again, if I type in just the IP of the beaglebone I see its ethernet port go crazy when I click button1. It does nothing when I add in the port. Of course my server is running on 9999 since port 80 is reserved for the internet. Any suggestions?

I would like to suggest you a two-step solution.
Step 1:
Problem>>Develop an android app which is capable to communicate via TCP-IP.
Solution>> I hope you are familiar with MIT-APP Inventor-2. Import an extension called ClientSocket extension V0.4.3 available here to app. Thanks to the developer of the extension.
Step 2:
Problem>>A server responding client request.
Solution>> I have written a Python code.
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 9000 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print (host)
s.listen(5)
while True:
c, addr = s.accept() # Establish connection with client.
data=(str(c.recv(1024)))
print data
conn.commit()
c.close()
cur.close()
Hope this will help.

Related

send a photo from android device to server (treat it on matlab)

I am in trouble and need your help. I am currently working on a project which requires me to first pick a photo from gallery using an android device (with android studio), extract text and then recognize the text.
I have already achieved the process of extraction and recognizing through Matlab. Now my problem is, how can i transfer a photo which I picked from my android cell phone to MATLAB? How to send the results back to the phone after processing the image?
Please help. A code will be appreciated.
I think the best way to do so will be using a TCP/IP socket. I did a similar project once where I had to receive text from my RPi in MATLAB. Simply connect your Android device and the workstation with MATLAB to the same network and note the IP address of the server(the device on which you will initialize your Server. Preferably make your Android device as the server). Connect the client to the server and read/write the data. Here is an example of what I did. I initialized MATLAB as the client and my other device as Server.
t = tcpip('xxx.xxx.xxx.xx', yyyyy, 'NetworkRole', 'Client'); #Replace xxx.xxx.xxx.xx with IP address and yyyyy with the Port
#Change Client to Server if you want MATLAB to work as server
fopen(t)
dataMat = zeros(1, N);
while 1
data = fread(t); #Change fread to fwrite to transmit data
raw = char(data); ###############################
val = str2num(raw); #### This part converts the ###
val = val'; #### received ASCII values ####
l = length(val); #### to the original data #####
message = 0; ###############################
for i = 1:l
message = val(i)*(10^(l-i)) + message;
end
dataMat(count) = message;
message
end
I'm not sure how to send an image over TCP/IP, but I'm pretty sure it's possible. Click here for TCP/IP with MATLAB blog.
I haven't used Android studio so can't help with TCP/IP there, but if your app is fairly simple, you can create a TCP/IP application with MIT App Inventor(which I have worked on), so can help with that.
Cheers!

Getting Raspberry pi events from android/iOS app

I just got started with Raspberry pi and I wanted to make a program on my Raspberry which gets input from an app on my Android/iOS device over bluetooth. I wanted to first check if something like this is possible and second if you have any clues on how to do something like this.
Thanks
PS: Since I just got started I'm only looking for clues and I don't want anyone to write and app for me so don't down vote
You would likely need to establish a network communication between the Raspberry Pi and the device.
For the server:
import socket
HOST = '' # This should receive from all available interfaces.
PORT = 1111 # Random port number.
data = "Test" # Data to send to the client.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOST, PORT))
while True:
s.sendto(data, (HOST, PORT))
print data
On the client, very similar code but add:
data, addr = s.recvfrom(1024)
print addr
print "Message received: ", data
Under the while True
Reference the following for setting up RPI wireless hotspot:
http://elinux.org/RPI-Wireless-Hotspot
The HOST for each is going to be the device IPv4 address, usually in format:
192.168.x.x.
I have not personally found a way without using serials for data communication. The most common way to communicate information between devices is over network. Look into peer-to-peer network solutions.
The code may not work as is, you will need to make client/server-side code specific for your needs.
Reference the following for setting up sockets and a low-level network interface: https://docs.python.org/2/howto/sockets.html
Hopefully this helps.
EDIT:
There is a Bluetooth method for RPI.
Here's a good branch in Github that contains example of the Bluetooth library used in Python:
https://github.com/karulis/pybluez/blob/master/examples/simple
Good references:
Bluetooth programming with Python.
http://people.csail.mit.edu/albert/bluez-intro/c212.html
How to create a Bluetooth tag with RPI.
https://www.raspberrypi.org/magpi/create-a-raspberry-pi-3-bluetooth-tag/

Python Bluetooth how to send a file to a phone

in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.

Send AT-command through bluetooth with python

I try to send AT commands from my computer (ubuntu 13.04) to my phone (Android 5.1) via bluetooth. I want to read the SMS.
I retrieve the MAC address of my phone with :
hcitool scan
I browse all available services on the device with :
sdptool browse XX:XX:XX:XX:XX:XX
I get the good RFCOMM channel for SMS/MMS service and now I'm trying to send the AT command.
I tried with pySerial with a bound and connected rfcomm to my phone but no response :
import serial
phone = serial.Serial('/dev/rfcomm0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
I tried the same code on a USB serial port and I have a response :
import serial
phone = serial.Serial('/dev/ttyACM0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
# *EMRDY: 1
# AT
# OK
I tried with pyBluez but same problem, no response of my AT command :
import bluetooth
client_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
client_sock.connect(('XX:XX:XX:XX:XX:XX', 4))
client_sock.send(b'AT\r')
data = client_sock.recv(1024)
print "received [%s]" % data
And I finally tried with native python sockets, but no response :
import socket
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect(('XX:XX:XX:XX:XX:XX',4))
s.send(b'AT\r')
data = s.recv(1024)
s.close()
print('Received', repr(data))
Note: The phone displays a prompt window to accept that my computer accesses my sms. Of course I accepted.
Can anyone tell me what is wrong and what I can try?
Well, for starter it is better to check first that you have a two-way communication between your Host computer and your Phone on bluetooth, like you said, it did work with USB, then there should be no reason it does not with bluetooth unless you didn't yet established a good communication, so I think it is better to try first that you have good communication by just sending and replying with the same string (kinda hand-shaking protocol) and make sure that you know what your python code is actually sending, may be unseen extra characters using bluetooth that you don't pay attention to, which makes your AT command unrecognizable by your phone.

Suddenly unable to create socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print "entered loop"
c, addr = s.accept() # Establish connection with client.
I was using this piece of code to simply create a socket and accept a connection on it from an Android. Simple enough.
But since the past few hours (with no changes to the code), python is for some reason unable to create the socket. I don't get any errors, but it's forever stuck at the s.accept() line.
I tried using Fing, a network diagnostics app on my Android to see the open ports, and indeed, Python is unable to create/open the socket. Earlier, as soon as I ran the code and rescanned on Fing, I was able to see the port open and test a TCP connection on it.
Sometimes, randomly in between, it works for one or two attempts.
What could be going wrong? I've tried to change port. I don't know what to debug since there isn't any error!
Same Error occur to me . it was due to not closing the Socket . Try Closing the Sockets.
Else it will work for once when ever you will run and then after that it will start causing problems.

Categories

Resources