Bluetooth paired list will have list of devices which are already paired previously. Paired devices are the trusted connection established between two Bluetooth devices by "Passkey". Before calling this method please make sure that you have already enabled your Bluetooth or your device having such hardware for Bluetooth data communication described below.
Identify list of paired devices
By using the BluetoothAdapter class we got the Bonded devices and name them paired devices. Using these paired device we can easily find device name its MAC address etc.
/**
* List of paired devices.
*/
private void getListOfPairedDevices() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// getting list of devies which are already paired with our device
Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices();
if (pairedDevice.size() > 0) {
for (BluetoothDevice device : pairedDevice) {
// device name
String deviceName = device.getName();
// device mac address
String deviceHardwareAddress = device.getAddress();
Log.e(TAG, "Device name:" + deviceName + "Device Hardware/MAC Address:" + deviceHardwareAddress);
}
}
}
Verify Bluetooth feature is supported or not in your device
Below method to identify hardware availability in your device.
/**
* Verify bluetooth hardware.
*/
private void verifyBluetoothHardware() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
// bluetooth feature available
showToast("Bluetooth feature supported in this device.");
/* Now ensure that Bluetooth is enabled */
enableBluetoothFeature();
} else {
// bluetooth feature is un-available
showToast("Bluetooth feature does not supported in this device.");
}
}
Ensure Bluetooth is enabled or not
Enable bluetooth using ACTION_REQUEST_ENABLE intent this will show a dialog to Turn On bluetooth in your device and the result will be reflected on onActivityResult.
/**
* Enable bluetooth feature.
* Bluetooth enable check while working with its feature.
*/
private void enableBluetoothFeature() {
if (!bluetoothAdapter.isEnabled()) {
// make intent for enabling bluetooth on your device
bluetoothEnableIntent();
} else {
// already enabled
showToast("Bluetooth already enabled in your device.");
}
}
/**
* Bluetooth enable intent.
* Method to enable bluetooth in your device by providing intent action.
*/
private void bluetoothEnableIntent() {
Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// match your intent action with request code
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == RESULT_OK) {
// bluetooth enabled in your device
showToast("Bluetooth Enabled on your device.");
} else if (resultCode == RESULT_CANCELED) {
// error occurred
showToast("Bluetooth not enabled.");
}
}
}
0 Comment(s)