yy1717
2020-11-25 1baa8f7baa336ce7e78d4b8389a351526cb8c673
lib/src/main/java/com/anyun/exam/lib/util/Bluetooth.java
@@ -15,13 +15,13 @@
public class Bluetooth extends BroadcastReceiver {
    private static final String TAG = Bluetooth.class.getCanonicalName();
    public static final String DEFAULT_BT_PASSWORD = "0000";
    public static final String DEFAULT_BT_PASSWORD = "1111";
    private static Bluetooth instance = null;
    private Context context;
    private BluetoothAdapter mBtAdapter = null;
    private Handler mHandler;
    private String pin = DEFAULT_BT_PASSWORD;
    private String pin = null;
    private String mName = null;
    public static Bluetooth getInstance(Context context, Handler handler) {
@@ -46,6 +46,9 @@
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        filter.addAction("android.bluetooth.device.action.PAIRING_REQUEST");
        filter.setPriority(1000);
        context.registerReceiver(this, filter);
    }
@@ -129,6 +132,25 @@
            Log.d(TAG, "匹配请求");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String name = device.getName();
            if (pin == null && name != null && name.contains("AKS3A_") && name.length() >= 8) {
                char [] sn = new char[8];
                name.getChars(name.length() - 8, name.length(), sn, 0);
                int a = ((sn[0] - '0') * 10 + sn[1] - '0') * 100 + (sn[2] - '0') * 10 + sn[3] - '0';
                int b = ((sn[4] - '0') * 10 + sn[5] - '0') * 100 + (sn[6] - '0') * 10 + sn[7] - '0';
                a = (a * b * 1978) % 10000;
                pin = String.format("%02d", a/100) + String.format("%02d", a%100);
            }
            if (pin == null) {
                pin = DEFAULT_BT_PASSWORD;
            }
            Log.d(TAG, "匹配吗 " + pin);
            /*if (device.getName().equals(BLUETOOTH_OBD_NAME) || device.getName().equals(BLUETOOTH_TARGET2_NAME)) */{
                try {
                    ClsUtils.setPairingConfirmation(device.getClass(), device, true);
@@ -139,7 +161,7 @@
                    ClsUtils.setPin(device.getClass(), device, pin);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {