@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_ENABLE_BT: if (resultCode == RESULT_OK) { // doDiscovery(); Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); String noDevices = getResources().getText(R.string.none_paired).toString(); mPairedDevicesArrayAdapter.add(noDevices); } } else { Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show(); finish(); } break; default: break; } }
@Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { if ((Math.abs(rssi) < mCurrentRSSI || mCurrentRSSI == 0) && (mDevice == null || !mConnected || !device.getAddress().equals(mDevice.getAddress()))) { mDevice = device; if (mDeviceTimer != null) { mDeviceTimer.cancel(); mDeviceTimer.purge(); } mDeviceTimer = new Timer(); mDeviceTimer.schedule( new TimerTask() { @Override public void run() { scanLeDevice(false); setDevice(device); } }, 1000); mCurrentRSSI = Math.abs(rssi); } }
public static PortInfo[] getPorts() { System.out.println("Starting Bluetooth Detection and Device Pairing"); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Log.w("ComPort", "Bluetooth not supported."); PortInfo[] BTDeviceSet = new PortInfo[1]; BTDeviceSet[0] = new PortInfo("", "No Devices paired :-("); return BTDeviceSet; } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); PortInfo[] BTDeviceSet = new PortInfo[pairedDevices.size()]; Log.v("ComPort", "Anzahl paired devices: " + pairedDevices.size()); // If there are paired devices if (pairedDevices.size() > 0) { // Loop through paired devices int i = 0; for (BluetoothDevice device : pairedDevices) { // Add the name and address to an array adapter to show in a // ListView Log.d( "OOBD:BluetoothIntiWorker", "Found Bluetooth Device: " + device.getName() + "=" + device.getAddress()); BTDeviceSet[i] = new PortInfo(device.getAddress(), device.getName()); i++; } } return BTDeviceSet; }
@Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; // General ListView optimization code. if (view == null) { view = mInflator.inflate(R.layout.listitem_device, null); viewHolder = new ViewHolder(); viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address); viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } BluetoothDevice device = mLeDevices.get(i); final String deviceName = device.getName(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); if (prefs.getString("last_connected_device_address", "").compareTo(device.getAddress()) == 0) { viewHolder.deviceName.setTextColor(ChartUtils.COLOR_BLUE); viewHolder.deviceAddress.setTextColor(ChartUtils.COLOR_BLUE); } viewHolder.deviceName.setText(deviceName); viewHolder.deviceAddress.setText(device.getAddress()); return view; }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // 查找到设备action if (BluetoothDevice.ACTION_FOUND.equals(action)) { // 得到蓝牙设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 如果是已配对的则略过,已得到显示,其余的在添加到列表中进行显示 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } else { // 添加到已配对设备列表 mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); // mPairedDevicesArrayAdapter.add(mBtAdapter.getRemoteDevice(device.getAddress()).getName() + "\n" + device.getAddress()); } // 搜索完成action } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); setTitle(getResources().getString(R.string.select)); if (mNewDevicesArrayAdapter.getCount() == 0) { String noDevices = getResources().getString(R.string.nonewdevice); mNewDevicesArrayAdapter.add(noDevices); } } }
private void setDevice(BluetoothDevice device) { EventBus.getDefault().post(new HeartRateEvent("Setting device: " + device.getAddress())); if (mDevice != null && device != null && !device.getAddress().equals(mDevice.getAddress()) && mBluetoothLeService != null) { mBluetoothLeService.disconnect(); } mDevice = device; if (mDevice != null && mBluetoothLeService != null) { mBluetoothLeService.connect(mDevice.getAddress()); removeTimers(); mTimer = new Timer(); mTimer.schedule( new TimerTask() { @Override public void run() { if (!mConnected) { scanLeDevice(true); } } }, SCAN_PERIOD); } }
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) { // hook into the builder to refresh the list BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = (bta != null) ? bta.getBondedDevices() : null; if (pairedDevices == null) { super.onPrepareDialogBuilder(builder); return; } CharSequence[] entries = new CharSequence[pairedDevices.size()]; CharSequence[] entryValues = new CharSequence[pairedDevices.size()]; int i = 0; for (BluetoothDevice dev : pairedDevices) { if (dev.getAddress() != null) { entries[i] = dev.getName(); if (entries[i] == null) entries[i] = "(null)"; entryValues[i] = dev.getAddress(); i++; } } setEntries(entries); setEntryValues(entryValues); super.onPrepareDialogBuilder(builder); }
@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()); // if (lastAdress.compareTo(device.getAddress())==0) // { setAndReturn(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); } } }
@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); Short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE); mNewDevicesArrayAdapter.add( device.getName() + "\n" + "RSSI = " + rssi + "\n" + device.getAddress()); // log for test if (device.getName().equals("Zhenan")) { DataLogger dl = new DataLogger(device.getName(), device.getAddress(), rssi.intValue()); } // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); setTitle(R.string.scan_finish); if (mNewDevicesArrayAdapter.getCount() == 0) { String noDevices = getResources().getText(R.string.none_found).toString(); mNewDevicesArrayAdapter.add(noDevices); } } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG, "进入广播:" + action.toString()); // 发现设备 if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, "发现设备"); devices.add(device.getName() + "\n" + device.getAddress()); Log.d(TAG, "devices:" + device.getName() + "[" + device.getAddress() + "]"); // 添加非配对设备 if (isLock(device)) { String address = device.getAddress(); devices.add(device.getName() + "\n" + device.getAddress()); Log.d(TAG, "devices" + devices); deviceList.add(device); Log.d(TAG, "deviceList" + deviceList); Log.d(TAG, "address" + address); device1 = adapter.getRemoteDevice(address); Log.d(TAG, "device1:" + device1); blueclient.nativeInit(device); } } }
@Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { if (hrClient == null) return; if (hrClientHandler == null) return; String address = device.getAddress(); if (mIsConnecting && address.equals(btDevice.getAddress())) { stopScan(); btGatt = btDevice.connectGatt(context, false, btGattCallbacks); return; } if (mScanDevices.contains(address)) return; mScanDevices.add(address); hrClientHandler.post( new Runnable() { @Override public void run() { if (mIsScanning) { // NOTE: mIsScanning in user-thread hrClient.onScanResult(NAME, device); } } }); }
public void connectTo(BluetoothDevice device) { if (device != null) { BluetoothGatt bg = device.connectGatt(context, false, btleGattCallback); gatts.put(device.getAddress(), bg); temperature.put(device.getAddress(), 0.0); humidity.put(device.getAddress(), 0.0); } else { System.out.println("null device!"); } }
public void storeLastConnectedDevice(BluetoothDevice device) { SharedPreferences.Editor editor = mContext .getSharedPreferences( DeviceManager.KNOWN_BLUETOOTH_DEVICE_PREFERENCES, Context.MODE_MULTI_PROCESS) .edit(); editor.putString(LAST_CONNECTED_BLUETOOTH_DEVICE_PREF_KEY, device.getAddress()); editor.apply(); Log.d(TAG, "Stored last connected device: " + device.getAddress()); }
public GBDevice toSupportedDevice(BluetoothDevice device) { GBDeviceCandidate candidate = new GBDeviceCandidate(device, GBDevice.RSSI_UNKNOWN); if (coordinator != null && coordinator.supports(candidate)) { return new GBDevice(device.getAddress(), device.getName(), coordinator.getDeviceType()); } for (DeviceCoordinator coordinator : getAllCoordinators()) { if (coordinator.supports(candidate)) { return new GBDevice(device.getAddress(), device.getName(), coordinator.getDeviceType()); } } return null; }
@Override public void onReceive(Context context, Intent intent) { if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { BluetoothDevice found = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Filter duplicates for (BluetoothDevice d : m_foundDevices) if (d.getAddress().equals(found.getAddress())) return; m_foundDevices.add(found); } }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { Log.d(TAG, "Item Clicked"); final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); if (device == null || device.getName() == null) return; Toast.makeText(this, R.string.connecting_to_device, Toast.LENGTH_LONG).show(); ActiveBluetoothDevice btDevice = new Select().from(ActiveBluetoothDevice.class).orderBy("_ID desc").executeSingle(); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); prefs.edit().putString("last_connected_device_address", device.getAddress()).apply(); if (btDevice == null) { ActiveBluetoothDevice newBtDevice = new ActiveBluetoothDevice(); newBtDevice.name = device.getName(); newBtDevice.address = device.getAddress(); newBtDevice.save(); } else { btDevice.name = device.getName(); btDevice.address = device.getAddress(); btDevice.save(); } if (device.getName().toLowerCase().contains("dexcom")) { if (!CollectionServiceStarter.isBTShare(getApplicationContext())) { prefs.edit().putString("dex_collection_method", "DexcomShare").apply(); prefs.edit().putBoolean("calibration_notifications", false).apply(); } if (prefs.getString("share_key", "SM00000000").compareTo("SM00000000") == 0 || prefs.getString("share_key", "SM00000000").length() < 10) { requestSerialNumber(prefs); } else returnToHome(); } else if (device.getName().toLowerCase().contains("bridge")) { if (!CollectionServiceStarter.isDexbridgeWixel(getApplicationContext())) prefs.edit().putString("dex_collection_method", "DexbridgeWixel").apply(); if (prefs.getString("dex_txid", "00000").compareTo("00000") == 0 || prefs.getString("dex_txid", "00000").length() < 5) { requestTransmitterId(prefs); } else returnToHome(); } else if (device.getName().toLowerCase().contains("drip")) { if (!(CollectionServiceStarter.isBTWixel(getApplicationContext()) || CollectionServiceStarter.isWifiandBTWixel(getApplicationContext()))) { prefs.edit().putString("dex_collection_method", "BluetoothWixel").apply(); } returnToHome(); } else { returnToHome(); } }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); if (device == null) return; if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } appState.deviceAddress = device.getAddress(); mBLE_reciv.connect(device.getAddress()); mBLE_send.connect(device.getAddress()); }
/** Connect to the picked device */ private void connect() { if (mPickedDevice == null) { showMessage( getString(R.string.error_connect, mPickedDevice.getName(), mPickedDevice.getAddress())); return; } mPickedDeviceGatt = mPickedDevice.connectGatt(this, false, mGattCallback); if (mPickedDeviceGatt == null) { showMessage( getString(R.string.error_connect, mPickedDevice.getName(), mPickedDevice.getAddress())); } }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); String name = ""; // 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 (name == null) { mArrayAdapter.add(device.getAddress()); } else { mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } }
@Override public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); // Log.d(LOG_TAG, "onCharacteristicChanged " + characteristic); byte[] dataValue = characteristic.getValue(); Log.d(LOG_TAG, "Letto:" + BleManager.bytesToHex(dataValue) + " da:" + device.getAddress()); WritableMap map = Arguments.createMap(); map.putString("peripheral", device.getAddress()); map.putString("characteristic", characteristic.getUuid().toString()); map.putString("value", BleManager.bytesToHex(dataValue)); sendEvent("BleManagerDidUpdateValueForCharacteristic", map); }
@Override public void onLeScan( final BluetoothDevice device, final int rssi, final byte[] scanRecord) { // if (true) { if (device.getName().contains("SensorTag") || device.getAddress().contains("D0")) { if (onScanListener != null) { onScanListener.onScan(device, rssi, scanRecord); } System.out.println("scan info:"); System.out.println("rssi=" + rssi); System.out.println("ScanRecord:"); for (byte b : scanRecord) System.out.printf("%02X ", b); System.out.println(""); ((Activity) context) .runOnUiThread( new Runnable() { @Override public void run() { Message msg = new Message(); BluetoothScanInfo info = new BluetoothScanInfo(); info.device = device; info.rssi = rssi; info.scanRecord = scanRecord; msg.obj = info; mHandler.sendMessage(msg); } }); } }
/** * Busca todos os dispositivos Bluetooth pareados com o device * * @param callbackContext */ protected void getBluetoothPairedDevices(CallbackContext callbackContext) { BluetoothAdapter mBluetoothAdapter = null; try { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { callbackContext.error(this.getErrorByCode(1)); return; } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); this.mCordova.getActivity().startActivityForResult(enableBluetooth, 0); } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { JSONArray json = new JSONArray(); for (BluetoothDevice device : pairedDevices) { Hashtable map = new Hashtable(); map.put("type", device.getType()); map.put("address", device.getAddress()); map.put("name", device.getName()); JSONObject jObj = new JSONObject(map); json.put(jObj); } callbackContext.success(json); } else { callbackContext.error(this.getErrorByCode(2)); } } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); e.printStackTrace(); callbackContext.error(e.getMessage()); } }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); bluetoothImage.setBackgroundResource(R.drawable.discovering_animation); anim = (AnimationDrawable) bluetoothImage.getBackground(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { discoverBT.setText(R.string.cancel_discovery); enableBT.setEnabled(false); checkBT.setEnabled(false); anim.start(); Toast.makeText(MainActivity.this, R.string.discovering, Toast.LENGTH_SHORT).show(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { discoverBT.setText(R.string.discover_devices); enableBT.setEnabled(true); checkBT.setEnabled(true); anim.stop(); bluetoothImage.setBackground(getResources().getDrawable((R.drawable.btooth_on))); Toast.makeText(MainActivity.this, R.string.discovery_finished, Toast.LENGTH_SHORT) .show(); } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (!tmpBtChecker.contains(device)) { tmpBtChecker.add(device); discoveredDevicesArList.add(device.getName() + "\n" + device.getAddress()); adapterForDiscoveredDevices.notifyDataSetChanged(); } } }
public boolean attemptToCreateNewConnection(BluetoothDevice bluetoothDevice, Context context) { try { this.bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID); Log.e(PASS, "Succeed createRfcommSocketToServiceRecord()"); } catch (IOException e) { Log.e( FAIL, "Failed to createRfcommSocketToServiceRecord() in BluetoothManager => attemptToCreateNewConnection()"); e.printStackTrace(); return false; } try { this.bluetoothSocket.connect(); createObjectStreams(); } catch (IOException e) { Log.e(NOTE, "Could not connect over BT socket"); startBTServer(); if (this.bluetoothSocket == null) return false; } sendPubKey(context); String pubKey = new Integer(getPubKey()).toString(); storeConnection(pubKey, bluetoothDevice.getName(), bluetoothDevice.getAddress()); return true; }
/** * Start the ConnectedThread to begin managing a Bluetooth connection * * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected */ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (D) Log.d(TAG, "connected"); // Cancel the thread that completed the connection if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } // Cancel any thread currently running a connection if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } // Cancel the accept thread because we only want to connect to one device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(Papandro.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(Papandro.DEVICE_NAME, device.getName()); bundle.putString(Papandro.DEVICE_ADDR, device.getAddress()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // ada device baru if (BluetoothDevice.ACTION_FOUND.equals(action)) { // ambil objek BluetoothDevice dari Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); arrayPairedDevices.add(device); // tulis nama dan MAC address ke ListView adapter.add("Baru:" + device.getName() + "\n" + device.getAddress()); // refresh listview, JANGAN LUPA!! adapter.notifyDataSetChanged(); } else // mulai proses discovery, untuk debug saja, memastikan proses dimulai if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { Toast toast = Toast.makeText( getApplicationContext(), "Mulai proses discovery", Toast.LENGTH_LONG); toast.show(); } else // mulai proses discovery, untuk debug saja, memastikan proses dimulai if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { Toast toast = Toast.makeText( getApplicationContext(), "Proses discovery selesai", Toast.LENGTH_LONG); toast.show(); } }
@Override public void onResume() { super.onResume(); // *************** checkBTState(); textView1 = (TextView) findViewById(R.id.connecting); textView1.setTextSize(40); textView1.setText(" "); // Initialize array adapter for paired devices mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); // Find and set up the ListView for paired devices ListView pairedListView = (ListView) findViewById(R.id.paired_devices); pairedListView.setAdapter(mPairedDevicesArrayAdapter); pairedListView.setOnItemClickListener(mDeviceClickListener); // Get the local Bluetooth adapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); // Get a set of currently paired devices and append to 'pairedDevices' Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); // Add previosuly paired devices to the array if (pairedDevices.size() > 0) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); // make title viewable for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { String noDevices = getResources().getText(R.string.none_paired).toString(); mPairedDevicesArrayAdapter.add(noDevices); } }
private void initializeUIElements() { TextView textViewDeviceName = (TextView) findViewById(R.id.textViewDeviceName); textViewDeviceName.setText(device.getName()); ImageView imageViewPairStatus = (ImageView) findViewById(R.id.imageViewPairStatus); switch (device.getBondState()) { case BluetoothDevice.BOND_NONE: imageViewPairStatus.setImageResource(R.drawable.bluetooth_notpaired); break; case BluetoothDevice.BOND_BONDED: imageViewPairStatus.setImageResource(R.drawable.bluetooth_paired); break; case BluetoothDevice.BOND_BONDING: imageViewPairStatus.setImageResource(R.drawable.bluetooth_bonding); break; } TextView textViewStatus = (TextView) findViewById(R.id.textViewStatus); switch (device.getBondState()) { case BluetoothDevice.BOND_BONDED: textViewStatus.setText(R.string.label_status_bonded); break; case BluetoothDevice.BOND_BONDING: textViewStatus.setText(R.string.label_status_bonding); break; case BluetoothDevice.BOND_NONE: textViewStatus.setText(R.string.label_status_not_bonded); break; } TextView textViewAddress = (TextView) findViewById(R.id.textViewAddress); textViewAddress.setText( getResources().getString(R.string.label_device_address) + " " + device.getAddress()); }
// Start device discover with the BluetoothAdapter private void doDiscovery() { if (D) Log.d(TAG, "doDiscovery()"); // Remove all element from the list mPairedDevicesArrayAdapter.clear(); // If there are paired devices, add each one to the ArrayAdapter if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else { String strNoFound = getIntent().getStringExtra("no_devices_found"); if (strNoFound == null) strNoFound = "No devices found"; mPairedDevicesArrayAdapter.add(strNoFound); } // Indicate scanning in the title String strScanning = getIntent().getStringExtra("scanning"); if (strScanning == null) strScanning = "Scanning for devices..."; setProgressBarIndeterminateVisibility(true); setTitle(strScanning); // Turn on sub-title for new devices // findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); // If we're already discovering, stop it if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); } // Request discover from BluetoothAdapter mBtAdapter.startDiscovery(); }
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) { String strNoFound = getIntent().getStringExtra("no_devices_found"); if (strNoFound == null) strNoFound = "No devices found"; if (mPairedDevicesArrayAdapter.getItem(0).equals(strNoFound)) { mPairedDevicesArrayAdapter.remove(strNoFound); } mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); String strSelectDevice = getIntent().getStringExtra("select_device"); if (strSelectDevice == null) strSelectDevice = "Select a device to connect"; setTitle(strSelectDevice); } }