@@ -1,10 +1,16 @@ |
||
1 | 1 |
package ai.pai.lensman.printer; |
2 | 2 |
|
3 |
+import android.bluetooth.BluetoothDevice; |
|
3 | 4 |
import android.content.Context; |
4 | 5 |
import android.view.LayoutInflater; |
5 | 6 |
import android.view.View; |
6 | 7 |
import android.view.ViewGroup; |
7 | 8 |
import android.widget.BaseAdapter; |
9 |
+import android.widget.TextView; |
|
10 |
+ |
|
11 |
+import java.util.ArrayList; |
|
12 |
+ |
|
13 |
+import ai.pai.lensman.R; |
|
8 | 14 |
|
9 | 15 |
/** |
10 | 16 |
* Created by chengzhenyu on 2016/9/1. |
@@ -13,28 +19,68 @@ import android.widget.BaseAdapter; |
||
13 | 19 |
public class BluetoothDeviceListAdapter extends BaseAdapter { |
14 | 20 |
|
15 | 21 |
private LayoutInflater inflater; |
22 |
+ private ArrayList<BluetoothDevice> deviceList; |
|
16 | 23 |
|
17 | 24 |
public BluetoothDeviceListAdapter(Context context){ |
18 | 25 |
inflater = LayoutInflater.from(context); |
19 | 26 |
} |
20 | 27 |
|
28 |
+ public void setDeviceList(ArrayList<BluetoothDevice> newDeviceList){ |
|
29 |
+ this.deviceList = newDeviceList; |
|
30 |
+ notifyDataSetChanged(); |
|
31 |
+ } |
|
32 |
+ |
|
33 |
+ public void addDevice(BluetoothDevice device){ |
|
34 |
+ if(this.deviceList==null){ |
|
35 |
+ this.deviceList = new ArrayList<>(); |
|
36 |
+ } |
|
37 |
+ if(!this.deviceList.contains(device)){ |
|
38 |
+ this.deviceList.add(device); |
|
39 |
+ } |
|
40 |
+ notifyDataSetChanged(); |
|
41 |
+ } |
|
42 |
+ |
|
21 | 43 |
@Override |
22 | 44 |
public int getCount() { |
23 |
- return 0; |
|
45 |
+ if(this.deviceList==null){ |
|
46 |
+ return 0; |
|
47 |
+ } |
|
48 |
+ return this.deviceList.size(); |
|
24 | 49 |
} |
25 | 50 |
|
26 | 51 |
@Override |
27 | 52 |
public Object getItem(int i) { |
28 |
- return null; |
|
53 |
+ return this.deviceList.get(i); |
|
29 | 54 |
} |
30 | 55 |
|
31 | 56 |
@Override |
32 | 57 |
public long getItemId(int i) { |
33 |
- return 0; |
|
58 |
+ return i; |
|
34 | 59 |
} |
35 | 60 |
|
36 | 61 |
@Override |
37 |
- public View getView(int i, View view, ViewGroup viewGroup) { |
|
38 |
- return null; |
|
62 |
+ public View getView(int position, View convertView, ViewGroup viewGroup) { |
|
63 |
+ ViewHolder holder; |
|
64 |
+ if(convertView==null){ |
|
65 |
+ convertView = inflater.inflate(R.layout.layout_bt_device_list_item,viewGroup,false); |
|
66 |
+ holder = new ViewHolder(); |
|
67 |
+ holder.deviceMac =(TextView) convertView.findViewById(R.id.tv_device_mac); |
|
68 |
+ holder.deviceName =(TextView) convertView.findViewById(R.id.tv_device_name); |
|
69 |
+ holder.deviceStatus =(TextView) convertView.findViewById(R.id.tv_device_status); |
|
70 |
+ convertView.setTag(holder); |
|
71 |
+ }else{ |
|
72 |
+ holder = (ViewHolder) convertView.getTag(); |
|
73 |
+ } |
|
74 |
+ BluetoothDevice device = deviceList.get(position); |
|
75 |
+ holder.deviceName.setText(device.getName()); |
|
76 |
+ holder.deviceMac.setText(device.getAddress()); |
|
77 |
+ return convertView; |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ public static class ViewHolder{ |
|
81 |
+ |
|
82 |
+ public TextView deviceName; |
|
83 |
+ public TextView deviceMac; |
|
84 |
+ public TextView deviceStatus; |
|
39 | 85 |
} |
40 | 86 |
} |
@@ -1,5 +1,9 @@ |
||
1 | 1 |
package ai.pai.lensman.printer; |
2 | 2 |
|
3 |
+import android.app.Activity; |
|
4 |
+import android.bluetooth.BluetoothAdapter; |
|
5 |
+import android.bluetooth.BluetoothDevice; |
|
6 |
+import android.content.Intent; |
|
3 | 7 |
import android.os.Bundle; |
4 | 8 |
import android.support.annotation.Nullable; |
5 | 9 |
import android.widget.ListView; |
@@ -9,28 +13,31 @@ import android.widget.ToggleButton; |
||
9 | 13 |
|
10 | 14 |
import com.android.views.mergeAdapter.MergeAdapter; |
11 | 15 |
|
16 |
+import java.util.ArrayList; |
|
17 |
+import java.util.List; |
|
18 |
+ |
|
12 | 19 |
import ai.pai.lensman.R; |
13 | 20 |
import ai.pai.lensman.base.BaseActivity; |
14 | 21 |
import butterknife.BindView; |
15 | 22 |
import butterknife.ButterKnife; |
23 |
+import butterknife.OnCheckedChanged; |
|
16 | 24 |
import butterknife.OnClick; |
17 | 25 |
|
18 |
-/** |
|
19 |
- * Created by chengzhenyu on 2016/8/31. |
|
20 |
- */ |
|
21 |
- |
|
22 | 26 |
public class PrinterSettingActivity extends BaseActivity implements PrinterSettingContract.View { |
23 | 27 |
|
28 |
+ @BindView(R.id.listview_bt_devices) ListView btDevicesList; |
|
24 | 29 |
@BindView(R.id.tv_printer_status) TextView printerStatusText; |
25 | 30 |
@BindView(R.id.tv_bluetooth_status) TextView bluetoothStatusText; |
26 | 31 |
@BindView(R.id.tb_bluetooth_switch) ToggleButton btSwitchToggle; |
27 |
- @BindView(R.id.list_bt_devices) ListView btDevicesList; |
|
28 | 32 |
|
29 | 33 |
private MergeAdapter mergeAdapter; |
30 | 34 |
private BluetoothDeviceListAdapter newDeviceAdapter; |
31 | 35 |
private BluetoothDeviceListAdapter pairedDeviceAdapter; |
32 | 36 |
private PrinterSettingContract.Presenter presenter; |
33 | 37 |
|
38 |
+ public static final int REQUEST_ENABLE_BT = 2; |
|
39 |
+ public static final int REQUEST_CONNECT_DEVICE = 3; |
|
40 |
+ |
|
34 | 41 |
@Override |
35 | 42 |
protected void onCreate(@Nullable Bundle savedInstanceState) { |
36 | 43 |
super.onCreate(savedInstanceState); |
@@ -44,7 +51,13 @@ public class PrinterSettingActivity extends BaseActivity implements PrinterSetti |
||
44 | 51 |
btDevicesList.setAdapter(mergeAdapter); |
45 | 52 |
mergeAdapter.addAdapter(pairedDeviceAdapter); |
46 | 53 |
mergeAdapter.addAdapter(newDeviceAdapter); |
54 |
+ |
|
47 | 55 |
presenter = new PrinterSettingPresenter(this,this); |
56 |
+ } |
|
57 |
+ |
|
58 |
+ @Override |
|
59 |
+ protected void onResume() { |
|
60 |
+ super.onResume(); |
|
48 | 61 |
presenter.start(); |
49 | 62 |
} |
50 | 63 |
|
@@ -56,11 +69,13 @@ public class PrinterSettingActivity extends BaseActivity implements PrinterSetti |
||
56 | 69 |
|
57 | 70 |
@Override |
58 | 71 |
public void onBluetoothEnabled() { |
72 |
+ btSwitchToggle.setChecked(true); |
|
59 | 73 |
bluetoothStatusText.setText(R.string.bt_is_enabled); |
60 | 74 |
} |
61 | 75 |
|
62 | 76 |
@Override |
63 | 77 |
public void onBluetoothDisabled() { |
78 |
+ btSwitchToggle.setChecked(false); |
|
64 | 79 |
bluetoothStatusText.setText(R.string.bt_is_disabled); |
65 | 80 |
} |
66 | 81 |
|
@@ -76,31 +91,67 @@ public class PrinterSettingActivity extends BaseActivity implements PrinterSetti |
||
76 | 91 |
|
77 | 92 |
@Override |
78 | 93 |
public void onPrinterConnected() { |
79 |
- Toast.makeText(this,R.string.printer_is_connected,Toast.LENGTH_SHORT).show(); |
|
94 |
+ showToast(getString(R.string.printer_is_connected)); |
|
80 | 95 |
} |
81 | 96 |
|
82 | 97 |
@Override |
83 | 98 |
public void onPrinterDisconnected() { |
84 |
- Toast.makeText(this,R.string.printer_is_disconnected,Toast.LENGTH_SHORT).show(); |
|
99 |
+ showToast(getString(R.string.printer_is_disconnected)); |
|
85 | 100 |
} |
86 | 101 |
|
87 | 102 |
@Override |
88 |
- public void onNewDeviceDiscovered() { |
|
89 |
- |
|
103 |
+ public void onNewDeviceDiscovered(BluetoothDevice device) { |
|
104 |
+ newDeviceAdapter.addDevice(device); |
|
90 | 105 |
} |
91 | 106 |
|
107 |
+ |
|
92 | 108 |
@Override |
93 | 109 |
public void onPrinterTestPageFail() { |
94 | 110 |
|
95 | 111 |
} |
96 | 112 |
|
97 | 113 |
@Override |
98 |
- public void onPairedDeviceDiscovered() { |
|
114 |
+ public void onPairedDeviceDiscovered(List<BluetoothDevice> devices) { |
|
115 |
+ pairedDeviceAdapter.setDeviceList((ArrayList<BluetoothDevice>)devices); |
|
116 |
+ } |
|
117 |
+ |
|
99 | 118 |
|
119 |
+ @Override |
|
120 |
+ public void showToast(String msg) { |
|
121 |
+ Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); |
|
100 | 122 |
} |
101 | 123 |
|
102 | 124 |
@OnClick(R.id.tv_print_test) |
103 | 125 |
void testPrint(){ |
104 | 126 |
presenter.printTestPage(); |
105 | 127 |
} |
128 |
+ |
|
129 |
+ @OnCheckedChanged(R.id.tb_bluetooth_switch) |
|
130 |
+ void switchBluetooth(){ |
|
131 |
+ if(!presenter.queryBluetoothStatus()){ |
|
132 |
+ Intent enableIntent = new Intent( |
|
133 |
+ BluetoothAdapter.ACTION_REQUEST_ENABLE); |
|
134 |
+ startActivityForResult(enableIntent, |
|
135 |
+ REQUEST_ENABLE_BT); |
|
136 |
+ }else{ |
|
137 |
+ BluetoothAdapter.getDefaultAdapter().disable(); |
|
138 |
+ onBluetoothDisabled(); |
|
139 |
+ } |
|
140 |
+ } |
|
141 |
+ |
|
142 |
+ @Override |
|
143 |
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
|
144 |
+ super.onActivityResult(requestCode, resultCode, data); |
|
145 |
+ if(resultCode == Activity.RESULT_OK){ |
|
146 |
+ if(requestCode == REQUEST_ENABLE_BT){ |
|
147 |
+ onBluetoothEnabled(); |
|
148 |
+ onPairedDeviceDiscovered(presenter.queryPairedDevices()); |
|
149 |
+ presenter.discoverNewDevices(); |
|
150 |
+ presenter.queryPrinterStatus(); |
|
151 |
+ }else if(requestCode == REQUEST_CONNECT_DEVICE){ |
|
152 |
+ |
|
153 |
+ } |
|
154 |
+ } |
|
155 |
+ } |
|
156 |
+ |
|
106 | 157 |
} |
@@ -1,5 +1,9 @@ |
||
1 | 1 |
package ai.pai.lensman.printer; |
2 | 2 |
|
3 |
+import android.bluetooth.BluetoothDevice; |
|
4 |
+ |
|
5 |
+import java.util.List; |
|
6 |
+ |
|
3 | 7 |
/** |
4 | 8 |
* Created by chengzhenyu on 2016/9/1. |
5 | 9 |
*/ |
@@ -11,12 +15,11 @@ public class PrinterSettingContract { |
||
11 | 15 |
void stop(); |
12 | 16 |
void queryPrinterStatus(); |
13 | 17 |
void printTestPage(); |
14 |
- void queryBluetoothStatus(); |
|
15 |
- void enableBluetooth(); |
|
16 |
- void disableBluetooth(); |
|
17 |
- void queryPairedDevices(); |
|
18 |
+ boolean queryBluetoothStatus(); |
|
19 |
+ List<BluetoothDevice> queryPairedDevices(); |
|
18 | 20 |
void discoverNewDevices(); |
19 | 21 |
void connectPrinter(); |
22 |
+ void cancelDiscovery(); |
|
20 | 23 |
} |
21 | 24 |
|
22 | 25 |
public interface View{ |
@@ -26,8 +29,9 @@ public class PrinterSettingContract { |
||
26 | 29 |
void onPrinterError(String error); |
27 | 30 |
void onPrinterConnected(); |
28 | 31 |
void onPrinterDisconnected(); |
29 |
- void onNewDeviceDiscovered(); |
|
32 |
+ void onNewDeviceDiscovered(BluetoothDevice device); |
|
30 | 33 |
void onPrinterTestPageFail(); |
31 |
- void onPairedDeviceDiscovered(); |
|
34 |
+ void onPairedDeviceDiscovered(List<BluetoothDevice> devices); |
|
35 |
+ void showToast(String msg); |
|
32 | 36 |
} |
33 | 37 |
} |
@@ -1,6 +1,15 @@ |
||
1 | 1 |
package ai.pai.lensman.printer; |
2 | 2 |
|
3 |
+import android.bluetooth.BluetoothAdapter; |
|
4 |
+import android.bluetooth.BluetoothDevice; |
|
5 |
+import android.content.BroadcastReceiver; |
|
3 | 6 |
import android.content.Context; |
7 |
+import android.content.Intent; |
|
8 |
+import android.content.IntentFilter; |
|
9 |
+ |
|
10 |
+import java.util.List; |
|
11 |
+ |
|
12 |
+import ai.pai.lensman.R; |
|
4 | 13 |
|
5 | 14 |
/** |
6 | 15 |
* Created by chengzhenyu on 2016/9/1. |
@@ -10,20 +19,29 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
10 | 19 |
|
11 | 20 |
private Context context; |
12 | 21 |
private PrinterSettingContract.View view; |
22 |
+ private BluetoothAdapter bluetoothAdapter; |
|
13 | 23 |
|
14 | 24 |
public PrinterSettingPresenter(Context context, PrinterSettingContract.View view){ |
15 | 25 |
this.view = view; |
16 | 26 |
this.context = context; |
27 |
+ bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
|
17 | 28 |
} |
18 | 29 |
|
19 | 30 |
@Override |
20 | 31 |
public void start() { |
21 |
- |
|
32 |
+ if(queryBluetoothStatus()){ |
|
33 |
+ view.onBluetoothEnabled(); |
|
34 |
+ view.showToast(context.getString(R.string.query_processing)); |
|
35 |
+ queryPrinterStatus(); |
|
36 |
+ }else{ |
|
37 |
+ view.onBluetoothDisabled(); |
|
38 |
+ view.showToast(context.getString(R.string.bt_is_disabled)); |
|
39 |
+ } |
|
22 | 40 |
} |
23 | 41 |
|
24 | 42 |
@Override |
25 | 43 |
public void stop() { |
26 |
- |
|
44 |
+ cancelDiscovery(); |
|
27 | 45 |
} |
28 | 46 |
|
29 | 47 |
@Override |
@@ -37,32 +55,66 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter |
||
37 | 55 |
} |
38 | 56 |
|
39 | 57 |
@Override |
40 |
- public void queryBluetoothStatus() { |
|
41 |
- |
|
58 |
+ public boolean queryBluetoothStatus() { |
|
59 |
+ if(bluetoothAdapter==null){ |
|
60 |
+ return false; |
|
61 |
+ } |
|
62 |
+ return bluetoothAdapter.isEnabled(); |
|
42 | 63 |
} |
43 | 64 |
|
44 | 65 |
@Override |
45 |
- public void enableBluetooth() { |
|
66 |
+ public List<BluetoothDevice> queryPairedDevices() { |
|
67 |
+ return (List<BluetoothDevice>) bluetoothAdapter.getBondedDevices(); |
|
46 | 68 |
|
47 | 69 |
} |
48 | 70 |
|
49 | 71 |
@Override |
50 |
- public void disableBluetooth() { |
|
51 |
- |
|
72 |
+ public void discoverNewDevices() { |
|
73 |
+ // Register for broadcasts when a device is discovered |
|
74 |
+ IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); |
|
75 |
+ context.registerReceiver(mFindBlueToothReceiver, filter); |
|
76 |
+ // Register for broadcasts when discovery has finished |
|
77 |
+ filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); |
|
78 |
+ context.registerReceiver(mFindBlueToothReceiver, filter); |
|
79 |
+ bluetoothAdapter.startDiscovery(); |
|
52 | 80 |
} |
53 | 81 |
|
54 | 82 |
@Override |
55 |
- public void queryPairedDevices() { |
|
83 |
+ public void connectPrinter() { |
|
56 | 84 |
|
57 | 85 |
} |
58 | 86 |
|
59 | 87 |
@Override |
60 |
- public void discoverNewDevices() { |
|
61 |
- |
|
88 |
+ public void cancelDiscovery() { |
|
89 |
+ try{ |
|
90 |
+ bluetoothAdapter.cancelDiscovery(); |
|
91 |
+ context.unregisterReceiver(mFindBlueToothReceiver); |
|
92 |
+ }catch (Exception e){ |
|
93 |
+ e.printStackTrace(); |
|
94 |
+ } |
|
62 | 95 |
} |
63 | 96 |
|
64 |
- @Override |
|
65 |
- public void connectPrinter() { |
|
97 |
+ // changes the title when discovery is finished |
|
98 |
+ private final BroadcastReceiver mFindBlueToothReceiver = new BroadcastReceiver() { |
|
99 |
+ @Override |
|
100 |
+ public void onReceive(Context context, Intent intent) { |
|
101 |
+ String action = intent.getAction(); |
|
102 |
+ // When discovery finds a device |
|
103 |
+ if (BluetoothDevice.ACTION_FOUND.equals(action)) { |
|
104 |
+ // Get the BluetoothDevice object from the Intent |
|
105 |
+ BluetoothDevice device = intent |
|
106 |
+ .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
|
107 |
+ // If it's already paired, skip it, because it's been listed |
|
108 |
+ // already |
|
109 |
+ if (device.getBondState() != BluetoothDevice.BOND_BONDED) { |
|
110 |
+ view.onNewDeviceDiscovered(device); |
|
111 |
+ } |
|
112 |
+ // When discovery is finished, change the Activity title |
|
113 |
+ } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED |
|
114 |
+ .equals(action)) { |
|
115 |
+ view.showToast(context.getString(R.string.bt_discover_complete)); |
|
116 |
+ } |
|
117 |
+ } |
|
118 |
+ }; |
|
66 | 119 |
|
67 |
- } |
|
68 | 120 |
} |
@@ -165,7 +165,7 @@ |
||
165 | 165 |
</LinearLayout> |
166 | 166 |
|
167 | 167 |
<ListView |
168 |
- android:id="@+id/list_bt_devices" |
|
168 |
+ android:id="@+id/listview_bt_devices" |
|
169 | 169 |
android:layout_width="match_parent" |
170 | 170 |
android:layout_height="match_parent" |
171 | 171 |
android:background="@color/white" |
@@ -1,23 +1,38 @@ |
||
1 | 1 |
<?xml version="1.0" encoding="utf-8"?> |
2 | 2 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 |
- android:orientation="vertical" android:layout_width="match_parent" |
|
3 |
+ android:layout_width="match_parent" |
|
4 | 4 |
android:layout_height="wrap_content" |
5 |
- android:paddingTop="4dp" |
|
6 |
- android:paddingBottom="4dp"> |
|
5 |
+ android:orientation="horizontal" |
|
6 |
+ android:paddingBottom="4dp" |
|
7 |
+ android:paddingTop="4dp"> |
|
7 | 8 |
|
8 |
- <TextView |
|
9 |
- android:id="@+id/tv_device_name" |
|
10 |
- android:layout_width="wrap_content" |
|
9 |
+ <LinearLayout |
|
10 |
+ android:layout_width="0dp" |
|
11 | 11 |
android:layout_height="wrap_content" |
12 |
- android:textSize="14sp" |
|
13 |
- android:textColor="@color/dark_grey"/> |
|
12 |
+ android:layout_gravity="center_vertical" |
|
13 |
+ android:layout_weight="1"> |
|
14 |
+ |
|
15 |
+ <TextView |
|
16 |
+ android:id="@+id/tv_device_name" |
|
17 |
+ android:layout_width="wrap_content" |
|
18 |
+ android:layout_height="wrap_content" |
|
19 |
+ android:textColor="@color/dark_grey" |
|
20 |
+ android:textSize="14sp" /> |
|
21 |
+ |
|
22 |
+ <TextView |
|
23 |
+ android:id="@+id/tv_device_mac" |
|
24 |
+ android:layout_width="wrap_content" |
|
25 |
+ android:layout_height="wrap_content" |
|
26 |
+ android:paddingTop="3dp" |
|
27 |
+ android:textColor="@color/grey" |
|
28 |
+ android:textSize="12sp" /> |
|
29 |
+ </LinearLayout> |
|
14 | 30 |
|
15 | 31 |
<TextView |
16 |
- android:id="@+id/tv_device_mac" |
|
32 |
+ android:id="@+id/tv_device_status" |
|
17 | 33 |
android:layout_width="wrap_content" |
18 | 34 |
android:layout_height="wrap_content" |
19 |
- android:paddingTop="3dp" |
|
20 |
- android:textSize="12sp" |
|
21 |
- android:textColor="@color/grey"/> |
|
22 |
- |
|
35 |
+ android:layout_gravity="center_vertical" |
|
36 |
+ android:textColor="@color/grey" |
|
37 |
+ android:textSize="14sp" /> |
|
23 | 38 |
</LinearLayout> |
@@ -69,7 +69,7 @@ |
||
69 | 69 |
|
70 | 70 |
<string name="printer_status">打印机状态</string> |
71 | 71 |
|
72 |
- <string name="query_processing">正在查询</string> |
|
72 |
+ <string name="query_processing">正在查询打印机状态</string> |
|
73 | 73 |
|
74 | 74 |
<string name="print_test">打印测试页</string> |
75 | 75 |
|
@@ -85,6 +85,8 @@ |
||
85 | 85 |
|
86 | 86 |
<string name="paired_devices">已配对设备</string> |
87 | 87 |
|
88 |
+ <string name="bt_discover_complete">蓝牙设备搜索完成</string> |
|
89 |
+ |
|
88 | 90 |
<string name="new_usable_devices">新可用设备</string> |
89 | 91 |
|
90 | 92 |
<string name="printer_is_ok">打印机工作正常</string> |