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())
{
if(adapter.enable())
{
done.block();
done.close();
}
SystemClock.sleep(delayTime);
}
Display.appendInfo("start discovery...\n");
adapter.startDiscovery();
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) {
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) {
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);
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;
}
}
}
}
}