public void BTPairAsHost(Activity activity)
{
Log.e(TAG,"BTPairAsHost");
try{
int delayTime = 3000;
adapter = BluetoothAdapter.getDefaultAdapter();
String mac = adapter.getAddress();
boundDeviceAddress = "B8:BC:1B:1E:F5:D9";
Log.e(TAG,"mac : "+mac);
if(adapter != null)
{
IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter2.addAction(ACTION_PAIRING_REQUEST);
filter2.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
final ConditionVariable done = new ConditionVariable();
BluetoothStateChangeReceiver receiver1 = new BluetoothStateChangeReceiver(done);
BluetoothDiscoveryReceiver receiver2 = new BluetoothDiscoveryReceiver(done);
activity.registerReceiver(receiver1, filter1);
activity.registerReceiver(receiver2, filter2);
Log.e(TAG,"start enable\n");
if(!adapter.isEnabled())
{
//2.打开设备
if(adapter.enable())
{
//等待设备打开完毕(在 BluetoothStateChangeReceiver中完成)
done.block();
done.close();
}
SystemClock.sleep(delayTime);
}
Display.appendInfo("start discovery...\n");
//3.搜索蓝牙设备
adapter.startDiscovery();
//等待扫描到指定设备并配对(4.进行配对)
done.block();
done.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
广播:
class BluetoothStateChangeReceiver extends BroadcastReceiver{
private ConditionVariable done;
BluetoothStateChangeReceiver(ConditionVariable done)
{
this.done = done;
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.e("state onReceive","Action = "+arg1.getAction());
if(arg1.getAction() == BluetoothAdapter.ACTION_STATE_CHANGED)
{
int state = arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
//蓝牙打开完成或关闭完成
Log.e(TAG,"state : "+state);
if(state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
{
Log.e(TAG,"open success\n");
done.open();
}
}
}
}
class BluetoothDiscoveryReceiver extends BroadcastReceiver{
private ConditionVariable done;
BluetoothDiscoveryReceiver(ConditionVariable done)
{
this.done = done;
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.e("Discovery onReceive","Action = "+arg1.getAction());
if(arg1.getAction() == BluetoothDevice.ACTION_FOUND)//搜索到蓝牙设备
{
try{
String name = arg1.getStringExtra(BluetoothDevice.EXTRA_NAME);
BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//搜索到指定MAC地址设备
if(boundDeviceAddress.equalsIgnoreCase(device.getAddress()))
{
Log.e(TAG,"address match\n");
c2 = Calendar.getInstance();
if(adapter.isDiscovering())
adapter.cancelDiscovery();
int connectState = device.getBondState();
Log.e(TAG,"connectState : "+connectState);
switch (connectState) {
case BluetoothDevice.BOND_NONE:
Display.appendInfo("create bond...\n");
//发送配对请求
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
createBondMethod.invoke(device);
break;
case BluetoothDevice.BOND_BONDED:
Display.appendInfo("BOND_BONDED\n");
remoteDevice = device;
done.open();
break;
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(arg1.getAction() == BluetoothDevice.ACTION_BOND_STATE_CHANGED)//配对状态改变
{
BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (boundDeviceAddress.equalsIgnoreCase(device.getAddress())) {
int connectState = device.getBondState();
switch (connectState) {
case BluetoothDevice.BOND_NONE:
break;
case BluetoothDevice.BOND_BONDING:
break;
//配对完成
case BluetoothDevice.BOND_BONDED:
c3 = Calendar.getInstance();
Display.appendInfo("BOND_BONDED\n");
remoteDevice = device;
done.open();
break;
}
}
}
else if(arg1.getAction() == ACTION_PAIRING_REQUEST)//收到配对请求
{
BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(boundDeviceAddress.equalsIgnoreCase(device.getAddress()))
{
int mType = arg1.getIntExtra(EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
switch (mType) {
case PAIRING_VARIANT_PASSKEY_CONFIRMATION:
int passkey =
arg1.getIntExtra(EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
//确认进行配对
try{
Method setPairingConfirmationMethod = BluetoothDevice.class.getMethod("setPairingConfirmation",new Class[]{boolean.class});
setPairingConfirmationMethod.invoke(device,true);
}catch(Exception e)
{
e.printStackTrace();
}
break;
case PAIRING_VARIANT_PIN:
break;
default:
break;
}
}
}
}
}
蓝牙配对
最新推荐文章于 2024-07-29 08:41:12 发布