fctom1215
2021-02-09 b1dfc31dd12a3b23d76a9e022d3b361bdb5f2638
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.anyun.exam.lib.util;
 
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
 
import java.lang.reflect.Method;
 
public class Bluetooth extends BroadcastReceiver {
    private static final String TAG = Bluetooth.class.getCanonicalName();
    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 = null;
    private String mName = null;
 
    public static Bluetooth getInstance(Context context, Handler handler) {
        if (instance == null) {
            synchronized (Bluetooth.class) {
                if (instance == null) {
                    instance = new Bluetooth(context, handler);
                }
            }
        }
        return instance;
    }
 
    private Bluetooth(Context context, Handler handler) {
        this.context = context;
        this.mHandler = handler;
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
 
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        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);
    }
 
    public void OpenBluetooth() {
        if (!mBtAdapter.isEnabled()) {
            mHandler.obtainMessage(Constants.MESSAGE_BLUETOOTH_STATUS, 0, -1, null)
                    .sendToTarget();
            mBtAdapter.enable();
        } else {
//            Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//            discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
//            discoveryIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(discoveryIntent);
 
            mHandler.obtainMessage(Constants.MESSAGE_BLUETOOTH_STATUS, 1, -1, null)
                    .sendToTarget();
        }
    }
 
    public void CloseBluetooth() {
        if (mBtAdapter.isEnabled()) {
            mBtAdapter.disable();
        }
    }
 
    public void unRegisterReceiver() {
        context.unregisterReceiver(this);
    }
 
    public void SetPin(String pin) {
        this.pin = pin;
    }
 
    /**
     * Start device discover with the BluetoothAdapter
     */
    public void doDiscovery(String name) {
        Log.d(TAG, "doDiscovery()");
 
        mName = name;
 
        // If we're already discovering, stop it
        if (mBtAdapter.isDiscovering()) {
            mBtAdapter.cancelDiscovery();
        }
 
        // Request discover from BluetoothAdapter
        mBtAdapter.startDiscovery();
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
 
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getName() != null && mName != null && device.getName().equals(mName)) {
                Message msg = mHandler.obtainMessage(Constants.MESSAGE_BLUETOOTH_FOUND);
                Bundle bundle = new Bundle();
                bundle.putString(Constants.DEVICE_ADDRESS, device.getAddress());
                msg.setData(bundle);
                mHandler.sendMessage(msg);
 
                mBtAdapter.cancelDiscovery();
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
 
            Message msg = mHandler.obtainMessage(Constants.MESSAGE_BLUETOOTH_DISCOVERY_FINISHED);
            mHandler.sendMessage(msg);
        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            int status2 = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
 
            if (status == BluetoothAdapter.STATE_ON) {
                OpenBluetooth();
            } else if (status == BluetoothAdapter.STATE_OFF) {
                OpenBluetooth();
            }
        } else if (action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 
            String name = device.getName();
 
            Log.d(TAG, "匹配请求 " + name);
            Log.d(TAG, "默认密码 " + pin);
 
            if (pin == null && name != null && name.contains("AKS3A_") && !name.contains("TEST") && 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 " + pin);
            /*if (device.getName().equals(BLUETOOTH_OBD_NAME) || device.getName().equals(BLUETOOTH_TARGET2_NAME)) */{
                try {
                    // 第三方的系统需要注释掉,否则会抛异常
//                    ClsUtils.setPairingConfirmation(device.getClass(), device, true);
                    Log.d(TAG, "isOrderedBroadcast:"+isOrderedBroadcast()+",isInitialStickyBroadcast:"+isInitialStickyBroadcast());
                    if (isOrderedBroadcast()) {
                        abortBroadcast();
                    }
 
                    ClsUtils.setPin(device.getClass(), device, pin);
                } catch (Exception e) {
 
                }
            }
        } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
 
        } else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            int status = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
            Log.d(TAG, "扫描模式 " + status);
        }
    }
}