注册广播
针对指定广播形式,对指定的广播做捕获操作。
<!-- 增加一个配对码广播监听 -->
<receiver android:name="com.winstar.minid.bletest.BluetoothReceiver">
<intent-filter >
<!-- 捕捉配对请求 -->
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST"/>
</intent-filter>
</receiver>
处理类
import org.whisper.bluetooth.BluetoothTools;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* 获取系统弹窗,针对 android.bluetooth.device.action.PAIRING_REQUEST <br>
* 捕获配对码;<br>
* 自动输入匹配码,实现pin的校验;
* @author 765199214
*
*/
public class BluetoothReceiver extends BroadcastReceiver {
private static final String TAG = "BluetoothReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "######## 配对请求捕获 ######");
// 从Intent中获取设备对象
BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//获取 action
String action = intent.getAction();
if("android.bluetooth.device.action.PAIRING_REQUEST".equals(action)) {
Log.i(TAG, "##### 是配对请求的请求 ######");
//判断是否是 北斗设备
if("bdstar".equalsIgnoreCase(btDevice.getName())) {
Log.i(TAG, "####### 北斗设备 #######");
Bundle extras = intent.getExtras();
Log.i(TAG, "-->"+extras.toString());
Object device = extras.get("android.bluetooth.device.extra.DEVICE");
Object pairkey = extras.get("android.bluetooth.device.extra.PAIRING_KEY");
Log.i(TAG, "device-->"+String.valueOf(device));
Log.i(TAG, "pairkey-->"+String.valueOf(pairkey));
try {
BluetoothTools.setPairingConfirmation(btDevice.getClass(), btDevice, true);
Log.i("order...", "isOrderedBroadcast:"+isOrderedBroadcast()+",isInitialStickyBroadcast:"+isInitialStickyBroadcast());
abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。
//3.调用setPin方法进行配对...
boolean ret = BluetoothTools.setPin(btDevice.getClass(), btDevice, String.valueOf(pairkey));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
工具类
public class BluetoothTools {
private static final boolean DBG = true;
private static final String TAG = "BluetoothTools";
public static boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method createBondMethod = btClass.getMethod("createBond", new Class[0]);
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice, new Object[0]);
return returnValue.booleanValue();
}
public static boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method removeBondMethod = btClass.getMethod("removeBond", new Class[0]);
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[0]);
return returnValue.booleanValue();
}
public static boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
try {
Class[] arrayOfClass = new Class[1];
arrayOfClass[0] = byte[].class;
Method removeBondMethod = btClass.getDeclaredMethod("setPin", arrayOfClass);
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = str.getBytes();
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, arrayOfObject);
Log.e(TAG, "setPin result: " + returnValue);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
//确认配对
static public void setPairingConfirmation(Class<?> btClass,BluetoothDevice device,boolean isConfirm)throws Exception
{
Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation",boolean.class);
setPairingConfirmation.invoke(device,isConfirm);
}
public static boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("cancelPairingUserInput", new Class[0]);
Boolean returnValue = (Boolean) createBondMethod.invoke(device, new Object[0]);
return returnValue.booleanValue();
}
public static boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("cancelBondProcess", new Class[0]);
Boolean returnValue = (Boolean) createBondMethod.invoke(device, new Object[0]);
return returnValue.booleanValue();
}
}