Example usage for android.bluetooth BluetoothDevice getName

List of usage examples for android.bluetooth BluetoothDevice getName

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice getName.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getName() 

Source Link

Document

Get the friendly Bluetooth name of the remote device.

Usage

From source file:com.example.android.bluetoothchat.DeviceListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ??/*from  w ww.  j  av a2 s .  c o  m*/
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_device_list);

    // ????????????CANCELED
    setResult(Activity.RESULT_CANCELED);

    // ????????
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });

    //???????
    Button startchatButton = (Button) findViewById(R.id.button_start_chat);
    startchatButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            connect();
        }
    });

    // array adapters??.
    // ??????????????
    ArrayAdapter<String> pairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // ????ListView?
    pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedListView.setAdapter(pairedDevicesArrayAdapter);
    pairedListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    // ????????ListView?
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    // ????
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // ???
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // local Bluetooth adapter??
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // ????
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

    // ????????ArrayAdapter??
    if (pairedDevices.size() > 0) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice device : pairedDevices) {
            pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    } else {
        String noDevices = getResources().getText(R.string.none_paired).toString();
        pairedDevicesArrayAdapter.add(noDevices);
    }
}

From source file:io.github.msc42.masterthemaze.SearchDeviceActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    lockScreenOrientation();//w  w w . j  ava 2  s  .c o m

    setContentView(R.layout.activity_search_device);

    mSearchNoteTextView = (TextView) findViewById(R.id.searchNoteTextView);

    mBluetoothDevicesListView = (ListView) findViewById(R.id.bluetoothDevicesListView);

    mBluetoothDevicesListView
            .setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDevicesList));

    mBluetoothDevicesListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (mBluetoothAdapter != null) {
                mBluetoothAdapter.cancelDiscovery();
            }
            BluetoothDevice device = mDevices.get(position);
            startMainActivity(device.getName(), device.getAddress());
        }
    });

    if (startBluetooth()) {
        searchDevices();
    }
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.MyHealthHubGlassMainActivity.java

private void openSelectDeviceDialog(String sensorType) {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
        BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices()
                .toArray(new BluetoothDevice[0]);

        int mDeviceIndex = 0;
        if (mAllBondedDevices.length > 0) {
            int deviceCount = mAllBondedDevices.length;
            String[] deviceNames = new String[deviceCount];
            int i = 0;
            for (BluetoothDevice device : mAllBondedDevices) {
                deviceNames[i++] = device.getName() + "|" + device.getAddress();
            }/*from  ww  w. j a v a2s.co m*/
            DialogFragment deviceDialog = new SelectDeviceDialogFragment();
            Bundle args = new Bundle();
            args.putString("sensorType", sensorType);
            args.putStringArray("names", deviceNames);
            args.putInt("position", mDeviceIndex);
            args.putBoolean("device", true);
            deviceDialog.setArguments(args);
            getFragmentManager().beginTransaction();
            deviceDialog.show(getSupportFragmentManager().beginTransaction(), "deviceDialog");
        }
    }
}

From source file:org.debian.paulliu.kajiradoubra.controller.KajiraDouBraController.java

@Override
public void onResume() {
    super.onResume();
    java.util.Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    mBluetoothDevice = null;/*www  .j a  va2s.c o  m*/
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            if (device.getName().compareTo("KajiraDouBra") == 0) {
                mBluetoothDevice = device;
                Log.i(LOGTAG, "BT Mac: " + device.getAddress());
                break;
            }
        }
    }
    if (mBluetoothDevice != null) {
        try {
            mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID);
        } catch (java.io.IOException e) {
            Log.w(LOGTAG, "Cannot get BluetoothSocket");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        mBluetoothAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(LOGTAG, "Connecting to Device...");
        try {
            mBluetoothSocket.connect();
            Log.d(LOGTAG, "Connection established and data link opened");
        } catch (java.io.IOException e) {
            try {
                mBluetoothSocket.close();
            } catch (java.io.IOException e2) {
                Log.e(LOGTAG, "In onResume() and unable to close socket during connection failure"
                        + e2.getMessage() + ".");
            }
        }
    }
}

From source file:com.ahmedabdelmeged.bluetoothmc.ui.BluetoothDevices.java

/**
 * get the paired devices in the phone// ww w. j  av  a 2 s  . c  om
 */
private void PairedDevicesList() {
    pairedDevices = mBluetoothAdapter.getBondedDevices();

    if (pairedDevices.size() > 0) {
        for (BluetoothDevice bt : pairedDevices) {
            //Get the device's name and the address
            bluetoothDevicesAdapter.addDevice(bt.getName() + "\n" + bt.getAddress());
        }
    } else {
        Toast.makeText(getApplicationContext(), R.string.no_paired_devices, Toast.LENGTH_LONG).show();
    }
}

From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java

/**
 * ???/*  w w  w . j a  v  a2  s . co m*/
 *
 * @return PluginResult
 */
private PluginResult listBoundBluetooth() {
    Set<BluetoothDevice> pairedDevices = mBtadapter.getBondedDevices();
    JSONArray devicesBound = new JSONArray();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                JSONObject boundDevice = new JSONObject();
                try {
                    boundDevice.put(DEVICE_NAME, device.getName());
                    boundDevice.put(DEVICE_MACADDRESSG, device.getAddress());
                    devicesBound.put(boundDevice);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    return new PluginResult(PluginResult.Status.OK, devicesBound);
}

From source file:com.rosterloh.moodring.profile.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    Log.i(TAG, "Service started");

    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();
    onServiceStarted();/*  w  w w .  j  a va 2  s  .  com*/

    Log.v(TAG, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}

From source file:io.github.msc42.masterthemaze.SearchDeviceActivity.java

private void showBoundedDevices() {
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    for (BluetoothDevice device : pairedDevices) {
        if (!mDevices.contains(device)) {
            mDevices.add(device);/* w  ww.j a va 2s  .c om*/
            mDevicesList.add(device.getName() + "\n" + device.getAddress());
            mBluetoothDevicesListView.setAdapter(
                    new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDevicesList));
        }
    }
}

From source file:com.example.RITW.Ble.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
    //      mLogSession = Logger.openSession(getApplicationContext(), logUri);
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    //      Logger.i(mLogSession, "Service started");

    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();
    onServiceStarted();//from  w w w.  j a  v  a2 s  .  c o  m

    //      Logger.v(mLogSession, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}

From source file:no.android.proxime.profile.BleProfileService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
        throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");

    final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
    mLogSession = Logger.openSession(getApplicationContext(), logUri);
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    Logger.i(mLogSession, "Service started");

    // notify user about changing the state to CONNECTING
    final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
    broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
    LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    final BluetoothAdapter adapter = bluetoothManager.getAdapter();
    final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
    mDeviceName = device.getName();
    onServiceStarted();//from  w  ww . j a  v a  2s .c om

    Logger.v(mLogSession, "Connecting...");
    mBleManager.connect(BleProfileService.this, device);
    return START_REDELIVER_INTENT;
}