BluetoothConnectActivityReceiver.java:监听蓝牙配对的广播
代码:
package com.imte.Broadcast;
import com.imte.utils.ClsUtils;
import com.itme.ActivityClass.R;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
String strPsw = "123456";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(
"android.bluetooth.device.action.PAIRING_REQUEST")) {
BluetoothDevice btDevice = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
try {
ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
ClsUtils.createBond(btDevice.getClass(), btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
Toast.makeText(
context,
context.getResources().getString(
R.string.bluetooth_connect_success),
Toast.LENGTH_SHORT);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// Thread thread=new Thread(strPsw);
// thread.
}
}
}
}
ClsUtils.java:里面主要是一些通过反射机制得到蓝牙配对的相关方法,代码如下:
package com.imte.utils;
/************************************ 蓝牙配对函数 * *************