| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- package com.uas.hystorage.activity;
- import android.bluetooth.BluetoothDevice;
- import android.content.Intent;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.chad.library.adapter.base.BaseQuickAdapter;
- import com.chad.library.adapter.base.BaseViewHolder;
- import com.uas.bluetooth.ConnectHelper;
- import com.uas.hystorage.R;
- import com.uas.hystorage.adapter.BluetoothAdapter;
- import com.uas.hystorage.tools.SharedPreUtil;
- import com.uas.hystorage.util.CommonUtil;
- import com.uas.hystorage.util.Constants;
- import com.uas.hystorage.util.FastjsonUtil;
- import com.uas.hystorage.util.ListUtils;
- import com.uas.hystorage.util.PrintUtils;
- import com.uas.hystorage.view.RecyclerItemDecoration;
- import com.yanzhenjie.permission.Action;
- import com.yanzhenjie.permission.AndPermission;
- import com.yanzhenjie.permission.Permission;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import vn.luongvo.widget.iosswitchview.SwitchView;
- public class BlueToothPrintAct extends BaseActivity {
- public static final int REQUEST_ENABLE_BT = 1;//开启蓝牙的请求回调
- private ImageView mRefreshImageView;
- private SwitchView mSwitchView;
- private TextView mDpiTextView;
- private RecyclerView mRecyclerView;
- private ConnectHelper connectHelper;
- private RotateAnimation mRotateAnimation;//动画
- private BluetoothAdapter mBluetoothAdapter;
- private boolean isConnected = false;
- private String mOldAddress = "";
- private List<Integer> mDpiList;
- private View mMenuView;
- private PopupWindow mMenuPopupWindow;
- private RecyclerView mMenuRecyclerView;
- private BaseQuickAdapter<Integer, BaseViewHolder> mMenuAdapter;
- private int mDpi;
- private String mPrintJson;
- private int mRetryCount = 0;
- private boolean isPrinting = false;
- private Button bt_return;
- private TextView tv_title;
- private ImageView iv_icon;
- private TextView tv_type;
- @Override
- protected void initViews() {
- setContentView(R.layout.act_blue_tooth_print);
- bt_return = findViewById(R.id.bt_return);
- tv_title = findViewById(R.id.tv_title);
- iv_icon = findViewById(R.id.iv_icon);
- tv_type = findViewById(R.id.tv_type);
- tv_title.setText("打印机连接");
- tv_type.setVisibility(View.VISIBLE);
- mDpi = SharedPreUtil.getInt(BlueToothPrintAct.this, "printDpi", 203);
- tv_type.setText(mDpi + "dpi");
- mPrintJson = getIntent().getStringExtra(Constants.KEY.BARCODE_PRINT_ARRAY);
- mRefreshImageView = findViewById(R.id.bluetooth_print_refresh_iv);
- mSwitchView = findViewById(R.id.bluetooth_print_sv);
- mRecyclerView = findViewById(R.id.bluetooth_print_rv);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(BlueToothPrintAct.this));
- mRecyclerView.addItemDecoration(new RecyclerItemDecoration(1));
- mDpiList = new ArrayList<>();
- isConnected = (SharedPreUtil.getInt(BlueToothPrintAct.this, "printConnect", 0) == 1);
- mOldAddress = SharedPreUtil.getString(BlueToothPrintAct.this, "printAddress", "");
- }
- @Override
- protected void initEvents() {
- bt_return.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- mRefreshImageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (connectHelper.isOpenBluetooth()) {
- mRefreshImageView.setAnimation(getImgAnimation());
- mRefreshImageView.startAnimation(getImgAnimation());
- connectHelper.searchDevices();
- }
- }
- });
- mDpiList.add(152);
- mDpiList.add(203);
- mDpiList.add(300);
- mDpiList.add(600);
- tv_type.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (mMenuView == null) {
- mMenuView = View.inflate(BlueToothPrintAct.this, R.layout.pop_common_list_menu, null);
- }
- if (mMenuRecyclerView == null) {
- mMenuRecyclerView = mMenuView.findViewById(R.id.common_list_menu_rv);
- mMenuRecyclerView.setLayoutManager(new LinearLayoutManager(BlueToothPrintAct.this));
- mMenuRecyclerView.addItemDecoration(new RecyclerItemDecoration(1));
- }
- if (mMenuAdapter == null) {
- mMenuAdapter = new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.item_common_list_menu, mDpiList) {
- @Override
- protected void convert(BaseViewHolder helper, Integer item) {
- helper.setText(R.id.item_common_list_menu_btn, item + "dpi");
- }
- };
- mMenuRecyclerView.setAdapter(mMenuAdapter);
- mMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
- @Override
- public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
- closeMenuPopupWindow();
- mDpi = mDpiList.get(position);
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printDpi", mDpi);
- tv_type.setText(mDpi + "dpi");
- }
- });
- }
- if (mMenuPopupWindow == null) {
- mMenuPopupWindow = new PopupWindow(mMenuView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
- mMenuPopupWindow.setBackgroundDrawable(new BitmapDrawable());
- mMenuPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
- @Override
- public void onDismiss() {
- closeMenuPopupWindow();
- }
- });
- }
- CommonUtil.setBackgroundAlpha(BlueToothPrintAct.this, 0.5f);
- mMenuPopupWindow.showAsDropDown(tv_type);
- }
- });
- }
- @Override
- protected void initDatas() {
- if (Build.VERSION.SDK_INT >= 6.0) {
- try {
- AndPermission.with(BlueToothPrintAct.this)
- .runtime()
- .permission(Permission.Group.LOCATION)
- .onGranted(new Action<List<String>>() {
- @Override
- public void onAction(List<String> data) {
- initHelper();
- }
- })
- .start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- initHelper();
- }
- }
- private void closeMenuPopupWindow() {
- if (mMenuPopupWindow != null) {
- mMenuPopupWindow.dismiss();
- mMenuPopupWindow = null;
- CommonUtil.setBackgroundAlpha(BlueToothPrintAct.this, 1f);
- }
- }
- private void initHelper() {
- connectHelper = new ConnectHelper(BlueToothPrintAct.this);
- connectHelper.setConnectListener(new ConnectHelper.ConnectListener() {
- @Override
- public void onDeviceChange(int state, BluetoothDevice device) {
- try {
- List<BluetoothDevice> models = mBluetoothAdapter.getModels();
- boolean hasConflict = false;
- if (!ListUtils.isEmpty(models)) {
- for (int i = 0; i < models.size(); i++) {
- BluetoothDevice model = models.get(i);
- if (model.getAddress().equals(device.getAddress())) {
- if (model.getBondState() != device.getBondState()) {
- models.remove(i);
- models.add(0, device);
- }
- hasConflict = true;
- break;
- }
- }
- }
- if (!hasConflict) {
- if (models == null) {
- models = new ArrayList<>();
- }
- if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
- models.add(0, device);
- } else {
- models.add(device);
- }
- }
- setAdapter(models);
- } catch (Exception e) {
- e.printStackTrace();
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "蓝牙设备搜索异常,请重新搜索");
- }
- }
- @Override
- public void bindResult(int portOpen, BluetoothDevice device) {
- progressDialog.dismiss();
- if (portOpen == -11) {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印机连接异常");
- isConnected = false;
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printConnect", 0);
- } else if (portOpen == -1) {
- mRetryCount++;
- if (mRetryCount > 3) {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印机连接失败,请检查打印机是否启动");
- isConnected = false;
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printConnect", 0);
- mRetryCount = 0;
- } else {
- connectHelper.toConnectDeviceHY(device);
- }
- } else {
- // TODO: 2018/12/24 执行打印操作
- mRetryCount = 0;
- isConnected = true;
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printConnect", 1);
- mOldAddress = device.getAddress();
- SharedPreUtil.saveString(BlueToothPrintAct.this, "printAddress", mOldAddress);
- printEvent();
- }
- }
- @Override
- public void onFinished() {
- mRefreshImageView.setAnimation(null);
- }
- });
- Set<BluetoothDevice> devices = connectHelper.getBondedDevices();
- List<BluetoothDevice> models = new ArrayList<>();
- if (!ListUtils.isEmptyBlue(devices)) {
- for (BluetoothDevice device : devices) {
- models.add(device);
- }
- }
- setAdapter(models);
- mSwitchView.setChecked(connectHelper.isOpenBluetooth());
- mSwitchView.setOnCheckedChangeListener(mOnCheckedChangeListener);
- }
- private void printEvent() {
- if (isPrinting) {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "正在打印中,请勿重复操作");
- return;
- }
- isPrinting = true;
- if (FastjsonUtil.validate(mPrintJson)) {
- JSONArray printArray = JSON.parseArray(mPrintJson);
- if (printArray != null && printArray.size() > 0) {
- boolean isFail = false;
- for (int i = 0; i < printArray.size(); i++) {
- JSONObject printObject = printArray.getJSONObject(i);
- int printResult = PrintUtils.printBar(BlueToothPrintAct.this, printObject.toJSONString(), mDpi, null);
- if (printResult <= 0) {
- isFail = true;
- }
- }
- if (isFail) {
- isConnected = false;
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printConnect", 0);
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印失败,请确定打印机是否正常");
- } else {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印成功");
- getSupportFragmentManager().popBackStack();
- }
- } else {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印失败,条码数据为空");
- }
- } else {
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "打印失败,条码数据为空");
- }
- isPrinting = false;
- }
- private Animation getImgAnimation() {
- if (mRotateAnimation == null) {
- mRotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- LinearInterpolator lin = new LinearInterpolator();
- mRotateAnimation.setInterpolator(lin);
- mRotateAnimation.setDuration(1000);//设置动画持续时间
- mRotateAnimation.setRepeatCount(-1);//设置重复次数
- mRotateAnimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
- mRotateAnimation.setStartOffset(10);//执行前的等待时间
- }
- return mRotateAnimation;
- }
- private void setAdapter(List<BluetoothDevice> models) {
- List<BluetoothDevice> printDevices = new ArrayList<>();
- if (!ListUtils.isEmpty(models)) {
- for (int i = 0; i < models.size(); i++) {
- BluetoothDevice bluetoothDevice = models.get(i);
- if (bluetoothDevice != null && bluetoothDevice.getBluetoothClass().getDeviceClass() == ConnectHelper.PRINT_TYPE) {
- printDevices.add(bluetoothDevice);
- }
- }
- }
- if (mBluetoothAdapter == null) {
- mBluetoothAdapter = new BluetoothAdapter(BlueToothPrintAct.this, printDevices);
- mBluetoothAdapter.setOnSendListener(mOnSendListener);
- mRecyclerView.setAdapter(mBluetoothAdapter);
- } else {
- mBluetoothAdapter.setModels(printDevices);
- }
- }
- private BluetoothAdapter.OnSendListener mOnSendListener = new BluetoothAdapter.OnSendListener() {
- @Override
- public void send(final BluetoothDevice device) {
- //TODO 对指定设备发送消息(必须先连接)
- /*AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
- builder.setTitle("选择连接类型");
- final String[] str = {"ESP机型", "汉印打印机"};
- builder.setSingleChoiceItems(str, -1, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //客户点击后,对话框记得消失
- dialog.dismiss();
- if (which == 0) {
- //ESC机型
- boolean bindOk = connectHelper.toConnectDevice(device);
- if (bindOk) {
- }
- } else {
- //汉印打印机
- connectHelper.toConnectDeviceHY(device);
- }
- }
- }
- );
- builder.show();*/
- String newAddress = device.getAddress();
- if (!TextUtils.isEmpty(newAddress) && newAddress.equals(mOldAddress)) {
- if (isConnected) {
- printEvent();
- } else {
- mRetryCount = 0;
- progressDialog.show();
- connectHelper.toConnectDeviceHY(device);
- }
- } else {
- mRetryCount = 0;
- isConnected = false;
- SharedPreUtil.saveInt(BlueToothPrintAct.this, "printConnect", 0);
- progressDialog.show();
- connectHelper.toConnectDeviceHY(device);
- }
- }
- @Override
- public void itemClick(BluetoothDevice device) {
- connectHelper.toConnectDevice(device);
- }
- };
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == REQUEST_ENABLE_BT) {
- mSwitchView.setOnCheckedChangeListener(null);
- if (resultCode == RESULT_OK) {
- mSwitchView.setChecked(true);
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "蓝牙开启成功");
- } else {
- mSwitchView.setChecked(false);
- CommonUtil.toastNoRepeat(BlueToothPrintAct.this, "蓝牙开启失败");
- }
- mSwitchView.setOnCheckedChangeListener(mOnCheckedChangeListener);
- }
- }
- private SwitchView.OnCheckedChangeListener mOnCheckedChangeListener = new SwitchView.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(SwitchView switchView, boolean b) {
- if (b) {
- connectHelper.openBluetooth(REQUEST_ENABLE_BT);
- } else {
- connectHelper.closeBluetooth();
- }
- }
- };
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (connectHelper != null) {
- connectHelper.onDestroy();
- }
- }
- }
|