Return the BluetoothDevice object by friendly name - Android Bluetooth

Android examples for Bluetooth:Bluetooth Device

Description

Return the BluetoothDevice object by friendly name

Demo Code


//package com.java2s;

import java.util.HashSet;
import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

public class Main {

     static BluetoothDevice getDeviceByName(
            BluetoothAdapter adapter, String name) {
        for (BluetoothDevice device : getBondedDevices(adapter)) {
            String thisDevice = device.getName();
            if (name.matches(thisDevice)) {
                return device;
            }/*from w  w w.ja va  2s  .c  o  m*/
        }
        return null;
    }

     static Set<BluetoothDevice> getBondedDevices(
            BluetoothAdapter adapter) {
        Set<BluetoothDevice> results = adapter.getBondedDevices();
        if (results == null) {
            results = new HashSet<BluetoothDevice>();
        }
        return results;
    }
}

Related Tutorials