|
|
@ -164,16 +164,17 @@ public class Bluetooth {
|
|
|
|
|
|
|
|
|
|
|
|
//搜索设备
|
|
|
|
//搜索设备
|
|
|
|
public void SearchingBTDevice() {
|
|
|
|
public void SearchingBTDevice() {
|
|
|
|
|
|
|
|
|
|
|
|
new CheckTypesTask().execute();
|
|
|
|
new CheckTypesTask().execute();
|
|
|
|
System.out.println("开始搜索");
|
|
|
|
System.out.println("开始搜索");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int btConn(final String address, Context context) throws Exception {
|
|
|
|
public void btConn(final String address, Context context) throws Exception {
|
|
|
|
// bluetoothPort.connect(address);
|
|
|
|
|
|
|
|
//final int[] portOpen = {};
|
|
|
|
System.out.println("btConn");
|
|
|
|
final int[] portOpen = new int[1];
|
|
|
|
|
|
|
|
btThread = new Thread(new Runnable() {
|
|
|
|
btThread = new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -181,9 +182,11 @@ public class Bluetooth {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
System.out.println(address);
|
|
|
|
System.out.println(address);
|
|
|
|
portOpen[0] = PrinterHelper.portOpenBT(context,address);
|
|
|
|
final int result = PrinterHelper.portOpenBT(context,address);
|
|
|
|
PrinterHelper.logcat("portOpen:"+ portOpen[0]);
|
|
|
|
PrinterHelper.logcat("portOpen result:"+ result);
|
|
|
|
System.out.println(portOpen[0]);
|
|
|
|
System.out.println(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
@ -193,8 +196,6 @@ public class Bluetooth {
|
|
|
|
btThread.start();
|
|
|
|
btThread.start();
|
|
|
|
//final int result = PrinterHelper.portOpenBT(context,address);
|
|
|
|
//final int result = PrinterHelper.portOpenBT(context,address);
|
|
|
|
//new connBT().execute(address);
|
|
|
|
//new connBT().execute(address);
|
|
|
|
|
|
|
|
|
|
|
|
return portOpen[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//断开连接
|
|
|
|
//断开连接
|
|
|
@ -202,9 +203,15 @@ public class Bluetooth {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// bluetoothPort.disconnect();
|
|
|
|
// bluetoothPort.disconnect();
|
|
|
|
PrinterHelper.portClose();
|
|
|
|
PrinterHelper.portClose();
|
|
|
|
if ((btThread != null) && (btThread.isAlive()))
|
|
|
|
// if ((btThread != null) && (btThread.isAlive()))
|
|
|
|
btThread.interrupt();
|
|
|
|
// btThread.interrupt();
|
|
|
|
|
|
|
|
if ((btThread != null) && (btThread.isAlive())) {
|
|
|
|
|
|
|
|
Thread dummy = btThread;
|
|
|
|
|
|
|
|
btThread = null;
|
|
|
|
|
|
|
|
dummy.interrupt();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
disconnectflags = true;
|
|
|
|
disconnectflags = true;
|
|
|
|
context.unregisterReceiver(connectDevice);
|
|
|
|
context.unregisterReceiver(connectDevice);
|
|
|
|
|
|
|
|
|
|
|
@ -271,53 +278,45 @@ public class Bluetooth {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class connBT extends AsyncTask<String, Void, Integer> {
|
|
|
|
// class connBT extends AsyncTask<String, Void, Integer> {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
@Override
|
|
|
|
// @Override
|
|
|
|
protected Integer doInBackground(String... strings) {
|
|
|
|
// protected Integer doInBackground(String... strings) {
|
|
|
|
Integer retVal = null;
|
|
|
|
// Integer retVal = null;
|
|
|
|
|
|
|
|
//
|
|
|
|
//bluetoothPort.connect(strings[0]);
|
|
|
|
// //bluetoothPort.connect(strings[0]);
|
|
|
|
|
|
|
|
//
|
|
|
|
retVal = Integer.valueOf(0);
|
|
|
|
// retVal = Integer.valueOf(0);
|
|
|
|
|
|
|
|
//
|
|
|
|
return retVal;
|
|
|
|
// return retVal;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
@Override
|
|
|
|
// @Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
// protected void onPreExecute() {
|
|
|
|
|
|
|
|
//
|
|
|
|
super.onPreExecute();
|
|
|
|
// super.onPreExecute();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
@Override
|
|
|
|
// @Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
// protected void onPostExecute(Integer result) {
|
|
|
|
if (result.intValue() == 0) // Connection success.
|
|
|
|
// if (result.intValue() == 0) // Connection success.
|
|
|
|
{
|
|
|
|
// {
|
|
|
|
// RequestHandler rh = new RequestHandler();
|
|
|
|
//// RequestHandler rh = new RequestHandler();
|
|
|
|
// btThread = new Thread(rh);
|
|
|
|
//// btThread = new Thread(rh);
|
|
|
|
// btThread.start();
|
|
|
|
//// btThread.start();
|
|
|
|
} else // Connection failed.
|
|
|
|
// } else // Connection failed.
|
|
|
|
{
|
|
|
|
// {
|
|
|
|
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
super.onPostExecute(result);
|
|
|
|
// super.onPostExecute(result);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
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> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected Void doInBackground(Context... contexts) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DisconnectDevice(contexts[0]);
|
|
|
|
DisconnectDevice(context);
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
if (disconnectflags)
|
|
|
|
if (disconnectflags)
|
|
|
@ -329,24 +328,45 @@ public class Bluetooth {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onPreExecute() {
|
|
|
|
|
|
|
|
super.onPreExecute();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
|
|
|
|
disconnectflags = false;
|
|
|
|
|
|
|
|
super.onPostExecute(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private class ExcuteDisconnectBT extends AsyncTask<Context, Void, Void> {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
|
|
// protected Void doInBackground(Context... contexts) {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// DisconnectDevice(contexts[0]);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// while (true) {
|
|
|
|
|
|
|
|
// if (disconnectflags)
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Thread.sleep(100);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } catch (InterruptedException e) {
|
|
|
|
|
|
|
|
// // TODO Auto-generated catch block
|
|
|
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return null;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
|
|
// protected void onPreExecute() {
|
|
|
|
|
|
|
|
// super.onPreExecute();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// ;
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
|
|
// protected void onPostExecute(Void result) {
|
|
|
|
|
|
|
|
// disconnectflags = false;
|
|
|
|
|
|
|
|
// super.onPostExecute(result);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// ;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|