Bonjour(IPP) vs Jetdirect-Socket Printer - android

I am trying to use my android phone as a printer. I am using ServerSocket to receive the document to be printed. If I add my phone as IP printer by providing IP address and port and select Generic Postscript Printer, I am able to receive the file in ps format correctly. I don't want to add my phone as printer as IP printer. So Now I am using NsdManager to register my device as printer. It gets recognized as Bonjour printer automatically and I can successfully add as printer. But now every time I print a document from by computer I get this data in the socket's input stream.
POST / HTTP/1.1 Content-Length: 673 Content-Type: application/ipp Host:
Android-2.local:9200 User-Agent: CUPS/2.1.0 (Darwin 15.2.0; x86_64)
IPP/2.0 Expect: 100-continue Gattributes-charsetutf-8Hattributes-
natural-languageen-usEprinter-uriipp://Android-
2.local.:9200/Drequested-attributescompression-supportedDcopies-
supportedDcups-versionDdocument-format-supportedD marker-colorsDmarker-
high-levelsD marker-levelsDmarker-low-levelsDmarker-messageDmarker-
namesDmarker-typesDmedia-col-supportedD$multiple-document-handling-
supportedDoperations-supportedDprint-color-mode-supportedD printer-
alertDprinter-alert-descriptionDprinter-is-accepting-jobsD printer-
mandatory-job-attributesD printer-stateDprinter-state-messageDprinter-
state-reasons
I read the IPP documentation and I am sending 100 Continue in the response and all the required params like this
clientSocket.setTcpNoDelay(true);
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
out.write("HTTP/1.1 100\r\n".getBytes("UTF-8"));
out.write("\r\n".getBytes("UTF-8"));
out.write("compression-supported: \"none\"\r\n".getBytes("UTF-8"));
out.write("printer-is-accepting-jobs: \"true\"\r\n".getBytes("UTF-8"));
.....
....
out.flush();
After that if i try to read the input stream for document, it gives null and on my computer i receive message "Printing: Connected to Printer" but if do
out.close();
in to close the outputstream for the socket I get the message "unable to get printer status" on my computer.
Please help me. Is there any way I just receive the document and not this post request or way to send a correct response and get the document? I am stuck with this for quite a long time now. Any help is highly appreciated.

Your computer/CUPS (Mac with El Capitain I guess) is trying to print via IPP, but your phone-print-device does not implement IPP. Obviously, that's not going to work.
Solution 1:
add the printer in CUPS with the correct setup.
for network printers CUPS offers:
IPP - http
IPP - https
IPP - ipp
IPP - ipps
LPD/LPR-Host
Windows printer via spoolss
AppSocket/HP JetDirect
the selection depends on the procotol you have implemented or plan to support in your app. IPP is not an option for you, except...
Solution 2:
Implement IPP in your "Print-Server-App". That's going to be tough!
There's a lot to implement... see https://www.pwg.org/ipp/
Solution 3:
Properly announce your service via Bonjour Printing 1.2
_pdl-datastream._tcp should be the correct service type.
(see also chapter 7.6, Flagship Naming)

I was finally able to implement it. Here is an excellent implementation for network printer using Node.js. It explains the details of IPP https://github.com/watson/ipp-printer
Also this video is good demonstration
https://www.youtube.com/watch?v=58Ti8w1yX2w
And I am using https://github.com/NanoHttpd/nanohttpd to handle the printing requests on my android phone.

Related

Bluetooth SPP profile on SIM800H

