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.bluetoothlegatt.DeviceScanActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null)
        return;//from  ww  w  .j  a va 2s  . c  o m
    final Intent intent = new Intent(this, DeviceControlActivity.class);
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        mScanning = false;
    }
    startActivity(intent);
}

From source file:com.wolkabout.hexiwear.activity.MainActivity.java

@Receiver(actions = BluetoothDevice.ACTION_BOND_STATE_CHANGED)
void onBondStateChanged(Intent intent) {
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
    final int newBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

    Log.d(TAG, device.getName() + "(" + device.getAddress() + ") changed state: " + previousBondState + " -> "
            + newBondState);//from w w  w  .j a v a2s .c o  m
    adapter.notifyDataSetChanged();

    if (newBondState == BluetoothDevice.BOND_BONDED) {
        onBonded(device);
    } else if (previousBondState == BluetoothDevice.BOND_BONDING && newBondState == BluetoothDevice.BOND_NONE) {
        device.createBond();
    }
}

From source file:com.bluetooth.mwoolley.microbitbledemo.ui.HrmListActivity.java

@Override
public void candidateBleDevice(final BluetoothDevice device, byte[] scan_record, int rssi) {
    if (device != null && device.getName() != null && !device.getName().contains("BBC")) {
        runOnUiThread(new Runnable() {
            @Override//from   w  w w .ja va  2 s .  com
            public void run() {
                ble_device_list_adapter.addDevice(device);
                ble_device_list_adapter.notifyDataSetChanged();
                device_count++;
            }
        });
    }
}

From source file:org.apache.cordova.sipkita.BluetoothPrinter.java

void listBT(CallbackContext callbackContext) {
    BluetoothAdapter mBluetoothAdapter = null;
    String errMsg = null;/*w ww.  j  a  v a  2  s  . com*/
    try {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothPort = BluetoothPort.getInstance();
        if (mBluetoothAdapter == null) {
            errMsg = "No bluetooth adapter available";
            Log.e(LOG_TAG, errMsg);
            callbackContext.error(errMsg);
            return;
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            this.cordova.getActivity().startActivityForResult(enableBluetooth, 2);
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            JSONArray json = new JSONArray();
            for (BluetoothDevice device : pairedDevices) {
                json.put(device.getName());
            }
            callbackContext.success(json);
        } else {
            callbackContext.error("No Bluetooth Device Found");
        }
        //         Log.d(LOG_TAG, "Bluetooth Device Found: " + mmDevice.getName());
    } catch (Exception e) {
        errMsg = e.getMessage();
        Log.e(LOG_TAG, errMsg);
        e.printStackTrace();
        callbackContext.error(errMsg);
    }
}

From source file:com.android.waleed.arduino101led.DeviceScanActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null)
        return;/*from  w w w  .ja  v a  2  s.  co m*/
    final Intent intent = new Intent(this, DeviceControlActivity.class);
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        mScanning = false;
    }

    startActivity(intent);
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeBluetoothStack.java

@RequiresPermission(Manifest.permission.BLUETOOTH)
public NativeBluetoothStack(@NonNull Context applicationContext, @NonNull ErrorListener errorListener,
        @NonNull LoggerFacade logger) {//  ww w . j av a 2 s.c  o m
    this.applicationContext = applicationContext;
    this.errorListener = errorListener;
    this.logger = logger;

    this.bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    this.adapter = bluetoothManager.getAdapter();
    if (adapter != null) {
        final BroadcastReceiver powerStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (newState == BluetoothAdapter.STATE_ON) {
                    enabled.onNext(true);
                } else if (newState == BluetoothAdapter.STATE_OFF || newState == BluetoothAdapter.ERROR) {
                    enabled.onNext(false);
                }
            }
        };
        applicationContext.registerReceiver(powerStateReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        enabled.onNext(adapter.isEnabled());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final BroadcastReceiver pairingReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    final Intent broadcast = new Intent(ACTION_PAIRING_REQUEST);
                    broadcast.putExtra(GattPeripheral.EXTRA_NAME, device.getName());
                    broadcast.putExtra(GattPeripheral.EXTRA_ADDRESS, device.getAddress());
                    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
                }
            };
            applicationContext.registerReceiver(pairingReceiver,
                    new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
        }
    } else {
        logger.warn(LOG_TAG, "Host device has no bluetooth hardware!");
        enabled.onNext(false);
    }
}

