I'm working on Qr generator to take data from another activity (text view,spinner) and print it in Bluetooth printer name id no chosen spinner Qr image depending on the data given above and print it in Bluetooth printer
public class Main3Activity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
BluetoothAdapter bluetoothAdapter;
BluetoothSocket bluetoothSocket;
BluetoothDevice bluetoothDevice;
OutputStream outputStream;
InputStream inputStream;
Thread thread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;
TextView lblPrinterName;
EditText textBox;
public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 400;
public final static int HEIGHT = 400;
public final static String STR = "A string to be encoded as QR code";
public class MySpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
String value= getIntent().getStringExtra("getData");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ImageView imageView = (ImageView) findViewById(R.id.myImage);
try {
Bitmap bitmap = encodeAsBitmap(STR);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void openMain2Activity(){
Intent intent= new Intent(this,Main3Activity.class);
startActivity(intent);
// Create object of controls
Button btnConnect = (Button) findViewById(R.id.button);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
FindBluetoothDevice();
openBluetoothPrinter();
}catch (Exception ex){
ex.printStackTrace();
}
}
});
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
printData();
}catch (Exception ex){
ex.printStackTrace();
}
}
});
}
void FindBluetoothDevice(){
try{
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
lblPrinterName.setText("No Bluetooth Adapter found");
}
if(bluetoothAdapter.isEnabled()){
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT,0);
}
Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices();
if(pairedDevice.size()>0){
for(BluetoothDevice pairedDev:pairedDevice){
// My Bluetoth printer name is BTP_F09F1A
if(pairedDev.getName().equals("BTP_F09F1A")){
bluetoothDevice=pairedDev;
lblPrinterName.setText("Bluetooth Printer Attached: "+pairedDev.getName());
break;
}
}
}
lblPrinterName.setText("Bluetooth Printer Attached");
}catch(Exception ex){
ex.printStackTrace();
}
}
// Open Bluetooth Printer
void openBluetoothPrinter() throws IOException {
try{
//Standard uuid from string //
UUID uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
bluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting);
bluetoothSocket.connect();
outputStream=bluetoothSocket.getOutputStream();
inputStream=bluetoothSocket.getInputStream();
beginListenData();
}catch (Exception ex){
}
}
void beginListenData(){
try{
final Handler handler =new Handler();
final byte delimiter=10;
stopWorker =false;
readBufferPosition=0;
readBuffer = new byte[1024];
thread=new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker){
try{
int byteAvailable = inputStream.available();
if(byteAvailable>0){
byte[] packetByte = new byte[byteAvailable];
inputStream.read(packetByte);
for(int i=0; i<byteAvailable; i++){
byte b = packetByte[i];
if(b==delimiter){
byte[] encodedByte = new byte[readBufferPosition];
System.arraycopy(
readBuffer,0,
encodedByte,0,
encodedByte.length
);
final String data = new String(encodedByte,"US-ASCII");
readBufferPosition=0;
handler.post(new Runnable() {
@Override
public void run() {
lblPrinterName.setText(data);
}
});
}else{
readBuffer[readBufferPosition++]=b;
}
}
}
}catch(Exception ex){
stopWorker=true;
}
}
}
});
thread.start();
}catch (Exception ex){
ex.printStackTrace();
}
}
// Printing Text to Bluetooth Printer //
void printData() throws IOException{
try{
String msg = textBox.getText().toString();
msg+="\n";
outputStream.write(msg.getBytes());
lblPrinterName.setText("Printing Text...");
}catch (Exception ex){
ex.printStackTrace();
}
}
// Disconnect Printer //
void disconnectBT() throws IOException{
try {
stopWorker=true;
outputStream.close();
inputStream.close();
bluetoothSocket.close();
lblPrinterName.setText("Printer Disconnected.");
}catch (Exception ex){
ex.printStackTrace();
}
}
}
name id no chosen spinner Qr image depending on the data given above and print it in Bluetooth prinrter
User contributions licensed under CC BY-SA 3.0