cL97">97
@Override
- public void onPairedDeviceDiscovered() {
+ public void onPairedDeviceDiscovered(List<BluetoothDevice> devices) {
+ pairedDeviceAdapter.setDeviceList((ArrayList<BluetoothDevice>)devices);
+ }
+
+ @Override
+ public void showToast(String msg) {
+ Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.tv_print_test)
void testPrint(){
presenter.printTestPage();
}
+
+ @OnCheckedChanged(R.id.tb_bluetooth_switch)
+ void switchBluetooth(){
+ if(!presenter.queryBluetoothStatus()){
+ Intent enableIntent = new Intent(
+ BluetoothAdapter.ACTION_REQUEST_ENABLE);
+ startActivityForResult(enableIntent,
+ REQUEST_ENABLE_BT);
+ }else{
+ BluetoothAdapter.getDefaultAdapter().disable();
+ onBluetoothDisabled();
+ }
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+ if(resultCode == Activity.RESULT_OK){
+ if(requestCode == REQUEST_ENABLE_BT){
+ onBluetoothEnabled();
+ onPairedDeviceDiscovered(presenter.queryPairedDevices());
+ presenter.discoverNewDevices();
+ presenter.queryPrinterStatus();
+ }else if(requestCode == REQUEST_CONNECT_DEVICE){
+
+ }
+ }
+ }
+
}
@@ -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> |