package com.printer.demo;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.media.MediaPlayer;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.print.demo.loghelper.LogcatHelper;
import com.printer.demo.global.GlobalContants;
import com.printer.demo.utils.BluetoothAddressUtil;
import com.printer.demo.utils.BondUtils;
import com.printer.demo.utils.PrefUtils;
import com.printer.demo.utils.PrintBean;
import com.printer.demo.utils.PrintLabel58;
import com.printer.demo.utils.PrintLabel80_test;
import com.printer.demo.utils.UriUtils;
import com.printer.demo.utils.XTUtils;
import com.printer.sdk.PrinterConstants;
import com.printer.sdk.PrinterConstants.Connect;
import com.printer.sdk.PrinterInstance;
import com.printer.sdk.usb.USBPort;
import com.printer.sdk.utils.Utils;
import com.printer.sdk.utils.XLog;
import com.printer.sdk.utils.recordlog.ZLog;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@SuppressLint("HandlerLeak")
public class SettingActivity extends BaseActivity
        implements OnClickListener, OnItemSelectedListener, OnCheckedChangeListener {
    private LinearLayout header;
    private TextView tvShowPrinterType;
    private TextView tvShowPrinterPortType;
    private Spinner spinner_printer_type;
    private Spinner spinner_interface_type;
    private List<String> data_list;
    private ArrayAdapter<CharSequence> arr_adapter;
    private ArrayAdapter<CharSequence> printType_adapter;
    private final static int SCANNIN_GREQUEST_CODE = 2;
    private final static int SCANNIN_NAME_GREQUEST_CODE = 4;
    public static final int CONNECT_DEVICE = 1;
    public static final int ENABLE_BT = 3;
    // 不同蓝牙链接方式的判断依据 “确认连接”
    public static int connectMains = 0;
    protected static final String TAG = "SettingActivity";
    private static Button btn_search_devices, btn_scan_and_connect, btn_selfprint_test,
            btn_status_test, btn_selfCheckingPage_print, btn_scan_and_connect_name,
            btn_select_file, btn_print_select_file;
    public static boolean isConnected = false;// 蓝牙连接状态
    public static String devicesName = "未知设备";
    private static String devicesAddress;
    private ProgressDialog dialog;
    public static PrinterInstance myPrinter;
    private static BluetoothDevice mDevice;
    private static UsbDevice mUSBDevice;
    private static Context mContext;
    private int printerId = 0;
    private int interfaceType = 0;
    private List<UsbDevice> deviceList;
    private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";
    private RadioGroup rg__select_paper_size;
    boolean isError;
    private BluetoothAdapter mBtAdapter;
    private TextView tv_device_name, tv_printer_address, tv_file_path;
    private IntentFilter bluDisconnectFilter;
    private static boolean hasRegDisconnectReceiver = false;
    private ProgressDialog dialogH;
    private static final String ACTION_UPDATE_CHARACTERS_CANCLE = "canclUpdateCharacters";
    private IntentFilter cancleUpdateCharactersFilter;
    private String strStatus = null;
    private RadioGroup rgPrinterType;
    private RadioGroup rgPrinterLog;
    private String defaultPin = "1234";

    String path = null;
    byte[] sendData = new byte[0];
    ExecutorService executor;
    private String textToSend;
    // USB第一次连接标志位
    private boolean isFirstUsbConnect = true;
    /**
     * 设置TSPL指令打印机
     *
     * @param mPrinter
     * @return 是否设置成功
     */

    /**
     * 显示扫描结果
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
        XLog.i(TAG, "yxz at SetteingActivity.java onCreate() SettingActivity.this:" + SettingActivity.this);
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        Log.e(TAG, "yxz at SetteingActivity.java onCreate()");

    }

    // 用于接受连接状态消息的 Handler
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("ShowToast")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Connect.SUCCESS:
                    isConnected = true;
                    GlobalContants.ISCONNECTED = isConnected;
                    GlobalContants.DEVICENAME = devicesName;
                    // 成功连接后重置第一次连接标志
                    isFirstUsbConnect = true;
                    if (interfaceType == 0) {
                        PrefUtils.setString(mContext, GlobalContants.DEVICEADDRESS, devicesAddress);
                        bluDisconnectFilter = new IntentFilter();
                        bluDisconnectFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
                        mContext.registerReceiver(myReceiver, bluDisconnectFilter);
                        hasRegDisconnectReceiver = true;
                        if (connectMains == 3) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    new PrintLabel58(mContext).doPrint(myPrinter, getResources());
//                                    try {
//                                        Thread.sleep(1000);
//                                    } catch (InterruptedException e) {
//                                        e.printStackTrace();
//                                    }
//                                    myPrinter.closeConnection();
//                                    try {
//                                        BondUtils.removeBond(mDevice.getClass(),mDevice);
//                                    } catch (Exception e) {
//                                        Toast.makeText(mContext,"取消配对异常",Toast.LENGTH_SHORT).show();
//                                        e.printStackTrace();
//                                    }
                                }
                            }).start();
                        }
                    }

                    // TOTO 暂时将TSPL指令设置参数的设置放在这
                    // if (setPrinterTSPL(myPrinter)) {
                    // if (interfaceType == 0) {
                    // Toast.makeText(mContext,
                    // R.string.settingactivitty_toast_bluetooth_set_tspl_successful,
                    // 0)
                    // .show();
                    // } else if (interfaceType == 1) {
                    // Toast.makeText(mContext,
                    // R.string.settingactivity_toast_usb_set_tspl_succefful,
                    // 0).show();
                    // }
                    // }
                    break;
                case Connect.FAILED:
                    isConnected = false;
                    // USB第一次连接失败时，显示提示让用户重新连接
                    if (interfaceType == 1 && isFirstUsbConnect) {
                        Toast.makeText(mContext, mContext.getString(R.string.first_usb_connect_tip), Toast.LENGTH_SHORT).show();
                        isFirstUsbConnect = false;
                    } else {
                        Toast.makeText(mContext, R.string.conn_failed, Toast.LENGTH_SHORT).show();
                    }
                    XLog.i(TAG, "ZL at SettingActivity Handler() 连接失败!");
                    break;
                case Connect.CLOSED:
                    isConnected = false;
                    GlobalContants.ISCONNECTED = isConnected;
                    GlobalContants.DEVICENAME = devicesName;
                    Toast.makeText(mContext, R.string.conn_closed, Toast.LENGTH_SHORT).show();
                    XLog.i(TAG, "ZL at SettingActivity Handler() 连接关闭!");
                    break;
                case Connect.NODEVICE:
                    isConnected = false;
                    Toast.makeText(mContext, R.string.conn_no, Toast.LENGTH_SHORT).show();
                    break;
                case 555:
                    connect2BlueToothdevice();
                    break;
                // case 10:
                // if (setPrinterTSPL(myPrinter)) {
                // Toast.makeText(mContext, "蓝牙连接设置TSPL指令成功", 0).show();
                // }
                default:
                    break;
            }

            updateButtonState(isConnected);

            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }

    };


    int count = 0;

    @SuppressWarnings("static-access")
    public void vibrator() {
        count++;
        PrefUtils.setInt(mContext, "count3", count);
        Log.e(TAG, "" + count);
        // Vibrator vib = (Vibrator) SettingActivity.this
        // .getSystemService(Service.VIBRATOR_SERVICE);
        // vib.vibrate(1000);
        // try {
        // Thread.sleep(500);
        // } catch (InterruptedException e) {
        // e.printStackTrace();
        // }
        MediaPlayer player = new MediaPlayer().create(mContext, R.raw.test);
        // MediaPlayer player2 = new MediaPlayer().create(mContext, R.raw.beep);

        player.start();
        // player2.start();
    }

    /**
     * 初始化界面
     */
    private void init() {
        mContext = SettingActivity.this;
        header = (LinearLayout) findViewById(R.id.ll_headerview_settingactivity);
        // 初始化标题
        initHeader(header);
        // 初始化下拉列表框
        // spinner_printer_type = (Spinner)
        // findViewById(R.id.spinner_printer_type);
        spinner_interface_type = (Spinner) findViewById(R.id.spinner_interface_type);
        // 初始化按钮的点击事件
        btn_search_devices = (Button) findViewById(R.id.btn_search_devices);
        btn_select_file = (Button) findViewById(R.id.btn_select_file);
        btn_scan_and_connect = (Button) findViewById(R.id.btn_scan_and_connect);
        btn_selfprint_test = (Button) findViewById(R.id.btn_selfprint_test);
        btn_status_test = (Button) findViewById(R.id.btn_status_test);
        btn_selfCheckingPage_print = (Button) findViewById(R.id.btn_selfchekingpage_print);
        btn_scan_and_connect_name = (Button) findViewById(R.id.btn_scan_and_connect_name);
        btn_print_select_file = (Button) findViewById(R.id.btn_print_select_file);
        // 设置按钮的监听事件
        btn_search_devices.setOnClickListener(this);
        btn_scan_and_connect.setOnClickListener(this);
        btn_selfprint_test.setOnClickListener(this);
        btn_status_test.setOnClickListener(this);
        btn_selfCheckingPage_print.setOnClickListener(this);
        btn_scan_and_connect_name.setOnClickListener(this);
        btn_select_file.setOnClickListener(this);
        btn_print_select_file.setOnClickListener(this);

        // 展示设备名和设备地址
        tv_device_name = (TextView) findViewById(R.id.tv_device_name);
        tv_printer_address = (TextView) findViewById(R.id.tv_printer_address);
        tv_file_path = (TextView) findViewById(R.id.tv_file_path);
        // // 适配器
        // arr_adapter = ArrayAdapter.createFromResource(this,
        // R.array.printertype, android.R.layout.simple_spinner_item);
        printType_adapter = ArrayAdapter.createFromResource(this, R.array.interface_type,
                android.R.layout.simple_spinner_item);

        // // 设置样式
        // arr_adapter
        // .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        printType_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // 加载适配器
        // spinner_printer_type.setAdapter(arr_adapter);
        spinner_interface_type.setAdapter(printType_adapter);
        // 下拉列表框的监听事件
        // spinner_printer_type.setOnItemSelectedListener(this);
        spinner_interface_type.setOnItemSelectedListener(this);

        rg__select_paper_size = (RadioGroup) findViewById(R.id.rg__select_paper_size);
        rg__select_paper_size.setOnCheckedChangeListener(this);

        rgPrinterType = (RadioGroup) findViewById(R.id.rg_printer_type);
        rgPrinterType.setOnCheckedChangeListener(this);

        rgPrinterLog = (RadioGroup) findViewById(R.id.rg_printer_log);
        rgPrinterLog.setOnCheckedChangeListener(this);
        // 初始化对话框
        dialog = new ProgressDialog(mContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle(getString(R.string.connecting));
        dialog.setMessage(getString(R.string.please_wait));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);

        getSaveState();
        updateButtonState(isConnected);
        // 初始化进度条对话框
        dialogH = new ProgressDialog(this);
        dialogH.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialogH.setCancelable(false);
        dialogH.setCanceledOnTouchOutside(false);

        PrinterConstants.paperWidth = PrefUtils.getInt(mContext, GlobalContants.PAPERWIDTH, 576);
        switch (PrinterConstants.paperWidth) {
            case 384:
                rg__select_paper_size.check(R.id.rb_58mm);
                break;
            case 576:
                rg__select_paper_size.check(R.id.rb_80mm);
                break;
            case 724:
                rg__select_paper_size.check(R.id.rb_100mm);
                break;
            default:
                rg__select_paper_size.check(R.id.rb_80mm);
                break;
        }
        switch (PrefUtils.getInt(mContext, GlobalContants.PRINTERTYPE, 0)) {
            case 0:
                rgPrinterType.check(R.id.rb_normal);
                break;
            case 1:
                rgPrinterType.check(R.id.rb_t7);
                break;
            default:
                rgPrinterType.check(R.id.rb_normal);
                break;
        }
        switch (PrefUtils.getInt(mContext, GlobalContants.PRINTERLOG, 0)) {
            case 0:
                rgPrinterLog.check(R.id.rb_log_open);
                break;
            case 1:
                rgPrinterLog.check(R.id.rb_log_close);
                break;
            default:
                rgPrinterLog.check(R.id.rb_log_open);
                break;
        }

        // 获取当前设备的CPU核心数
        int cpuCount = Runtime.getRuntime().availableProcessors();
        XLog.d("yxz", "cpuCount:" + cpuCount);
        // 创建一个线程池，包含与CPU核心数相同的线程
        executor = Executors.newScheduledThreadPool(cpuCount);
    }

    private void getSaveState() {
        XLog.d(TAG, "yxz at SettingActivity.java getSaveState() ---begin");
        isConnected = PrefUtils.getBoolean(SettingActivity.this, GlobalContants.CONNECTSTATE, false);
        printerId = PrefUtils.getInt(mContext, GlobalContants.PRINTERID, 0);
        interfaceType = PrefUtils.getInt(mContext, GlobalContants.INTERFACETYPE, 0);
        // spinner_printer_type.setSelection(printerId);
        spinner_interface_type.setSelection(interfaceType);
        XLog.i(TAG, "zl at MainActivity.java isConnected:" + isConnected);
        XLog.d(TAG, "yxz at SettingActivity.java getSaveState() ---end");

    }

    /**
     * 初始化标题上的信息
     */
    private void initHeader(LinearLayout header) {
        setHeaderLeftText(header, getString(R.string.back), new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
        headerConnecedState.setText(getTitleState());
        setHeaderLeftImage(header, new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setHeaderCenterText(header, getString(R.string.headview_setting));

    }

    @Override
    protected void onStart() {
        XLog.e(TAG, "yxz at SettingActivity.java onStart()   progressdialog");
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        XLog.e(TAG, "yxz at SettingActivity.java onResume()   progressdialog");
        XLog.e(TAG, "yxz at SettingActivity.java onResume()   liukong:" + PrefUtils.getInt(mContext, GlobalContants.LiuKongType, 1));

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        XLog.e(TAG, "yxz at SettingActivity.java onRestart()   progressdialog");
    }

    @Override
    protected void onPause() {
        super.onPause();
        XLog.e(TAG, "yxz at SettingActivity.java onPause()   progressdialog");
    }

    @Override
    protected void onStop() {
        super.onStop();
        XLog.e(TAG, "yxz at SettingActivity.java onStop()   progressdialog");

    }

    @Override
    protected void onDestroy() {
        XLog.e(TAG, "yxz at SettingActivity.java onDestroy()   progressdialog");
        super.onDestroy();
        if (interfaceType == 0 && hasRegDisconnectReceiver) {
            mContext.unregisterReceiver(myReceiver);
            hasRegDisconnectReceiver = false;
            // Log.i(TAG, "关闭了广播！");
        }

    }

    @SuppressLint("ShowToast")
    @Override
    public void onClick(View v) {

        if (v == btn_search_devices) {
            XLog.i(TAG, "YXZ at SettingActivity onClick() isConnected:" + isConnected);
            if (!isConnected) {
                switch (interfaceType) {
                    case 0:// kuetooth
                        new AlertDialog.Builder(this).setTitle(R.string.str_message).setMessage(R.string.str_connlast)
                                .setPositiveButton(R.string.yesconn, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        connectMains = 0;
                                        // 重新连接
                                        if (!(mBtAdapter == null)) {
                                            // 判断设备蓝牙功能是否打开
                                            if (!mBtAdapter.isEnabled()) {
                                                // 打开蓝牙功能
                                                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                startActivityForResult(enableIntent, ENABLE_BT);
                                            } else {
                                                // mDevice
                                                devicesAddress = PrefUtils.getString(mContext, GlobalContants.DEVICEADDRESS,
                                                        "");

                                                if (devicesAddress == null || devicesAddress.length() <= 0) {
                                                    Toast.makeText(SettingActivity.this, R.string.toast_app_first_start,
                                                            Toast.LENGTH_SHORT).show();
                                                } else {
                                                    connect2BlueToothdevice();

                                                }
                                            }
                                        }

                                    }
                                }).setNegativeButton(R.string.str_resel, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        connectMains = 1;
                                        if (!(mBtAdapter == null)) {
                                            // 判断设备蓝牙功能是否打开
                                            if (!mBtAdapter.isEnabled()) {
                                                // 打开蓝牙功能
                                                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                startActivityForResult(enableIntent, SettingActivity.ENABLE_BT);
                                            } else {
                                                Intent intent = new Intent(mContext, BluetoothDeviceList.class);
                                                startActivityForResult(intent, CONNECT_DEVICE);

                                            }

                                        }

                                    }

                                }).show();

                        break;
                    case 1:// USB

                        new AlertDialog.Builder(this).setTitle(R.string.str_message).setMessage(R.string.str_connlast)
                                .setPositiveButton(R.string.yesconn, new DialogInterface.OnClickListener() {
                                    @SuppressLint("InlinedApi")
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {

                                        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                                        usbAutoConn(manager);
                                    }
                                }).setNegativeButton(R.string.str_resel, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        XLog.i(TAG, "yxz at SettingActivity.java "
                                                + "DialogInterface.OnClickListener() isConnected:" + isConnected);

                                        Intent intent = new Intent(mContext, UsbDeviceList.class);
                                        startActivityForResult(intent, CONNECT_DEVICE);

                                    }

                                }).show();
                        break;
                    case 2:// wifi
                        Intent intent = new Intent(mContext, IpEditActivity.class);
                        intent.putExtra(GlobalContants.WIFINAME, getWiFiName());
                        startActivityForResult(intent, CONNECT_DEVICE);
                        break;
                    case 3:// serial port
                        Intent intentSerial = new Intent(mContext, SerialsDeviceList.class);
                        startActivityForResult(intentSerial, CONNECT_DEVICE);
                        break;
                    default:
                        break;
                }
            } else {
                if (myPrinter != null) {
                    myPrinter.closeConnection();
                    myPrinter = null;
                    XLog.i(TAG, "yxz at SettingActivity.java  onClick()  mPrinter:" + myPrinter);
                    if (interfaceType == 0 && hasRegDisconnectReceiver) {
                        mContext.unregisterReceiver(myReceiver);
                        hasRegDisconnectReceiver = false;
                        // Log.i(TAG, "关闭了广播！");
                    }
                }
                tv_device_name.setText(getString(R.string.printerName));
                tv_printer_address.setText(getString(R.string.printerAddress));
            }

        }

        if (v == btn_scan_and_connect) {
            connectMains = 2;
            if (interfaceType != 0) {
                Toast.makeText(mContext, R.string.toast_scan_and_connect, 0).show();
                return;
            }
            if (isConnected) {
                Toast.makeText(mContext, getString(R.string.toast_scan_already_connect) + devicesName, 0).show();
                return;
            }
            // 判断设备蓝牙功能是否打开
            if (!mBtAdapter.isEnabled()) {
                // 打开蓝牙功能
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, ENABLE_BT);
            } else {
                Intent intent = new Intent();
                intent.setClass(mContext, MipcaActivityCapture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
            }

        }
        if (v == btn_scan_and_connect_name) {
            connectMains = 3;
            if (interfaceType != 0) {
                Toast.makeText(mContext, R.string.toast_scan_and_connect, 0).show();
                return;
            }
            if (isConnected) {
                Toast.makeText(mContext, getString(R.string.toast_scan_already_connect) + devicesName, 0).show();
                return;
            }
            // 判断设备蓝牙功能是否打开
            if (!mBtAdapter.isEnabled()) {
                // 打开蓝牙功能
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, ENABLE_BT);
            } else {
                Intent intent = new Intent();
                intent.setClass(mContext, MipcaActivityCapture_GetBtName.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_NAME_GREQUEST_CODE);
            }

        }

        if (v == btn_selfprint_test) {
            // WIFI 由于是网络操作，需要放到线程中，其他通信方式可以放在线程中也可以不必如此
            if (isConnected) {
                new Thread(new Runnable() {
                    public void run() {

//                        XTUtils.testLaoTable(myPrinter);
                        XTUtils.printTest(getResources(), myPrinter);
                    }

                }).start();
            } else {
                Toast.makeText(mContext, getString(R.string.no_connected), 0).show();
            }
        }
        if (v == btn_selfCheckingPage_print) {
            if (isConnected) {
                new Thread(new Runnable() {
                    public void run() {
                        synchronized (this) {
                            myPrinter.printSelfCheckingPage();
                            myPrinter.cutPaper(66, 50);
                            myPrinter.closeConnection();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, getString(R.string.reconnect), 0).show();
                            }
                        });
                    }

                }).start();
            } else {
                Toast.makeText(mContext, getString(R.string.no_connected), 0).show();
            }
        }
        if (v == btn_select_file) {
            //选择文件
            Intent intent2 = new Intent();
            intent2.setType("*/*");
            intent2.addCategory(Intent.CATEGORY_OPENABLE);
            intent2.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent2, 15);
        }
        if (v == btn_print_select_file) {
            //打印选择文件
            if (path == null) {
                Toast.makeText(mContext, "请先选择文件", 0).show();
                return;
            }
            int fileType = PrefUtils.getInt(mContext, GlobalContants.FILE_TYPE, 0);
//            testModel = PrefUtils.getInt(mContext, GlobalContants.TEST_MODEL, 0);
            if (fileType == 0) {
//            sendData = XTUtils.readFromFile(new File(path));
                sendData = XTUtils.readFile(new File(path));
                if (sendData != null) {
                    textToSend = hexToString(sendData);
                    XLog.d(TAG, "打印文件：" + textToSend);
//                    myPrinter.sendBytesData(textToSend.getBytes("GBK")); // 假设打印机使用GBK编码
                }

            } else if (fileType == 1) {
//            String content = Utils.readFromFile(new File(path));
                String content = XTUtils.readStringFromFile(new File(path));
                try {
                    sendData = XTUtils.string2bytes2(content);
                } catch (Exception e) {
                    Toast.makeText(mContext, "数据异常，请检查", Toast.LENGTH_SHORT).show();
                }
            }

            dialogH.show();
            // 定义一个异步任务
            Runnable asyncTask = new Runnable() {
                @Override
                public void run() {
                    // 在这里执行异步任务的代码
//                    myPrinter.
                    try {

//                        int length = myPrinter.sendBytesData(sendData);
                        XLog.d(TAG, "打印文件开始：" + textToSend);
                        int length = myPrinter.sendBytesData(hexStringToByteArray(textToSend)); // 假设打印机使用GBK编码
//                        int length =  myPrinter.sendBytesData(textToSend.getBytes("GBK")); // 假设打印机使用GBK编码

//                        int length = myPrinter.sendBytesData(sendData);
                        if (length > 0) {
                            //success
                            dialogH.dismiss();
                            runOnUiThread(() -> Toast.makeText(mContext, "打印成功", 0).show());
                        } else {
                            //fail
                            dialogH.dismiss();
                            runOnUiThread(() -> Toast.makeText(mContext, "打印失败", 0).show());
                        }
                    } catch (Exception e) {
                        //fail
                        dialogH.dismiss();
                        runOnUiThread(() -> Toast.makeText(mContext, "打印失败" + e.getMessage(), 0).show());
                    }
//                    singlePrinterTestTask(devicesAddress);
                }
            };
            // 将异步任务提交给线程池执行
            executor.execute(asyncTask);
        }

        if (v == btn_status_test) {
            if (isConnected) {
                new Thread(new Runnable() {
                    public void run() {
                        int i = myPrinter.getCurrentStatus();
                        if (i == 0) {
                            strStatus = getString(R.string.get_printer_status_normal);
                        } else if (i == -1) {
                            strStatus = getString(R.string.get_printer_status_read_error);
                        } else if (i == -2) {
                            strStatus = getString(R.string.get_printer_status_no_paper);
                        } else if (i == -3) {
                            strStatus = getString(R.string.get_printer_status_paper_will_out);
                        } else if (i == -4) {
                            strStatus = getString(R.string.get_printer_status_cover_open);
                        } else if (i == -5) {
                            strStatus = getString(R.string.get_printer_status_write_error);
                        }else{
                            strStatus = "状态码 : "+i;
                        }
                        runOnUiThread(new Runnable() {
                            public void run() {
                                Toast.makeText(mContext, strStatus, Toast.LENGTH_LONG).show();
                                XLog.i(TAG, "zl at SettingActivity.java onClick()------> btn_status_test");
                            }
                        });

                    }

                }).start();
            } else {
                Toast.makeText(mContext, getString(R.string.no_connected), 0).show();
            }
        }
    }

    public static byte[] hexStringToByteArray(String hexString) {
        // 去掉字符串中的空格
        hexString = hexString.replaceAll("\\s+", "");

        // 创建字节数组
        int len = hexString.length();
        byte[] byteArray = new byte[len / 2];

        for (int i = 0; i < len; i += 2) {
            // 将两个字符作为一个16进制字节转换为整数
            byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return byteArray;
    }

    public static String hexToString(byte[] hexData) {
        StringBuilder sb = new StringBuilder();
        for (byte b : hexData) {
            sb.append((char) b);
        }
        return sb.toString();
    }

    /**
     * 使设备震动
     */
    public void vibrators() {

        Vibrator vib = (Vibrator) SettingActivity.this.getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(1000);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static String jsonToStringFromAssetFolder(String fileName, Context context) throws IOException {
        AssetManager manager = context.getAssets();
        InputStream file = manager.open(fileName);
        byte[] data = new byte[file.available()];
        file.read(data);
        file.close();
        return new String(data, "gbk");

    }

    private boolean isDiscoverying = false;

    // 安卓3.1以后才有权限操作USB
    @SuppressLint("ShowToast")
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK)
            return;
        if (requestCode == CONNECT_DEVICE) {// 连接设备
            if (interfaceType == 0) {
                devicesAddress = data.getExtras().getString(BluetoothDeviceList.EXTRA_DEVICE_ADDRESS);
                devicesName = data.getExtras().getString(BluetoothDeviceList.EXTRA_DEVICE_NAME);
                connect2BlueToothdevice();

            } else if (interfaceType == 1)// usb
            {
                mUSBDevice = data.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                myPrinter = PrinterInstance.getPrinterInstance(mContext, mUSBDevice, mHandler);
                devicesName = mUSBDevice.getDeviceName();
                devicesAddress = "vid: " + mUSBDevice.getVendorId() + "  pid: " + mUSBDevice.getProductId();
                UsbManager mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
                if (mUsbManager.hasPermission(mUSBDevice)) {
                    myPrinter.openConnection();
                } else {
                    // 没有权限询问用户是否授予权限
                    int flags = PendingIntent.FLAG_IMMUTABLE;
//                    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
//                            new Intent(ACTION_USB_PERMISSION), 0);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(
                            mContext, 0,
                            new Intent(ACTION_USB_PERMISSION), flags
                    );
                    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
                    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
                    mContext.registerReceiver(mUsbReceiver, filter);
                    mUsbManager.requestPermission(mUSBDevice, pendingIntent); // 该代码执行后，系统弹出一个对话框
                }

            } else if (interfaceType == 2)// wifi
            {
                devicesName = "Net device";
                devicesAddress = data.getStringExtra("ip_address");
                Log.d(TAG, "onActivityResult: devicesAddress = "+devicesAddress);
                myPrinter = PrinterInstance.getPrinterInstance(devicesAddress, 9100, mHandler);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        myPrinter.openConnection();
                    }
                }).start();
            } else if (interfaceType == 3) {// 串口

                int baudrate = 9600;
                int liukong = PrefUtils.getInt(mContext, GlobalContants.LiuKongType, 1);
                String path = data.getStringExtra("path");
                devicesName = "Serial device";
                devicesAddress = path;
                String com_baudrate = data.getExtras().getString("baudrate");
                if (com_baudrate == null || com_baudrate.length() == 0) {
                    baudrate = 9600;
                }
                baudrate = Integer.parseInt(com_baudrate);
                XLog.i(TAG, "baudrate:" + baudrate);
                myPrinter = PrinterInstance.getPrinterInstance(new File(path), baudrate, 0, liukong, mHandler);
                myPrinter.openConnection();
                // myPrinter
                // .printText("测试串口连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭测试蓝牙连续连接--打印--关闭/n");
                // myPrinter.closeConnection();
                Log.i(TAG, "波特率:" + baudrate + "路径:" + path);
            }

        }
        if (requestCode == 15) {
            mContext.getFilesDir();
            path = data.getData().toString();
            int index = path.indexOf("/storage");
            if (index == -1) {
                path = UriUtils.getPath(mContext, data.getData());
            } else {
                path = path.substring(index);
            }
            tv_file_path.setText(path);
        }
        if (requestCode == SCANNIN_GREQUEST_CODE) {
            // 校验扫描到的mac是否合法
            devicesAddress = data.getExtras().getString(BluetoothDeviceList.EXTRA_DEVICE_ADDRESS);
            Log.d(TAG, "SCANNIN_GREQUEST_CODE devicesAddress:" + devicesAddress);
            devicesAddress = extractFirst12Hex(devicesAddress);
            Log.d(TAG, "extractFirst12Hex devicesAddress:" + devicesAddress);
            devicesAddress = BluetoothAddressUtil.extractBluetoothAddress(devicesAddress);
            Log.d(TAG, "devicesAddress:" + devicesAddress);
            if (devicesAddress.isEmpty()) {
                Toast.makeText(mContext, "输入字符串中未找到有效的蓝牙地址部分,请重新扫描", Toast.LENGTH_LONG);
                return;
            }
            devicesAddress = XTUtils.formatTheString(devicesAddress);
            if (BluetoothAdapter.checkBluetoothAddress(devicesAddress)) {
                connect2BlueToothdevice();
            } else {
                Log.e("yxz", devicesAddress);
                Toast.makeText(mContext, "蓝牙mac:" + devicesAddress + "不合法", Toast.LENGTH_LONG).show();
            }
        }
        if (requestCode == SCANNIN_NAME_GREQUEST_CODE) {
            mDevice = null;
            String deviceNameAndPassword = data.getExtras().getString(BluetoothDeviceList.EXTRA_DEVICE_NAME);
            Log.e("yxz", "EXTRA_DEVICE_NAME:" + deviceNameAndPassword);

            if (!deviceNameAndPassword.contains("&")) {
                Toast.makeText(mContext, "蓝牙名称跟密码:" + deviceNameAndPassword + "格式不合法", Toast.LENGTH_LONG).show();
                return;
            }
            String nameAndPass[] = deviceNameAndPassword.split("&");
            devicesName = nameAndPass[0];
            defaultPin = nameAndPass[1].trim();
            Toast.makeText(mContext, "扫描蓝牙名称连接打印测试中，请稍后...", Toast.LENGTH_LONG).show();
            Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
            for (BluetoothDevice device : pairedDevices) {
                if (devicesName.equals(device.getName())) {
                    mDevice = device;
                    break;
                }
            }
            if (mDevice != null) {
                devicesAddress = mDevice.getAddress();
                connect2BlueToothdevice();
                return;
            }
            //1.注册广播接收器
            registerBondReceiver(mContext);
            //2.搜索
            doDiscovery();

        }
        if (requestCode == ENABLE_BT) {
            if (resultCode == Activity.RESULT_OK) {
                if (!mBtAdapter.isEnabled()) {
                    // 打开蓝牙功能
                    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableIntent, SettingActivity.ENABLE_BT);
                } else {
                    switch (connectMains) {
                        case 0:
                            // mDevice
                            devicesAddress = PrefUtils.getString(mContext, GlobalContants.DEVICEADDRESS, "");
                            if (devicesAddress == null || devicesAddress.length() <= 0) {
                                Toast.makeText(SettingActivity.this, "您是第一次启动程序，请选择重新搜索连接！", Toast.LENGTH_SHORT).show();
                            } else {
                                connect2BlueToothdevice();
                            }
                            break;
                        case 1:
                            Intent intent = new Intent(mContext, BluetoothDeviceList.class);
                            startActivityForResult(intent, CONNECT_DEVICE);
                            break;
                        case 2:
                            Intent scanIntent = new Intent();
                            scanIntent.setClass(mContext, MipcaActivityCapture.class);
                            scanIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivityForResult(scanIntent, SCANNIN_GREQUEST_CODE);
                            break;
                        case 3:
                            Intent scanIntent2 = new Intent();
                            scanIntent2.setClass(mContext, MipcaActivityCapture_GetBtName.class);
                            scanIntent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivityForResult(scanIntent2, SCANNIN_NAME_GREQUEST_CODE);
                            break;
                    }
                }
            } else {
                Toast.makeText(SettingActivity.this, R.string.bt_not_enabled, 0).show();
            }
        }
    }

    public static String extractFirst12Hex(String input) {
        // 删除所有非十六进制字符
        input = input.replaceAll("[^0-9A-Fa-f]", "");

        // 取前12个字符
        if (input.length() >= 12) {
            String rawAddress = input.substring(0, 12);
            // 格式化为标准的蓝牙地址格式 00:12:5B:01:F6:D2
            return rawAddress.replaceAll("(.{2})(?=.)", "$1:");
        } else {
            return ""; // 如果不足12个字符，返回空字符串
        }
    }

    private void connect2BlueToothdevice() {
        dialog.show();
        mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(devicesAddress);
        devicesName = mDevice.getName();
        myPrinter = PrinterInstance.getPrinterInstance(mDevice, mHandler);
        if (mDevice.getBondState() == BluetoothDevice.BOND_NONE) {// 未绑定
            // IntentFilter boundFilter = new IntentFilter();
            // boundFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            // mContext.registerReceiver(boundDeviceReceiver, boundFilter);
            PairOrConnect(true);
        } else {
            PairOrConnect(false);
        }
    }

    private void PairOrConnect(boolean pair) {
        if (pair) {
            IntentFilter boundFilter = new IntentFilter();
            boundFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            mContext.registerReceiver(boundDeviceReceiver, boundFilter);
            boolean success = false;
            try {
                // // 自动设置pin值
                // Method autoBondMethod =
                // BluetoothDevice.class.getMethod("setPin", new Class[] {
                // byte[].class });
                // boolean result = (Boolean) autoBondMethod.invoke(mDevice, new
                // Object[] { "1234".getBytes() });
                // Log.i(TAG, "setPin is success? : " + result);

                // 开始配对 这段代码打开输入配对密码的对话框
                Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
                success = (Boolean) createBondMethod.invoke(mDevice);
                // // 取消用户输入
                // Method cancelInputMethod =
                // BluetoothDevice.class.getMethod("cancelPairingUserInput");
                // boolean cancleResult = (Boolean)
                // cancelInputMethod.invoke(mDevice);
                // Log.i(TAG, "cancle is success? : " + cancleResult);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Log.e(TAG, "IllegalAccessException: " + e.getMessage());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                Log.e(TAG, "IllegalArgumentException: " + e.getMessage());
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                Log.e(TAG, "InvocationTargetException: " + e.getMessage());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                Log.e(TAG, "NoSuchMethodException: " + e.getMessage());
            }
            Log.i(TAG, "createBond is success? : " + success);
        } else {
            new connectThread().start();

        }
    }

    private void updateButtonState(boolean isConnected) {
        XLog.d(TAG, "yxz at SettingActivity.java updateButtonState() ---begin");
        XLog.d(TAG, "yxz at SettingActivity.java updateButtonState() ---isConnected" + isConnected);
        if (isConnected) {
            headerConnecedState.setText(R.string.on_line);
            btn_search_devices.setText(R.string.disconnect);
            setTitleState(mContext.getResources().getString(R.string.on_line));
            Log.i("fdh", getString(R.string.printerName).split(":")[0]);
            Log.i("fdh", getString(R.string.printerAddress).split(":")[0]);
            tv_device_name.setText(getString(R.string.printerName).split(":")[0] + ": " + devicesName);
            tv_printer_address.setText(getString(R.string.printerAddress).split(":")[0] + ": " + devicesAddress);
        } else {
            tv_device_name.setText(getString(R.string.printerName));
            tv_printer_address.setText(getString(R.string.printerAddress));
            btn_search_devices.setText(R.string.connect);
            headerConnecedState.setText(R.string.off_line);
            setTitleState(mContext.getResources().getString(R.string.off_line));
            // mHandler.removeCallbacks(runnable);
            // if (isFirst) {
            //
            // } else {
            // timer.cancel();
            // }
            // Log.i(TAG, "定时器取消了");
            XLog.d(TAG, "yxz at SettingActivity.java updateButtonState() ---end");

        }

        PrefUtils.setBoolean(SettingActivity.this, GlobalContants.CONNECTSTATE, isConnected);

    }

    private BroadcastReceiver boundDeviceReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (!mDevice.equals(device)) {
                    return;
                }
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_BONDING:
                        Log.i(TAG, "bounding......");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        Log.i(TAG, "bound success");
                        // if bound success, auto init BluetoothPrinter. open
                        // connect.
                        mContext.unregisterReceiver(boundDeviceReceiver);
                        dialog.show();
                        // 配对完成开始连接
                        if (myPrinter != null) {
                            new connectThread().start();
                        }
                        break;
                    case BluetoothDevice.BOND_NONE:
                        Log.i(TAG, "执行顺序----4");
                        mContext.unregisterReceiver(boundDeviceReceiver);
                        Log.i(TAG, "bound cancel");
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                            Toast.makeText(mContext, "配对失败", Toast.LENGTH_LONG).show();
                        }
                        break;
                    default:
                        break;
                }

            }
        }
    };

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @SuppressLint("NewApi")
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.w(TAG, "receiver action: " + action);

            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    mContext.unregisterReceiver(mUsbReceiver);
                    UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)
                            && mUSBDevice.equals(device)) {
                        myPrinter.openConnection();
                    } else {
                        mHandler.obtainMessage(Connect.FAILED).sendToTarget();
                        Log.e(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };


    private class connectThread extends Thread {
        @Override
        public void run() {
            if (myPrinter != null) {
                isConnected = myPrinter.openConnection();
            }
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // if (parent == spinner_printer_type) {
        // PrefUtils.setInt(mContext, GlobalContants.PRINTERID, position);
        // String printerName = getResources().getStringArray(
        // R.array.printertype)[position];
        // if (printerName.contains("T10") || printerName.contains("POS58")
        // || printerName.contains("T7")) {
        // rg__select_paper_size.check(R.id.rb_58mm);
        // } else if (printerName.contains("L31")
        // || printerName.contains("T9")
        // || printerName.contains("POS885")
        // || printerName.contains("EU80")) {
        // rg__select_paper_size.check(R.id.rb_80mm);
        // } else if (printerName.contains("L51")) {
        // rg__select_paper_size.check(R.id.rb_100mm);
        // }
        // } else if (parent == spinner_interface_type) {
        // }

        if (parent == spinner_interface_type) {
            PrefUtils.setInt(mContext, GlobalContants.INTERFACETYPE, position);
            interfaceType = position;
            Log.i(TAG, "position:" + position);
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group == rg__select_paper_size) {
            switch (checkedId) {
                case R.id.rb_58mm:
                    PrinterConstants.paperWidth = 384;
                    break;
                case R.id.rb_80mm:
                    PrinterConstants.paperWidth = 576;
                    break;
                case R.id.rb_100mm:
                    PrinterConstants.paperWidth = 812;
                    break;
                default:
                    PrinterConstants.paperWidth = 576;
                    break;
            }
            PrefUtils.setInt(mContext, GlobalContants.PAPERWIDTH, PrinterConstants.paperWidth);
        }
        if (group == rgPrinterType) {
            switch (checkedId) {
                case R.id.rb_normal:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERTYPE, 0);
                    break;
                case R.id.rb_t7:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERTYPE, 1);
                    break;
                default:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERTYPE, 0);
                    break;
            }
        }
        if (group == rgPrinterLog) {
            switch (checkedId) {
                case R.id.rb_log_open:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERLOG, 0);
                    // 打开记录
                    ZLog.openSaveToFile();
                    break;
                case R.id.rb_log_close:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERLOG, 1);
                    // 关闭记录
                    ZLog.closeSaveToFile();
                    break;
                default:
                    PrefUtils.setInt(mContext, GlobalContants.PRINTERLOG, 0);
                    break;
            }
        }
    }

    /*
     * @Override public void onConfigurationChanged(Configuration newConfig) {
     * super.onConfigurationChanged(newConfig); if
     * (this.getResources().getConfiguration().orientation ==
     * Configuration.ORIENTATION_LANDSCAPE) { // land } else if
     * (this.getResources().getConfiguration().orientation ==
     * Configuration.ORIENTATION_PORTRAIT) { // port } }
     */

    @SuppressLint({"InlinedApi", "NewApi"})
    public void usbAutoConn(UsbManager manager) {

        doDiscovery(manager);
        if (deviceList.isEmpty()) {
            Toast.makeText(mContext, R.string.no_connected, 0).show();
            return;
        }
        mUSBDevice = deviceList.get(0);
        if (mUSBDevice == null) {
            mHandler.obtainMessage(Connect.FAILED).sendToTarget();
            return;
        }
        myPrinter = PrinterInstance.getPrinterInstance(mContext, mUSBDevice, mHandler);
        devicesName = mUSBDevice.getDeviceName();
        devicesAddress = "vid: " + mUSBDevice.getVendorId() + "  pid: " + mUSBDevice.getProductId();
        UsbManager mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
        if (mUsbManager.hasPermission(mUSBDevice)) {
            myPrinter.openConnection();
        } else {
            // 没有权限询问用户是否授予权限
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
            filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
            mContext.registerReceiver(mUsbReceiver, filter);
            mUsbManager.requestPermission(mUSBDevice, pendingIntent); // 该代码执行后，系统弹出一个对话框
        }

    }

    @SuppressLint("NewApi")
    private void doDiscovery(UsbManager manager) {
        HashMap<String, UsbDevice> devices = manager.getDeviceList();
        deviceList = new ArrayList<UsbDevice>();
        for (UsbDevice device : devices.values()) {
            if (USBPort.isUsbPrinter(device)) {
                deviceList.add(device);
            }
        }

    }

    private String getWiFiName() {
        String wifiName = null;
        WifiManager mWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!mWifi.isWifiEnabled()) {
            mWifi.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = mWifi.getConnectionInfo();
        wifiName = wifiInfo.getSSID();
        Log.i("yxz", "wifiName" + wifiName);
        wifiName = wifiName.replaceAll("\"", "");
        return wifiName;

    }

    public BroadcastReceiver myReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {

                if (device != null && myPrinter != null && isConnected && device.equals(mDevice)) {
                    myPrinter.closeConnection();
                    mHandler.obtainMessage(Connect.CLOSED).sendToTarget();
                }
            }

        }
    };

    public synchronized static void allCut(View v) {
        if (isConnected) {
            new Thread(new Runnable() {
                public void run() {
                    XTUtils.printNote(mContext.getResources(), myPrinter);
                    myPrinter.cutPaper(65, 50);
                }
            }).start();

            // num=0;

        } else {
            Toast.makeText(mContext, mContext.getString(R.string.no_connected), 0).show();
        }
    }

    static int num = 0;

    public synchronized static void halfCut(View v) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                PrinterInstance myPrinter = PrinterInstance.getPrinterInstance("192.168.1.170", 9100, null);
