Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Bluetooth Printer Android
- HOWTO:
- 1. Install app ini di android
- 2. Buat dokumen yang mau di print, taruh ke web
- 3. Buat halaman web berisi link seperti contoh di bawah.
- 4. Nanti kalau linknya diklik, dokumen kecetak deh.
- 5. Enjoy!
- <h1>Contoh</h1>
- <p>Untuk ngeprint isi halaman http://gungsukma.pw/test.txt ke printer MTP-II,
- klik <a href="myapp://ahok.gungsukma.com/?device=MTP-II&url=http://gungsukma.pw/test.txt">di sini</a>.</p>
- */
- package com.gungsukma.btp;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.widget.Toast;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.UUID;
- // Permisi, manifest numpang lewat..
- /*
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.gungsukma.btp">
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <application
- android:allowBackup="true"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme"
- android:usesCleartextTraffic="true">
- <activity android:name=".PrintFile">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.BROWSABLE" />
- <data android:host="ahok.gungsukma.com" />
- <data android:scheme="myapp"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- */
- public class PrintFile extends AppCompatActivity {
- private static final UUID MY_UUID = UUID.fromString( "00001101-0000-1000-8000-00805F9B34FB" );
- private static final String newLine = System.getProperty( "line.separator" );
- public void showToast( final String toast ) {
- runOnUiThread( new Runnable() {
- @Override public void run() {
- Toast.makeText( PrintFile.this, toast, Toast.LENGTH_SHORT ).show();
- }
- } );
- Log.d( "BTP", toast );
- }
- // Mendownload webpage (atau tepatnya dokumen), mengembalikan kontennya
- public String downloadWebpage( String uri ) {
- StringBuilder sb = new StringBuilder();
- try {
- URL url = new URL( uri );
- URLConnection connection = url.openConnection();
- connection.connect();
- int i = connection.getContentLength();
- BufferedInputStream in = new BufferedInputStream( url.openStream() );
- try {
- byte dataBuffer[] = new byte[1024];
- int bytesRead;
- while ( ( bytesRead = in.read( dataBuffer, 0, 1024 ) ) != -1 ) {
- String s = new String( dataBuffer, 0, bytesRead );
- sb.append( s );
- }
- } catch ( Exception e ) {
- showToast( "Cannot download webpage ( 1 ). " + e.getMessage() );
- }
- in.close();
- } catch ( Exception e ) {
- showToast( "Cannot download webpage ( 2 ). " + e.getMessage() );
- }
- return sb.toString();
- } // downloadWebpage
- @Override protected void onCreate( Bundle savedInstanceState ) {
- super.onCreate( savedInstanceState );
- setContentView( R.layout.layout );
- Intent intent = getIntent();
- if ( intent.getAction().equals( Intent.ACTION_VIEW ) && intent.getScheme().equals( "myapp" ) ) {
- Uri uri = intent.getData();
- final String deviceName = uri.getQueryParameter( "deviceName" );
- final String url = uri.getQueryParameter( "url" );
- new Thread( new Runnable() {
- public void run() {
- DoBTP( deviceName, url );
- runOnUiThread( new Runnable() {
- @Override public void run() {
- PrintFile.this.finish();
- }
- } );
- }
- } ).start();
- }
- } // onCreate
- // Print ke bluetooth printer
- public void DoBTP( String deviceName, String url ) {
- BluetoothAdapter btAdapter;
- BluetoothDevice btDevice = null;
- BluetoothSocket btSocket = null;
- String address = null;
- btAdapter = BluetoothAdapter.getDefaultAdapter();
- for ( BluetoothDevice d : btAdapter.getBondedDevices() )
- if ( d.getName().equals( deviceName ) || d.getAddress().equals( deviceName ) )
- address = d.getAddress();
- try {
- btDevice = btAdapter.getRemoteDevice( address );
- } catch ( Exception e ) {
- showToast( "Cannot find device. " + e.getMessage() );
- }
- try {
- btSocket = btDevice.createRfcommSocketToServiceRecord( MY_UUID );
- } catch ( IOException e ) {
- showToast( "Cannot socket. " + e.getMessage() );
- }
- try {
- btSocket.connect();
- try {
- InputStream tmpIn = btSocket.getInputStream();
- OutputStream tmpOut = btSocket.getOutputStream();
- try {
- int bytes = tmpIn.available();
- byte[] buffer = new byte[1024];
- if ( bytes != 0 )
- tmpIn.read( buffer, 0, bytes );
- } catch ( IOException e ) {
- showToast( "Cannot empty buffer. " + e.getMessage() );
- }
- for ( String s : downloadWebpage( url ).split( newLine ) )
- tmpOut.write( ( s + "\n" ).getBytes() );
- // TODO: I'm trying to cut paper here
- // 29 'V' 48 atau 29 'V' 65 atau GS 86 65 0
- String cutPaper = new String( new byte[]{'\n', 0x1B, 'm', '\n'} );
- tmpOut.write( cutPaper.getBytes() );
- } catch ( IOException e ) {
- showToast( "Cannot stream. " + e.getMessage() );
- }
- } catch ( IOException e ) {
- showToast( "Cannot connect. " + e.getMessage() );
- } finally {
- try {
- btSocket.close();
- } catch ( IOException e ) {
- showToast( "Cannot close socket. " + e.getMessage() );
- }
- }
- } // DoBTP
- } // class
Advertisement
Add Comment
Please, Sign In to add comment