yy1717
2020-11-24 6e0f29b08a040d14576d7053c1206a8439936570
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
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 = "0000";
 
    private static Bluetooth instance = null;
    private Context context;
    private BluetoothAdapter mBtAdapter = null;
    private Handler mHandler;
    private String pin = DEFAULT_BT_PASSWORD;
    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");
        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")) {
            Log.d(TAG, "匹配请求");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            /*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) {
                    e.printStackTrace();
                }
            }
        } 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);
        }
    }
}