BluetoothConnectActivityReceiver.java 1.24 KB
package com.eztlib.blu;

import com.ectrip.cyt.config.MyApp;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
	String strPsw = "1234";

	@Override
	public void onReceive(Context context, final Intent intent) {
		try {
			if (intent.getAction().equals(
					"android.bluetooth.device.action.PAIRING_REQUEST")) {

				if (MyApp.getInstance().isHandset) {
					new Thread() {
						@Override
						public void run() {
							BluetoothDevice btDevice = intent
									.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

//							 byte[] pinBytes =
//							 BluetoothDevice.convertPinToBytes("1234");
//							 device.setPin(pinBytes);
							try {
								ClsUtils.setPin(btDevice.getClass(), btDevice,
										strPsw); // 手机和蓝牙采集器配对
								ClsUtils.createBond(btDevice.getClass(),
										btDevice);
								ClsUtils.cancelPairingUserInput(
										btDevice.getClass(), btDevice);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}