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); } } }