2222import android .view .View ;
2323import android .widget .Button ;
2424import android .widget .EditText ;
25- import android .widget .Toast ;
2625
2726import com .dantsu .escposprinter .EscPosPrinter ;
2827import com .dantsu .escposprinter .connection .DeviceConnection ;
2928import com .dantsu .escposprinter .connection .bluetooth .BluetoothConnection ;
30- import com .dantsu .escposprinter .connection .bluetooth .BluetoothConnections ;
3129import com .dantsu .escposprinter .connection .bluetooth .BluetoothPrintersConnections ;
3230import com .dantsu .escposprinter .connection .tcp .TcpConnection ;
3331import com .dantsu .escposprinter .connection .usb .UsbConnection ;
@@ -52,39 +50,32 @@ public class MainActivity extends AppCompatActivity {
5250 protected void onCreate (Bundle savedInstanceState ) {
5351 super .onCreate (savedInstanceState );
5452 setContentView (R .layout .activity_main );
55- Button button = (Button ) this .findViewById (R .id .button_bluetooth );
53+ Button button = (Button ) this .findViewById (R .id .button_bluetooth_browse );
5654 button .setOnClickListener (new View .OnClickListener () {
5755 @ Override
5856 public void onClick (View view ) {
59- printBluetooth ();
60- }
61- });
62- button = (Button ) this .findViewById (R .id .button_usb );
63- button .setOnClickListener (new View .OnClickListener () {
64- @ Override
65- public void onClick (View view ) {
66- printUsb ();
57+ browseBluetoothDevice ();
6758 }
6859 });
69- button = (Button ) this . findViewById (R .id .button_tcp );
60+ button = (Button ) findViewById (R .id .button_bluetooth );
7061 button .setOnClickListener (new View .OnClickListener () {
7162 @ Override
7263 public void onClick (View view ) {
73- printTcp ();
64+ printBluetooth ();
7465 }
7566 });
76- button = (Button ) this .findViewById (R .id .button_browse );
67+ button = (Button ) this .findViewById (R .id .button_usb );
7768 button .setOnClickListener (new View .OnClickListener () {
7869 @ Override
7970 public void onClick (View view ) {
80- browseBluetoothDevice ();
71+ printUsb ();
8172 }
8273 });
83- button = (Button ) findViewById (R .id .button_bluetooth2 );
74+ button = (Button ) this . findViewById (R .id .button_tcp );
8475 button .setOnClickListener (new View .OnClickListener () {
8576 @ Override
8677 public void onClick (View view ) {
87- printBySelectedBluetoothDevice ();
78+ printTcp ();
8879 }
8980 });
9081
@@ -97,34 +88,6 @@ public void onClick(View view) {
9788
9889 public static final int PERMISSION_BLUETOOTH = 1 ;
9990
100- public void printBluetooth () {
101- if (ContextCompat .checkSelfPermission (this , Manifest .permission .BLUETOOTH ) != PackageManager .PERMISSION_GRANTED ) {
102- ActivityCompat .requestPermissions (this , new String []{Manifest .permission .BLUETOOTH }, MainActivity .PERMISSION_BLUETOOTH );
103- } else {
104- //first pair bluetooth device print
105- //synchronized print
106- //this.printIt(BluetoothPrintersConnections.selectFirstPaired());
107- //asynchronized print
108- new AsyncBluetoothEscPosPrint (this ).execute (this .getAsyncEscPosPrinter (null ));
109-
110- }
111- }
112-
113- public void printBySelectedBluetoothDevice (){
114- if (ContextCompat .checkSelfPermission (this , Manifest .permission .BLUETOOTH ) != PackageManager .PERMISSION_GRANTED ) {
115- ActivityCompat .requestPermissions (this , new String []{Manifest .permission .BLUETOOTH }, MainActivity .PERMISSION_BLUETOOTH );
116- } else {
117- //select device for print
118- if (selectedDevice != null ){
119- // this.printIt(selectedDevice); //synchronized print
120- new AsyncBluetoothEscPosPrint (this ).execute (this .getAsyncEscPosPrinter (selectedDevice )); //asynchronized print
121- }else {
122- Toast .makeText (this ,"Please Select A Device" ,Toast .LENGTH_SHORT ).show ();
123- }
124- }
125-
126- }
127-
12891 @ Override
12992 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , int [] grantResults ) {
13093 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
@@ -137,25 +100,31 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
137100 }
138101
139102 private BluetoothConnection selectedDevice ;
140- public void browseBluetoothDevice (){
141- final BluetoothConnection [] bluetoothDevicesList = (new BluetoothConnections ()).getList ();
142103
143- if (bluetoothDevicesList != null ){
144- int i = 0 ;
145- String [] items = new String [bluetoothDevicesList .length ];
104+ public void browseBluetoothDevice () {
105+ final BluetoothConnection [] bluetoothDevicesList = (new BluetoothPrintersConnections ()).getList ();
146106
147- for (BluetoothConnection device : bluetoothDevicesList ){
148- items [i ++] = device .getDevice ().getName ();
107+ if (bluetoothDevicesList != null ) {
108+ final String [] items = new String [bluetoothDevicesList .length + 1 ];
109+ items [0 ] = "Default printer" ;
110+ int i = 0 ;
111+ for (BluetoothConnection device : bluetoothDevicesList ) {
112+ items [++i ] = device .getDevice ().getName ();
149113 }
150114
151115 AlertDialog .Builder alertDialog = new AlertDialog .Builder (MainActivity .this );
152- alertDialog .setTitle ("Pick Device " );
116+ alertDialog .setTitle ("Bluetooth printer selection " );
153117 alertDialog .setItems (items , new DialogInterface .OnClickListener () {
154118 @ Override
155119 public void onClick (DialogInterface dialogInterface , int i ) {
156- selectedDevice = bluetoothDevicesList [i ];
157- Button button = (Button ) findViewById (R .id .button_bluetooth2 );
158- button .setText ("Print By: " + selectedDevice .getDevice ().getName ());
120+ int index = i - 1 ;
121+ if (index == -1 ) {
122+ selectedDevice = null ;
123+ } else {
124+ selectedDevice = bluetoothDevicesList [index ];
125+ }
126+ Button button = (Button ) findViewById (R .id .button_bluetooth_browse );
127+ button .setText (items [i ]);
159128 }
160129 });
161130
@@ -164,9 +133,15 @@ public void onClick(DialogInterface dialogInterface, int i) {
164133 alert .show ();
165134
166135 }
167-
168136 }
169137
138+ public void printBluetooth () {
139+ if (ContextCompat .checkSelfPermission (this , Manifest .permission .BLUETOOTH ) != PackageManager .PERMISSION_GRANTED ) {
140+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .BLUETOOTH }, MainActivity .PERMISSION_BLUETOOTH );
141+ } else {
142+ new AsyncBluetoothEscPosPrint (this ).execute (this .getAsyncEscPosPrinter (selectedDevice ));
143+ }
144+ }
170145
171146 /*==============================================================================================
172147 ===========================================USB PART=============================================
0 commit comments