I'm having issues when trying to use bluetooth on my project. On previous version I used the HC-05 module for bluetooth and it gives me a simple serial connection right after any device connects. Now the project evolved and we opted on using SIM800H because it gives us GSM+Bluetooth.
When I connect any android device it gives me some profiles but never SPP. I can pair normally but when using any SPP server app (https://stackoverflow.com/a/4037619/2637661) I can never send or get data from my device. If I start the connection from the Android app, it says that it's connecting while the SIM800 gives me the URC and I respond:
+BTCONNECTING: "34:c7:31:aa:37:5b","SPP"
AT+BTACPT=1
OK
+BTCONNECT: 1,"Android",34:c7:31:aa:37:5b,"SPP"
But it stays on server mode and I can't use the commands AT+BTSPPSEND or AT+BTSPPGET, as the documentation says.
On the other hand, if I start the connection from my device just like the docs say:
AT+BTCONNECT=1,4 // Device is 1 and SPP profile is 4
OK
AT+BTSPPSEND
>I type anything here + ctrl+z
SEND FAIL
and get nothing on the Android side.
Plus in both cases the connection drops after something like 30s and I can't reestablish it unless I turn the SIM800H off and on again.
Got no success using the APP mode either (sending the string "SIMCOMSPPFORAPP" right after connection succeeds for transparent communication).
The SIM800H firmware version is
AT+CGMR
Revision:1309B07SIM800H32_BT
and I tried using the following apps
Bluetooth Terminal
Bluetooth spp pro
BlueSPP
The GSM side works flawlessly and I can send/receive TCP messages everytime I try.
Does anyone have any kind of experience using this module? Thanks for reading!
UPDATE:
I'm using a simple sketch in order to talk with the module's serial, don't know if it's relevant but here it goes.
#define SIM800_POWER 23
void setup() {
Serial.begin(9600);
Serial1.begin(19200);
Serial.print("Setting all up");
pinMode(SIM800_POWER, OUTPUT);
Serial.print(".");
delay(500);
digitalWrite(SIM800_POWER, HIGH);
Serial.print(".");
Serial.println("OK");
}
void loop() {
if(Serial1.available()){
Serial.write(Serial1.read());
}
if(Serial.available()){
Serial1.write(Serial.read());
}
}
And what I get after trying Andrii's answer:
Setting all up..OK
AT
OK
AT
OK
AT
OK
AT+BTPOWER=0
OK
AT+BTPOWER=1
OK
AT+BTCONNECT=1,4
OK
+BTCONNECT: 1,"Will",d4:87:d8:77:37:0b,"SPP"
AT+BTSTATUS?
+BTSTATUS: 5
P: 1,"Will",d4:87:d8:77:37:0b
C: 1,"Will",d4:87:d8:77:37:0b,"SPP"
OK
AT+BTSPPSEND
> SIMCOMSPPFORAPP
SEND FAIL
Seems AT+BTSPPSEND without any parameters is only for AT-command send from client (your SIM800H) to server (other SIM800, not your Android device unless your Android device implements AT-command support). For data sending you should use AT+BTSPPSEND and after receiving > symbol send SIMCOMSPPFORAPP keyword and then, after receiving SEND OK response send command AT+BTSPPSEND=<LENGTH_OF_YOUR_DATA> and then, after receiving > symbol, send your data until Ctrl+Z code e.g.:
AT+BTSPPSEND
> SIMCOMSPPFORAPP
SEND OK
AT+BTSPPSEND=5
> HELLO
SEND OK
^Z
where HELLO - is your data, and 5 in AT+BTSPPSEND=5 is length of HELLO string. Details in SIM800H_BT_Application_Note.
UPDATE
Selected by bold small, but important part of answer (thanks to hlovdal)

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/

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.

In App Inventor Make TCP IP Client

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.

How do I resolve a Bonjour domain name on Android?

I need to get my app to play a video file located on my network. I know the url of the file is:
http://something.local/abc.mp4
Now, when I manually substitute "something.local" with its true ip address, the MediaPlayer has no problem playing it. Nonetheless, when I have the above address, the MediaPlayer errors out with error (1, -1007).
So I'm assuming this is because Android doesn't understand "something.local" as being correct.
My question is: How can I "translate" something.local into an ip myself, so that I can then pass it into MediaPlayer?
A small caveat: I believe that MediaPlayer does not work with IPv6 addresses, so please keep that in mind...
Just a side note, in case it makes my situation clearer: When I run ping something.local -4 in the Windows command prompt, it returns:
Pinging something.local [192.168.1.126] with 32 bytes of data:
Reply from 192.168.1.126: bytes=32 time=145ms TTL=64
Reply from 192.168.1.126: bytes=32 time=112ms TTL=64
Reply from 192.168.1.126: bytes=32 time=32ms TTL=64
Reply from 192.168.1.126: bytes=32 time=169ms TTL=64
That translation where windows went from something.local -> 192.168.1.126 is what I want to do in my Android app.
Firstly, you need read document about Bonjour (iOS term) or Zero Config (Linux term).
To understand what's something.local:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html#//apple_ref/doc/uid/TP40002458-SW1
For example, if a user types steve.local. into a Web browser, this
tells the system to multicast the request for steve on the local
network instead of sending it to the conventional DNS server. If a
Bonjour-enabled computer named steve is on the local network, the
user’s browser is sent the correct IP address for it. This allows
users to access local hosts and services without a conventional DNS
server.
For how to resolve it:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/NetServicesArchitecture.html#//apple_ref/doc/uid/20001074-SW1
For java library, previous answers provided good enough example.
You should try this snippet with jmDNS library api.. may need some changes.
JmDNS jmdns = JmDNS.create();
DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
if (addressEntry instanceof DNSRecord) {
ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
if (cachedAddressInfo != null) {
for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
// use the `address`
}
}
You have access to java,net APIS on android and can use them to resolve adresses.
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html
However, success will depend on network proper configuration. Your device receives DNS server setup via DHCP - so you are at mercy of network provider

Categories

Resources