disconnected return

master
张萌 3 years ago
parent 4ba42981ac
commit a642670f57

@ -3,6 +3,8 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="2f8e3712-04e2-429a-bcea-c8db1a54008e" name="默认变更列表" comment=""> <list default="true" id="2f8e3712-04e2-429a-bcea-c8db1a54008e" name="默认变更列表" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/android/src/main/java/com/dj/printer/dj_printer/DjPrinterPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/com/dj/printer/dj_printer/DjPrinterPlugin.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/pubspec.lock" beforeDir="false" afterPath="$PROJECT_DIR$/example/pubspec.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/dj_printer.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/dj_printer.dart" afterDir="false" /> <change beforePath="$PROJECT_DIR$/lib/dj_printer.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/dj_printer.dart" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />

@ -41,7 +41,7 @@ public class Blutooth {
bluetoothPort.SetMacFilter(false); bluetoothPort.SetMacFilter(false);
bluetoothSetup(); bluetoothSetup();
searchflags = false; searchflags = false;
disconnectflags=false; disconnectflags = false;
System.out.println("初始化完成"); System.out.println("初始化完成");
} }
@ -57,12 +57,15 @@ public class Blutooth {
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
try { try {
if (bluetoothPort.isConnected()) { if (bluetoothPort.isConnected()) {
System.out.println("1111s");
bluetoothPort.disconnect(); bluetoothPort.disconnect();
if ((btThread != null) && (btThread.isAlive())) {
cancelThread();
}
eventSink.success("disconnected");
} }
if ((btThread != null) && (btThread.isAlive())) {
cancelThread();
}
System.out.println("disconnected");
eventSink.success("disconnected");
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -160,7 +163,7 @@ public class Blutooth {
try { try {
bluetoothPort.disconnect(); bluetoothPort.disconnect();
if((btThread != null) && (btThread.isAlive())) if ((btThread != null) && (btThread.isAlive()))
btThread.interrupt(); btThread.interrupt();
disconnectflags = true; disconnectflags = true;
@ -177,8 +180,11 @@ public class Blutooth {
//取消搜索 //取消搜索
public void cancelDiscoveryResult(Context context) { public void cancelDiscoveryResult(Context context) {
System.out.println("取消搜索");
mBluetoothAdapter.cancelDiscovery(); mBluetoothAdapter.cancelDiscovery();
context.unregisterReceiver(discoveryResult); context.unregisterReceiver(discoveryResult);
context.unregisterReceiver(searchStart);
context.unregisterReceiver(searchFinish);
} }
@ -263,12 +269,11 @@ public class Blutooth {
} }
} }
public void ExcuteDisconnect(Context context) public void ExcuteDisconnect(Context context) {
{
new ExcuteDisconnectBT().execute(context); new ExcuteDisconnectBT().execute(context);
} }
private class ExcuteDisconnectBT extends AsyncTask<Context, Void, Void>{ private class ExcuteDisconnectBT extends AsyncTask<Context, Void, Void> {
@Override @Override
@ -276,9 +281,8 @@ public class Blutooth {
try { try {
DisconnectDevice(contexts[0]); DisconnectDevice(contexts[0]);
while(true) while (true) {
{ if (disconnectflags)
if(disconnectflags)
break; break;
Thread.sleep(100); Thread.sleep(100);
@ -291,16 +295,20 @@ public class Blutooth {
} }
@Override @Override
protected void onPreExecute(){ protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
}; }
;
@Override @Override
protected void onPostExecute(Void result){ protected void onPostExecute(Void result) {
disconnectflags = false; disconnectflags = false;
super.onPostExecute(result); super.onPostExecute(result);
}; }
;
} }
} }

@ -99,8 +99,12 @@ public class DjPrinterPlugin implements FlutterPlugin, MethodCallHandler {
} else if (call.method.equals("getStatus")) { } else if (call.method.equals("getStatus")) {
int sta = asPrint.Get_Status(); int sta = asPrint.Get_Status();
result.success(sta); result.success(sta);
} else { } else if (call.method.equals("disposeDiscovery")) {
result.notImplemented(); blutooth.cancelDiscoveryResult(context);
result.success(true);
} else if (call.method.equals("disposeConnect")) {
blutooth.ExcuteDisconnect(context);
result.success(true);
} }
} }

@ -102,13 +102,6 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.12.11" version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.2"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -211,7 +204,7 @@ packages:
name: test_api name: test_api
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.4.8" version: "0.4.3"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:

@ -11,13 +11,13 @@ class DjPrinter {
factory DjPrinter() => _instance; factory DjPrinter() => _instance;
static const MethodChannel _channel = MethodChannel('dj_printer'); static const MethodChannel _channel = MethodChannel('dj_printer');
static const EventChannel _deviceChannel = static const EventChannel _deviceChannel =
EventChannel("com.discovery.devices"); EventChannel("com.discovery.devices");
StreamSubscription? _discoveryStream; StreamSubscription? _discoveryStream;
StreamSubscription addDiscoveryListen( StreamSubscription addDiscoveryListen(
{required void Function(dynamic data) onReceive, {required void Function(dynamic data) onReceive,
void Function()? onStart, void Function()? onStart,
void Function()? onFinish}) { void Function()? onFinish}) {
if (_discoveryStream == null) { if (_discoveryStream == null) {
return _deviceChannel.receiveBroadcastStream().listen((data) { return _deviceChannel.receiveBroadcastStream().listen((data) {
if (data == "start" && onStart != null) { if (data == "start" && onStart != null) {
@ -34,18 +34,17 @@ class DjPrinter {
} }
void cancelDiscovery() { void cancelDiscovery() {
if (_discoveryStream != null) { _discoveryStream?.cancel();
_discoveryStream!.cancel(); _discoveryStream = null;
_discoveryStream = null; print('结束搜索');
} disposeDiscovery();
} }
static const EventChannel _connectChannel = EventChannel("com.connect"); static const EventChannel _connectChannel = EventChannel("com.connect");
StreamSubscription? _connectStream; StreamSubscription? _connectStream;
StreamSubscription addConnectListen( StreamSubscription addConnectListen({required void Function() onConnect,
{required void Function() onConnect, required void Function() onDisconnect}) {
required void Function() onDisconnect}) {
if (_connectStream == null) { if (_connectStream == null) {
return _connectChannel.receiveBroadcastStream().listen((data) { return _connectChannel.receiveBroadcastStream().listen((data) {
if (data == 'connected') { if (data == 'connected') {
@ -66,9 +65,8 @@ class DjPrinter {
} }
} }
Future<bool?> get startSearch async { void get startSearch {
final res = await _channel.invokeMethod('startSearch'); final res = _channel.invokeMethod('startSearch');
return res;
} }
Future<bool?> connect(String address) async { Future<bool?> connect(String address) async {
@ -76,6 +74,16 @@ class DjPrinter {
return res; return res;
} }
void disposeDiscovery() {
print('disposeDiscovery');
final res = _channel.invokeMethod('disposeDiscovery');
}
Future<bool?> disposeConnect() async {
final res = await _channel.invokeMethod('disposeConnect');
return res;
}
Future<bool?> init() async { Future<bool?> init() async {
final res = await _channel.invokeMethod('init'); final res = await _channel.invokeMethod('init');
return res; return res;
@ -104,13 +112,12 @@ class DjPrinter {
} }
} }
Future<bool?> print( Future<bool?> printAScode({required String code,
{required String code, required String channel,
required String channel, required String country,
required String country, required String countStr,
required String countStr, required int offset,
required int offset, required bool hasPlan}) async {
required bool hasPlan}) async {
final res = await _channel.invokeMethod('print', { final res = await _channel.invokeMethod('print', {
'code': code, 'code': code,
'channel': channel, 'channel': channel,

Loading…
Cancel
Save