Arduino ESP8266 HTTPS server How to install SSL certificate - android
I made HTTP server on my NodeMCU ESP8266 board using ESP8266WebServer library. I would like to convert it to the HTTPS server, so I used ESP8266WebServerSecure library, bought SSL certificate and got 3 files:
certificate.cert
dv_ca.pem
private.pem
Could someone explain me how should I install it on my server? Below my code when I was trying to launch https server with self-generated certificate, but there was a problem to connect it using android application(Trust anchor not found for Android SSL connection).
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
#define output4 4
#define output5 5
#define input0 A0
const char* ssid = "SSID";
const char* password = "password";
//HTTPS server object, port number 443
BearSSL::ESP8266WebServerSecure server(443);
static const char serverCert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)EOF";
static const char serverKey[] PROGMEM = R"EOF(
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
)EOF";
void handleNotFound() {
String message = "No such a data or incorrect request...";
server.send(404, "text/plain", message);
}
void changeStatus(){
//Parsing request argument to string
String postBody = server.arg("plain");
//Changing output value condition
if(server.hasArg("number")&&server.hasArg("status")){
int pinNumber = server.arg("number").toInt();
if(server.arg("status")=="on"){
digitalWrite(pinNumber, HIGH);
} else if(server.arg("status")=="off"){
digitalWrite(pinNumber, LOW);
}
} else {
handleNotFound();
}
Serial.println("Argument: "+postBody);
server.send(200, "text/plain", postBody);
}
void getStatus(){
//Creating json document
DynamicJsonDocument doc(200);
//Pushing data to json document
if(digitalRead(output4)==LOW){
JsonObject out4 = doc.createNestedObject("sensor 4");
out4["name"] = "output4";
out4["value"] = 0;
} else {
JsonObject out4 = doc.createNestedObject("sensor 4");
out4["name"] = "output4";
out4["value"] = 1;
}
if(digitalRead(output5)==LOW){
JsonObject out5 = doc.createNestedObject("sensor 5");
out5["name"] = "output5";
out5["value"] = 0;
} else {
JsonObject out5 = doc.createNestedObject("sensor 5");
out5["name"] = "output5";
out5["value"] = 1;
}
//Getting analog input value
JsonObject in0 = doc.createNestedObject("sensor 0");
in0["name"] = "input0";
in0["value"] = analogRead(A0);
//Variable co
String outputData = "";
serializeJsonPretty(doc, outputData);
server.send(200, "application/json", outputData);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
// Set outputs to LOW
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);
//Get current time
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
//Start wifi connection
WiFi.begin(ssid, password);
delay(100);
Serial.println("Trying to connect wifi network");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP adress: ");
Serial.println(WiFi.localIP());
//Handling requests
server.on("/", HTTP_GET, getStatus);
server.on("/led", HTTP_GET, changeStatus);
//Setting server certificate and key
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
// Start server
server.begin();
Serial.println("Secured server ready");
}
void loop() {
// put your main code here, to run repeatedly:
// Handling incoming client requests in loop
server.handleClient();
}
The code is from examples of ESP8266. Change the certificate except testing purposes. You can get a certificate from internet or generate it with make-self-signed-cert.sh which is located in ESP8266WiFi/examples/WiFiHTTPSServer. Before execution of make-self-signed-cert.sh, modify the script.Find your-name-here and replace it with your name or organization name. Then execute the script. At the end of the execution, there will be new files that named as key.h and x509.h. Certificate codes are in the two new files. Copy&paste them into the arduino code.
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
// The certificate is stored in PMEM
static const uint8_t x509[] PROGMEM = {
0x30, 0x82, 0x01, 0x3d, 0x30, 0x81, 0xe8, 0x02, 0x09, 0x00, 0xfe, 0x56,
0x46, 0xf2, 0x78, 0xc6, 0x51, 0x17, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x26, 0x31,
0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x45, 0x53,
0x50, 0x38, 0x32, 0x36, 0x36, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
0x04, 0x03, 0x0c, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x31, 0x38, 0x31,
0x34, 0x34, 0x39, 0x31, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x30, 0x31, 0x31,
0x32, 0x35, 0x31, 0x34, 0x34, 0x39, 0x31, 0x38, 0x5a, 0x30, 0x26, 0x31,
0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x45, 0x53,
0x50, 0x38, 0x32, 0x36, 0x36, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55,
0x04, 0x03, 0x0c, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
0x31, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02,
0x41, 0x00, 0xc6, 0x72, 0x6c, 0x12, 0xe1, 0x20, 0x4d, 0x10, 0x0c, 0xf7,
0x3a, 0x2a, 0x5a, 0x49, 0xe2, 0x2d, 0xc9, 0x7a, 0x63, 0x1d, 0xef, 0xc6,
0xbb, 0xa3, 0xd6, 0x6f, 0x59, 0xcb, 0xd5, 0xf6, 0xbe, 0x34, 0x83, 0x33,
0x50, 0x80, 0xec, 0x49, 0x63, 0xbf, 0xee, 0x59, 0x94, 0x67, 0x8b, 0x8d,
0x81, 0x85, 0x23, 0x24, 0x06, 0x52, 0x76, 0x55, 0x9d, 0x18, 0x09, 0xb3,
0x3c, 0x10, 0x40, 0x05, 0x01, 0xf3, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30,
0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
0x05, 0x00, 0x03, 0x41, 0x00, 0x69, 0xdc, 0x6c, 0x9b, 0xa7, 0x62, 0x57,
0x7e, 0x03, 0x01, 0x45, 0xad, 0x9a, 0x83, 0x90, 0x3a, 0xe7, 0xdf, 0xe8,
0x8f, 0x46, 0x00, 0xd3, 0x5f, 0x2b, 0x0a, 0xde, 0x92, 0x1b, 0xc5, 0x04,
0xc5, 0xc0, 0x76, 0xf4, 0xf6, 0x08, 0x36, 0x97, 0x27, 0x82, 0xf1, 0x60,
0x76, 0xc2, 0xcd, 0x67, 0x6c, 0x4b, 0x6c, 0xca, 0xfd, 0x97, 0xfd, 0x33,
0x9e, 0x12, 0x67, 0x6b, 0x98, 0x7e, 0xd5, 0x80, 0x8f
};
// And so is the key. These could also be in DRAM
static const uint8_t rsakey[] PROGMEM = {
0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xc6, 0x72,
0x6c, 0x12, 0xe1, 0x20, 0x4d, 0x10, 0x0c, 0xf7, 0x3a, 0x2a, 0x5a, 0x49,
0xe2, 0x2d, 0xc9, 0x7a, 0x63, 0x1d, 0xef, 0xc6, 0xbb, 0xa3, 0xd6, 0x6f,
0x59, 0xcb, 0xd5, 0xf6, 0xbe, 0x34, 0x83, 0x33, 0x50, 0x80, 0xec, 0x49,
0x63, 0xbf, 0xee, 0x59, 0x94, 0x67, 0x8b, 0x8d, 0x81, 0x85, 0x23, 0x24,
0x06, 0x52, 0x76, 0x55, 0x9d, 0x18, 0x09, 0xb3, 0x3c, 0x10, 0x40, 0x05,
0x01, 0xf3, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x35, 0x0b, 0x74,
0xd3, 0xff, 0x15, 0x51, 0x44, 0x0f, 0x13, 0x2e, 0x9b, 0x0f, 0x93, 0x5c,
0x3f, 0xfc, 0xf1, 0x17, 0xf9, 0x72, 0x94, 0x5e, 0xa7, 0xc6, 0xb3, 0xf0,
0xfe, 0xc9, 0x6c, 0xb1, 0x1e, 0x83, 0xb3, 0xc6, 0x45, 0x3a, 0x25, 0x60,
0x7c, 0x3d, 0x92, 0x7d, 0x53, 0xec, 0x49, 0x8d, 0xb5, 0x45, 0x10, 0x99,
0x9b, 0xc6, 0x22, 0x3a, 0x68, 0xc7, 0x13, 0x4e, 0xb6, 0x04, 0x61, 0x21,
0x01, 0x02, 0x21, 0x00, 0xea, 0x8c, 0x21, 0xd4, 0x7f, 0x3f, 0xb6, 0x91,
0xfa, 0xf8, 0xb9, 0x2d, 0xcb, 0x36, 0x36, 0x02, 0x5f, 0xf0, 0x0c, 0x6e,
0x87, 0xaa, 0x5c, 0x14, 0xf6, 0x56, 0x8e, 0x12, 0x92, 0x25, 0xde, 0xb3,
0x02, 0x21, 0x00, 0xd8, 0x99, 0x01, 0xf1, 0x04, 0x0b, 0x98, 0xa3, 0x71,
0x56, 0x1d, 0xea, 0x6f, 0x45, 0xd1, 0x36, 0x70, 0x76, 0x8b, 0xab, 0x69,
0x30, 0x58, 0x9c, 0xe0, 0x45, 0x97, 0xe7, 0xb6, 0xb5, 0xef, 0xc1, 0x02,
0x21, 0x00, 0xa2, 0x01, 0x06, 0xc0, 0xf2, 0xdf, 0xbc, 0x28, 0x1a, 0xb4,
0xbf, 0x9b, 0x5c, 0xd8, 0x65, 0xf7, 0xbf, 0xf2, 0x5b, 0x73, 0xe0, 0xeb,
0x0f, 0xcd, 0x3e, 0xd5, 0x4c, 0x2e, 0x91, 0x99, 0xec, 0xb7, 0x02, 0x20,
0x4b, 0x9d, 0x46, 0xd7, 0x3c, 0x01, 0x4c, 0x5d, 0x2a, 0xb0, 0xd4, 0xaa,
0xc6, 0x03, 0xca, 0xa0, 0xc5, 0xac, 0x2c, 0xe0, 0x3f, 0x4d, 0x98, 0x71,
0xd3, 0xbd, 0x97, 0xe5, 0x55, 0x9c, 0xb8, 0x41, 0x02, 0x20, 0x02, 0x42,
0x9f, 0xd1, 0x06, 0x35, 0x3b, 0x42, 0xf5, 0x64, 0xaf, 0x6d, 0xbf, 0xcd,
0x2c, 0x3a, 0xcd, 0x0a, 0x9a, 0x4d, 0x7c, 0xad, 0x29, 0xd6, 0x36, 0x57,
0xd5, 0xdf, 0x34, 0xeb, 0x26, 0x03
};
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServerSecure server(443);
void setup() {
Serial.begin(115200);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Set the certificates from PMEM (if using DRAM remove the _P from the call)
server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClientSecure client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
unsigned long timeout = millis() + 3000;
while (!client.available() && millis() < timeout) {
delay(1);
}
if (millis() > timeout) {
Serial.println("timeout");
client.flush();
client.stop();
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
val = 0;
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
} else {
Serial.println("invalid request");
client.print("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><body>Not found</body></html>");
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val) ? "high" : "low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
Related
Unable to send keyboard events to Bluetooth HID connected device
I am able to connect to the Bluetooth HID device successfully and able to control the paired device home button and navigation buttons. But unable to send Keyboard key events properly to the HID device. When I am typing something in the host device, the HID device responding with random actions. I have the following code for the connection with descriptors array, override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) { if (profile == BluetoothProfile.HID_DEVICE) { mBtHidDevice = proxy as BluetoothHidDevice val sdp = BluetoothHidDeviceAppSdpSettings( "HidControl", "Android HID Joystick", "Android", 0xC0.toByte(), descriptor ) } } The descriptor array is as below, private val descriptor = byteArrayOf( // HID descriptor 0x09, // bLength 0x21, // bDescriptorType - HID 0x11, 0x01, // bcdHID (little endian - 1.11) 0x00, // bCountryCode 0x01, // bNumDescriptors (min 1) 0x22, // bDescriptorType - Report 0x30, 0x00, // wDescriptorLength (48) // Report descriptor 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x06, // USAGE (Key board) 0xa1.toByte(), 0x01, // COLLECTION (Application) 0xa1.toByte(), 0x00, // COLLECTION (Physical) 0x05, 0x09, // USAGE_PAGE (Button) 0x19, 0x01, // USAGE_MINIMUM (Button 1) 0x29, 0x04, // USAGE_MAXIMUM (Button 4) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95.toByte(), 0x04, // REPORT_COUNT (4) 0x81.toByte(), 0x02, // INPUT (Data,Var,Abs) 0x75, 0x04, // REPORT_SIZE (4) 0x95.toByte(), 0x01, // REPORT_COUNT (1) 0x81.toByte(), 0x03, // INPUT (Cnst,Var,Abs) 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x15, 0x81.toByte(), // LOGICAL_MINIMUM (-127) 0x25, 0x7f, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95.toByte(), 0x02, // REPORT_COUNT (2) 0x81.toByte(), 0x02, // INPUT (Data,Var,Abs) 0xc0.toByte(), // END_COLLECTION 0xc0.toByte() // END_COLLECTION ) Code to send keyboard events to the target device is below override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { Log.d("Key Codes ", "$keyCode + event: $event") for (btDev in mBtHidDevice!!.connectedDevices) { mBtHidDevice!!.sendReport( btDev, 0, byteArrayOf(event!!.keyCode.toByte()) ) mBtHidDevice!!.sendReport( btDev, 0, byteArrayOf( 0 ) ) } return super.onKeyUp(keyCode, event) } Please suggest if I am missing anything here. Thanks for the help!
You need to use Keyboard Usage Page. The logical minimum and logical maximum that I see is only from 1 to 4. You wanted those USAGEs alone? Refer to HID usage table https://www.usb.org/document-library/hid-usage-tables-122 There is a Keyboard use case in the appendix. You can refer to that.
NFC Apdu Response 67 00 (wrong length) on Android using isodep
i tried to send extended APDU command into Desfire card on Android. i was select the application on the card and success. but when i tried to authentication process and send this command, i got response 67 00 (wrong length) i send data : 00820000288C9D8E3837E8E47514E9010801C1615E65E677A21DC40E07B32DF6DE24EED3D6CD950BA8CB5E738528 where: CLA : 0x00 INS : 0x82 P1 : 0x00 P2 : 0x00 Lc : 0x28 //(40 bytes) data : 0x8C, 0x9D, 0x8E, 0x38, 0x37, 0xE8, 0xE4, 0x75, 0x14, 0xE9, 0x01, 0x08, 0x01, 0xC1 , 0x61, 0x5E, 0x65, 0xE6, 0x77, 0xA2, 0x1D, 0xC4, 0x0E, 0x07, 0xB3, 0x2D, 0xF6, 0xDE , 0x24, 0xEE, 0xD3, 0xD6, 0xCD, 0x95, 0x0B, 0xA8, 0xCB, 0x5E, 0x73, 0x85 **Le : 0x28 (expected return length)** i think there is nothing wrong with the command structure, but why the response is 67 00 i am using isodep library. please some one help me..
What is / how do I find the universal ibeacon identifier?
I am trying to write an android application to interface with iBeacons, but I need to find the start of the UUID / major / minor characteristics in the byte array. Through look at a couple of resources including this question, it seems like ALL iBeacons transmit some pattern in the byte array that is the same that identifies them as iBeacons. I asked another question recently and got a useful answer, link here but 1) I haven't been able to test it's functionality (waiting on my device) and 2) I want to know how it is working. So my questions: What is that pattern? Can I find it just by searching that array for the pattern? And is the UUID / Major / Minor always a predefined number of spots in the array from that identifying pattern? Thanks!
So after looking around on a few different sites, I have found that 3 people list their patterns as 02 01 06 1A FF 4C 00 02 15 or 02 01 1A 1A FF 4C 00 02 15 which isn't super helpful, since the 4C 00 appears to just be the Apple identifier, which I'm assuming could switch based on the manufacturer (Estimote, GE, Apple, whatever). However, it appears that the 02 15 is static, so I'm using that. My solution for finding it basically is just searching the byte array that I get for the first occurrence of that sequence of 2 bytes, and starting right after that I grab the next 16 as the UUID, the next 2 after that as the Major, and the next 2 after that as the Minor. So to clarify with code: //adPacket is an array I created to represent an iBeacon ad for testing byte[] adPacket = {(byte) 0xd6, (byte) 0xbe, (byte) 0x89, (byte) 0x8e, 0x40, 0x24, 0x05, (byte) 0xa2, 0x17, 0x6e, 0x3d, 0x71, 0x02, 0x01, 0x06, 0x1A, (byte) 0xFF, 0x4C, 0x00, 0x02, 0x15, (byte) 0xe2, (byte) 0xc5, 0x6d, (byte) 0xb5, (byte) 0xdf, (byte) 0xfb, 0x48, (byte) 0xd2, (byte) 0xb0, 0x60, (byte) 0xd0, (byte) 0xf5, (byte) 0xa7, 0x10, (byte) 0x96, (byte) 0xe0, 0x00, 0x00, 0x00, 0x00, (byte) 0xc5, 0x52, (byte) 0xab, (byte) 0x8d, 0x38, (byte) 0xa5}; byte[] pattern = {0x02, 0x15}; int startIndex = findAdPacketEnd(adPacket, pattern) + 1; if(startIndex == 0){ System.out.println("Pattern not found"); return;} int endIndex = startIndex + 21; ArrayList<Byte> tempArray = new ArrayList<Byte>(); for(int i = startIndex; i<endIndex; i++){ tempArray.add(adPacket[i]); } byte[] proxUUID = new byte[16]; for(int i = 0; i<16; i++){ proxUUID[i] = tempArray.get(i); } byte[] major = new byte[2]; major[0] = tempArray.get(16); major[1] = tempArray.get(17); byte[] minor = new byte[2]; minor[0] = tempArray.get(18); minor[1] = tempArray.get(19); ... where my findAdPacketEnd function is (not the most efficient but it works): private static int findAdPacketEnd(byte[] adPacket, byte[] pattern){ //return -1 if pattern is too long if(adPacket.length < pattern.length) return -1; //iterate over adPacket for(int i = 0; i<adPacket.length - pattern.length; i++){ System.out.println("Searching Ad Packet"); //iterate over pattern for(int j = 0; j<pattern.length; j++){ //compare wherever you are in the adpacket to the pattern, break if it doesn't match if(adPacket[i+j] != pattern[j]) break; // if you get to the end of the pattern and there wasn't a mismatch, return the index if(j == pattern.length-1) return i+pattern.length-1; } } //pattern not found return -1; }
Wifi printer not printing the pages android
I have using the following code: Socket client = new Socket(etIp.getText().toString(), 515); String printText = "This is a print test from Wifi"; byte[] mybytearray = printText.getBytes(); OutputStream outputStream = client.getOutputStream(); outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte outputStream.flush(); outputStream.close(); client.close(); Socket connection opened, and no exception while complete process of write data in output stream. But printer not eject the page. Please help me what I have did wrong.
I think you need to send few more data for it to eject and let the printer know. For Epson L355 I am appending a byte array at the end of my string. byte[] EjectByteArray = {0X00, 0X0D, 0X0C, 0X1B, 0X40, 0X1B, 0X28, 0X52, 0X08, 0X00, 0X00, 0X52, 0X45, 0X4D, 0X4F, 0X54, 0X45, 0X31, 0X4C, 0X44, 0X00, 0X00, 0X1B, 0X00, 0X00, 0X00, 0X1B, 0X40, 0X1B, 0X28, 0X52, 0X08, 0X00, 0X00, 0X52, 0X45, 0X4D, 0X4F, 0X54, 0X45, 0X31, 0X4C, 0X44, 0X00, 0X00, 0X4A, 0X45, 0X01, 0X00, 0X00, 0X1B, 0X00, 0X00, 0X00}; You may need to run a Port sniffer and try to capture the last byte array being passed after a print is finished. It would give you a clue on how you should send an eject command. This is one works on me using port 9100 on Epson L355 outputStream.write(mybytearray, 0, mybytearray.length); outputStream.write(EjectByteArray, 0, EjectByteArray.length); outputStream.flush(); outputStream.close(); client.close();
Add \n end of the line.this is work for me String printText = "Finally its working \n";
NFC Smartposter to Dial a number
i want to create a NFC SmartPoster which dial a number with Action Record type "act". Can anyone tell how to get Action Record type "act" in android from packet and check whether packet contains Action Record type "act" or not. Below is packet i have created. /** * Smart Poster containing a Telephone number and Action record type. */ public static final byte[] SMART_POSTER_Dial_Number = new byte[] { // SP type record (byte) 0xd1, (byte) 0x02, (byte) 0x26, (byte) 0x53, (byte) 0x70, // Call type record (byte) 0xd1, (byte) 0x01, (byte) 0x0e, (byte) 0x55, (byte) 0x05, (byte) 0x2b, (byte) 0x39, (byte) 0x31, (byte) 0x38, (byte) 0x38, (byte) 0x37, (byte) 0x32, (byte) 0x37, (byte) 0x34, (byte) 0x33, (byte) 0x39, (byte) 0x33, (byte) 0x39, // Action type record (byte) 0x11, (byte) 0x03, (byte) 0x01, (byte) 0x61, (byte) 0x63, (byte) 0x74, (byte) 0x00, // Text type record with 'T' (byte) 0x91, (byte) 0x01, (byte) 0x09, (byte) 0x54, (byte) 0x02, (byte) 'C', (byte) 'a', (byte) 'l', (byte) 'l', (byte) 'i', (byte) 'n', (byte) 'g', (byte) '.' }; Please help..
When you receive an NDEF message in your Activity via an ACTION_NDEF_DISCOVERED intent, you can parse and check the contents for a SmartPoster record with an embedded 'act' record as follows: Intent intent = getIntent(); final Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage mesg = (NdefMessage) rawMsgs[0]; // in theory there can be more messages // let's inspect the first record only NdefRecord[] record = mesg.getRecords()[0]; byte[] type = record.getType(); // check if it is a SmartPoster byte[] smartPoster = { 'S', 'p'}; if (Arrays.equals(smartPoster, type) { byte[] payload = record.getPayload(); // try to parse the payload as NDEF message NdefMessage n; try { n = new NdefMessage(payload); } catch (FormatException e) { return; // not an NDEF message, we're done } // try to find the 'act' record NdefRecord[] recs = n.getRecords(); byte[] act = { 'a', 'c', 't' }; for (NdefRecord r : recs) { if (Arrays.equals(act, r.getType()) { ... // found it; do your thing! return; } } } return; // nothing found BTW: You will find that there are a couple of format errors in the example message in your question: the first byte of the Uri record should be 0x81 and the first byte of the Text record should be 0x51.