在工作中遇到一個(gè)問(wèn)題:在藍(lán)牙連接時(shí)需要跳過(guò)pin驗(yàn)證男图,當(dāng)時(shí)找了很多博客呆万,最后基本解決(有些room會(huì)出問(wèn)題)肌蜻,因?yàn)樵诤?jiǎn)書(shū)中沒(méi)有搜到該問(wèn)題的解決方案所以就寫(xiě)下這篇博客。
基本解決思路:
1恕刘、使用反射調(diào)用BluetoothDevice的setPin方法缤谎。
2、接收系統(tǒng)的彈出的驗(yàn)證pin碼的彈框的廣播褐着,及時(shí)終止坷澡。
1、注冊(cè)廣播
<receiver android:name=".BluetoothConnectActivityReceiver" >
<intent-filter android:priority="1000">
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
</intent-filter>
</receiver>
public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.bluetooth.device.action.PAIRING_REQUEST".equals(intent.getAction())) {
BluetoothDevice mBluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
try {
//(三星)4.3版本測(cè)試手機(jī)還是會(huì)彈出用戶(hù)交互頁(yè)面(閃一下)含蓉,如果不注釋掉下面這句頁(yè)面不會(huì)取消但可以配對(duì)成功频敛。(中興,魅族4(Flyme 6))5.1版本手機(jī)兩中情況下都正常
//ClsUtils.setPairingConfirmation(mBluetoothDevice.getClass(), mBluetoothDevice, true);
abortBroadcast();//如果沒(méi)有將廣播終止馅扣,則會(huì)出現(xiàn)一個(gè)一閃而過(guò)的配對(duì)框斟赚。
//3.調(diào)用setPin方法進(jìn)行配對(duì)...
boolean ret = ClsUtils.setPin(mBluetoothDevice.getClass(), mBluetoothDevice, "你需要設(shè)置的PIN碼");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
最后把ClsUtils貼出來(lái),網(wǎng)上這個(gè)類(lèi)應(yīng)該是很多的差油。我已開(kāi)始也用到了拗军,因?yàn)闆](méi)有監(jiān)聽(tīng)上面的廣播所以失敗了任洞。
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
public class ClsUtils {
/**
* 與設(shè)備配對(duì) 參考源碼:platform/packages/apps/Settings.git
* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}
/**
* 與設(shè)備解除配對(duì) 參考源碼:platform/packages/apps/Settings.git
* /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
static public boolean removeBond(Class<?> btClass, BluetoothDevice btDevice) throws Exception {
Method removeBondMethod = btClass.getMethod("removeBond");
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}
static public boolean setPin(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice, String str) throws Exception {
try {
Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[]{byte[].class});
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice,
new Object[]
{str.getBytes()});
Log.e("returnValue", "" + returnValue);
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
// 取消用戶(hù)輸入
static public boolean cancelPairingUserInput(Class<?> btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
// cancelBondProcess(btClass, device);
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
// 取消配對(duì)
static public boolean cancelBondProcess(Class<?> btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("cancelBondProcess");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
//確認(rèn)配對(duì)
static public void setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm) throws Exception {
Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
setPairingConfirmation.invoke(device, isConfirm);
}
/**
*
* @param clsShow
*/
static public void printAllInform(Class clsShow) {
try {
// 取得所有方法
Method[] hideMethod = clsShow.getMethods();
int i = 0;
for (; i < hideMethod.length; i++) {
Log.e("method name", hideMethod[i].getName() + ";and the i is:"+ i);
}
// 取得所有常量
Field[] allFields = clsShow.getFields();
for (i = 0; i < allFields.length; i++) {
Log.e("Field name", allFields[i].getName());
}
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
藍(lán)牙的連接,斷開(kāi)的代碼我就不貼出來(lái)了发侵,記得要加上必要的權(quán)限交掏。
Android 6.0 一定要加上模糊定位的權(quán)限!
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
向開(kāi)源者致敬!