//实现代码:
@Override
public void onCreate(Bundle savedInstanceState)
{
//......
/*蓝牙自动配对*/
registerReceiver(_pairingRequest, new
IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST"));
}
/**窗口结束后,注销蓝牙配对监听*/
@Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(_pairingRequest);
}
/**
* 蓝牙自动配对处理
* @see
* */
private BroadcastReceiver _pairingRequest = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().indexOf("PAIRING_REQUEST") > 0)
{
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
try
{
BluetoothCtrl.setPin(device, "0000");
}
catch (Exception e)
{
}
}
}
};
/*在需要进行配对处理的时,发出配对请求的代码(根据需要加在相应的位置)*/
BluetoothDevice device = mBT.getRemoteDevice(sMac);
//返回时,判断设备是否建立过绑定
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{ //建立设备绑定
try
{
if (BluetoothCtrl.createBond(device))
{ //配对建立成功
Toast.makeText(actDiscovery.this,
getString(R.string.msg_bluetooth_Bond_Success),
Toast.LENGTH_LONG).show();
bBondSuccess = true;
}
else //配对未完成
{
Toast.makeText(actDiscovery.this,
getString(R.string.msg_bluetooth_Bond_fail),
Toast.LENGTH_LONG).show();
bBondSuccess = false;
}
}
catch (Exception e)
{
Toast.makeText(actDiscovery.this,
getString(R.string.msg_bluetooth_Bond_fail),
Toast.LENGTH_LONG).show();
}
}
else //已经建立过蓝牙绑定
bBondSuccess = true;