I have two Android phones. I want to make an auto connection between them via Bluetooth. For example,
I have my android phone which paired to another Bluetooth. When I put these phone near together, they need to detect Bluetooth device, and automatically connect to a selected android phone (known Adress/MAC/Paired before). I don't need to connect it again. I want this kind of connectivity in my Android application.
I google and found some related reference, but they are did not solve the issue yet. I think that I need to create a thread/service to automatically connect Bluetooth when they are in range. However, I can not implement it. If you have a good solution, please let me know. Thank you
Automatically connect to paired bluetooth device when in range
Find already paired bluetooth devices automatically, when they are in range
/**
* The BroadcastReceiver that listens for discovered devices and changes the title when
* discovery is finished
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};