//                for(int j=0;j<1000;j++){
//                    if (myPrinter.openConnection()) {
//                        myPrinter.sendBytesData(new byte[]{0x10, 0x04, 0x01});
//                        try {
//                            Thread.sleep(200);
//                        }catch (InterruptedException e){
//
//                        }
//                        int readLen = 0;
//                        byte[] data = new byte[10];
//                        for (int i = 0; i < 30; i++) {
//                            readLen = myPrinter.read(data);
//                            if (readLen > 0)
//                                break;
//                            try {
//                                Thread.sleep(100);
//                            }catch (InterruptedException e){
//
//                            }
//                        }
//                        XLog.d("test", "第"+j+"次收到查询数据："+Utils.bytesToHexString(data, readLen));
//                    } else {
//                        XLog.e("test", "第"+j+"次连接失败");
//                    }
//                    try {
//                        Thread.sleep(500);
//                    }catch (InterruptedException e){
//
//                    }
//                }
//            }
//        }).start();


        if (isConnected) {
            new Thread(new Runnable() {
                public void run() {
                    XTUtils.printNote(mContext.getResources(), myPrinter);
                    synchronized (this) {
                        myPrinter.cutPaper(66, 50);
                    }
                }
            }).start();
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    List<PrintBean> list =new ArrayList();
//                    PrintBean pb =new PrintBean("transferWaybillNo","goodQty","flowDirectionCore","serviceProduct","goodsNum");
//                    list.add(pb);
//                    num=100;
//                    for(int i=0;i<num;i++){
//                        new PrintLabel80_test().doPrint(myPrinter,getResources(), (ArrayList<PrintBean>) list);
//                        try {
//                            Thread.sleep(500);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }).start();
        } else {
            Toast.makeText(mContext, mContext.getString(R.string.no_connected), 0).show();
        }
    }


    private final BroadcastReceiver bondReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //开始搜索的广播
            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                XLog.d(TAG, "开始搜索蓝牙设备");
                return;
            }
            //停止搜索的广播
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                XLog.d(TAG, "停止搜索蓝牙设备");
                return;
            }
            //发现蓝牙设备的广播
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                XLog.d(TAG, "发现蓝牙设备，device.name:" + device.getName() + ",device.address:" + device.getAddress());
                int deviceType = device.getBluetoothClass().getMajorDeviceClass();
                if ((devicesName.equals(device.getName()))) {
                    XLog.d(TAG, "找到目标打印机，停止搜索");
                    //找到目标打印机，停止搜索。
                    cancelDiscovery();
                    //自动配对
                    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                        try {
                            BondUtils.createBond(mContext, device.getClass(), device);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(mContext, "配对异常，请手动配对", Toast.LENGTH_SHORT).show();
                        }
                        return;
                    }
                    //已配对，连接并打印。
                    devicesAddress = device.getAddress();
                    mHandler.obtainMessage(555).sendToTarget();
                    return;
                }
            }
            //执行自动配对代码发送的广播
            if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 2.终止有序广播
                abortBroadcast();// 如果没有将广播终止，则会出现一个一闪而过的配对框。
                // 3.调用setPin方法进行配对...
                try {
                    //该返回值不能准确的表示是否配对成功，测试发现该值返回true时，设备的绑定状态为正在绑定中，故将添加设备的操作放至监听绑定状态的广播中。
                    boolean ret = BondUtils.setPin(device.getClass(), device, defaultPin);
                    Log.d(TAG, "提示信息:" + "setPin()结果：" + ret);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_BONDING:
                        //Toast.makeText(mContext, "正在配对中,请稍候", Toast.LENGTH_SHORT).show();
                        XLog.d(TAG, "----------bounding......,mac" + device.getAddress() + "----------");
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        // Toast.makeText(mContext, "配对成功", Toast.LENGTH_SHORT).show();
                        devicesAddress = device.getAddress();
                        connect2BlueToothdevice();
                        XLog.d(TAG, "----------bound success, mac:" + device.getAddress() + "----------");
                        break;

                    case BluetoothDevice.BOND_NONE:
                        //   Toast.makeText(mContext, "配对取消，请手动配对再重新测试", Toast.LENGTH_SHORT).show();
                        XLog.d(TAG, "----------bound cancel, mac:" + device.getAddress() + "----------");
                        break;
                    default:
                        break;

                }
            }
        }
    };
    private boolean hasRegist = false;

    public void cancelDiscovery() {
        XLog.d(TAG, "yxz at MainActivity.java cancelDiscovery() begin");
        isDiscoverying = false;
        if (mBtAdapter != null && mBtAdapter.isDiscovering()) {
            mBtAdapter.cancelDiscovery();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            XLog.d(TAG, "yxz at MainActivity.java mBtAdapter.cancelDiscovery()");
        }
        XLog.d(TAG, "yxz at MainActivity.java cancelDiscovery() end");
    }

    public void doDiscovery() {
        XLog.d(TAG, "yxz at MainActivity.java doDiscovery() begin");
        isDiscoverying = true;
        mBtAdapter.startDiscovery();
        XLog.d(TAG, "yxz at MainActivity.java doDiscovery() end");
    }

    public void registerBondReceiver(Context mContext) {
        if (hasRegist) {
            unRegisterDiscoveryReceiver(mContext);
        }
        IntentFilter filter = new IntentFilter();
        filter.setPriority(1000);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mContext.registerReceiver(bondReceiver, filter);
        hasRegist = true;
        XLog.d(TAG, "yxz at MainActivity.java registerDiscoveryReceiver() 注册广播接收者，接收发现设备消息的广播");
    }

    public void unRegisterDiscoveryReceiver(Context mContext) {
        if (hasRegist) {
            mContext.unregisterReceiver(bondReceiver);
            hasRegist = false;
            XLog.d(TAG, "yxz at MainActivity.java registerDiscoveryReceiver() 取消注册广播接收者");
        }
    }
}