From source file:org.chromium.ChromeBluetooth.java

private JSONObject getBasicDeviceInfo(BluetoothDevice device) throws JSONException {
    JSONObject deviceInfo = new JSONObject();
    deviceInfo.put("address", device.getAddress());
    deviceInfo.put("name", device.getName());
    deviceInfo.put("deviceClass", device.getBluetoothClass().getDeviceClass());
    deviceInfo.put("paired", device.getBondState() == BluetoothDevice.BOND_BONDED);
    deviceInfo.put("connected", isConnected(device));
    deviceInfo.put("uuids", new JSONArray(getUuidStringsFromDevice(device)));
    return deviceInfo;
}

From source file:fr.bmartel.android.notti.service.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
public void init(Context context) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager) context
            .getSystemService(Context.BLUETOOTH_SERVICE);

    mBluetoothAdapter = bluetoothManager.getAdapter();

    //init message handler
    mHandler = null;/* w  ww. ja  va2 s  .c o  m*/
    mHandler = new Handler();

    scanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) {

            if (device.getAddress() != null && device.getName() != null
                    && !scanningList.containsKey(device.getAddress())) {

                scanningList.put(device.getAddress(), device);

                try {
                    JSONObject object = new JSONObject();
                    object.put("address", device.getAddress());
                    object.put("deviceName", device.getName());

                    ArrayList<String> deviceInfo = new ArrayList<>();
                    deviceInfo.add(object.toString());

                    broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCOVERED, deviceInfo);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

From source file:activities.GatewayActivity.java

@Override
public void WATCHiTSwitchClicked(boolean on) {
    if (on) {//from w  w  w. j a va 2s .com
        if (sApp.currentActiveSpace == null) {
            showToast(getString(R.string.register_event_first));
            configFragment.updateWATCHiTView(false);
            return;
        }
        if (!btAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
            sApp.isWATChiTOn = false;
            return;
        } else {
            sApp.service.start();
            //sApp.isWATChiTOn = true;
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
            arrayAdapter = new ArrayList<String>();
            devices = new ArrayList<BluetoothDevice>();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    arrayAdapter.add("Name: " + device.getName() + "\n" + getString(R.string.adress) + " :"
                            + device.getAddress());
                    devices.add(device);
                    //sApp.arrayAdapter.add()
                }
                sApp.bluetoothDevices = devices;
            }
            Bundle b = new Bundle();
            b.putStringArrayList("adapter", arrayAdapter);
            ChooseBlueToothDeviceDialog dialog = new ChooseBlueToothDeviceDialog();
            dialog.setArguments(b);
            dialog.show(getSupportFragmentManager(), "pairedbluetoothdevices");
        }
    }
    if (!on) {
        if (sApp.service.isRunning())
            sApp.service.stop();
        showToast(getString(R.string.toast_stopped_watchit_service));
        sApp.isWATChiTOn = false;
    }
    configFragment.updateWATCHiTView(sApp.isWATChiTOn);
}

From source file:com.xiaocheng.xc_application.bluetoothutils.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_bluetooth_list);

    //getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    //???//from   w  w  w  .  ja  v  a  2 s .  c  om
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //??
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                /*MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION*/0);
        //?? ????
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
            Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();
        }
    }

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        /* Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
         finish();
         return;*/
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

    }

    lv_bleList = (ListView) findViewById(R.id.lv_bleList);

    mDevListAdapter = new DeviceListAdapter();
    lv_bleList.setAdapter(mDevListAdapter);

    lv_bleList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mDevListAdapter.getCount() > 0) {

                BluetoothDevice device1 = mDevListAdapter.getItem(position);
                if (device1 == null)
                    return;
                Intent intent1 = new Intent();
                intent1.putExtra(EXTRAS_DEVICE_NAME, device1.getName());
                intent1.putExtra(EXTRAS_DEVICE_ADDRESS, device1.getAddress());
                if (mScanning) {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    mScanning = false;
                }

                // setResult  intent data Activity
                setResult(Activity.RESULT_OK, intent1);
                //? Activity
                finish();
                //                    startActivity(intent1);
            }
        }
    });
}