public BluetoothDevice getRemoteDevice(String macAddress) { return bluetoothAdapter.getRemoteDevice(macAddress); }
mBTDevice = mBTAdapter.getRemoteDevice(mPeerMAC);
BluetoothDevice device = btAdapter.getRemoteDevice(btdevaddr);
@Before public void setUp() throws Exception { device1 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:AA:BB"); device2 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("11:22:33:AA:BB:00"); bluetoothHeadset = Shadow.newInstanceOf(BluetoothHeadset.class); }
@Test public void getUuids_setUuidsNotCalled_shouldReturnNull() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.getUuids()).isNull(); }
@Test @Config(minSdk = JELLY_BEAN_MR2) public void canSetAndGetType() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); shadowOf(device).setType(DEVICE_TYPE_CLASSIC); assertThat(device.getType()).isEqualTo(DEVICE_TYPE_CLASSIC); } }
@Test public void canSetAndGetCreatedBond() { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.createBond()).isFalse(); shadowOf(device).setCreatedBond(true); assertThat(device.createBond()).isTrue(); }
@Test public void canSetAndGetFetchUuidsWithSdpResult() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.fetchUuidsWithSdp()).isFalse(); shadowOf(device).setFetchUuidsWithSdpResult(true); assertThat(device.fetchUuidsWithSdp()).isTrue(); }
String[] vals = str.split("\n"); Log.e(TAG,"mac: "+vals[1]); BluetoothDevice dev = myBt.getRemoteDevice(vals[1]); mConnThread = new ConnectThread(dev); mConnThread.run();
@Test public void canSetAndGetBondState() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.getBondState()).isEqualTo(BOND_NONE); shadowOf(device).setBondState(BOND_BONDED); assertThat(device.getBondState()).isEqualTo(BOND_BONDED); }
@Test public void getCorrectFetchUuidsWithSdpCount() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(0); device.fetchUuidsWithSdp(); assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(1); device.fetchUuidsWithSdp(); assertThat(shadowOf(device).getFetchUuidsWithSdpCount()).isEqualTo(2); }
@Test public void canSetAndGetUuids() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); ParcelUuid[] uuids = new ParcelUuid[] { ParcelUuid.fromString("00000000-1111-2222-3333-000000000011"), ParcelUuid.fromString("00000000-1111-2222-3333-0000000000aa") }; shadowOf(device).setUuids(uuids); assertThat(device.getUuids()).isEqualTo(uuids); }
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress); if (mState == STATE_CONNECTING) { if (mConnectThread != null) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter == null) { // Device does not support Bluetooth finish(); //exit } if (!adapter.isEnabled()) { //make sure the device's bluetooth is enabled Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT); } final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection mac = "00:15:83:3D:0A:57"; //my laptop's mac adress device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired // Get a BluetoothSocket to connect with the given BluetoothDevice BluetoothSocket socket = null; OutputStream out = null; try { socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID); } catch (IOException e) {} try { socket.connect(); out = socket.getOutputStream(); //now you can use out to send output via out.write } catch (IOException e) {}
@Override public void run() { // always return a remote device BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mac); mBluetoothAdapter.cancelDiscovery(); mCurrStatus = STATUS.FREE; try { Log.d(TAG, "prepare to connect: " + remoteDevice.getAddress() + " " + remoteDevice.getName()); // BluetoothSocket socket = remoteDevice.createRfcommSocketToServiceRecord(UUID.fromString(STR_UUID)); // BluetoothSocket socket = (BluetoothSocket) remoteDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(remoteDevice,1); mSocket = remoteDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(Constants.STR_UUID)); // if(!socket.isConnected()) mSocket.connect(); mInputStream = mSocket.getInputStream(); mOutputStream = mSocket.getOutputStream(); mCurrStatus = STATUS.CONNECTED; } catch (Exception e) { if (listener != null) listener.onError(e); try { mInputStream.close(); mOutputStream.close(); } catch (IOException closeException) { closeException.printStackTrace(); } mCurrStatus = STATUS.FREE; } } }
// use paired devices or create a BluetoothDevice using a mac address //Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); BluetoothAdapter myAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice remoteDevice = myAdapter.getRemoteDevice("00:00:00:00:00:00"); BluetoothSockt btSocket = remoteDevice.createRfcommSocketToServiceRecord(UUID); btSocket.connect(); //get input and output stream etc...
/** * connect to a device * * @param address mac * @param secure is secure? */ public void connectToDevice(String address, boolean secure) { // Get the BluetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device mChatService.connect(device, secure); }
/** * Connect to the target device and open a socket. This method will block * while waiting for the device. * <p> * Returns a socket connected to the device. */ public BluetoothSocket connect(String targetAddress) throws BluetoothException { return connect(getDefaultAdapter().getRemoteDevice(targetAddress)); }
byte[] data_to_write; // Your data BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SREVICE); BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter(); BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(....); BluetoothGatt mBG = mDevice.connectGatt(....); BluetoothGattService mSVC = mBG.getService(service_uuid); BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(characteristic_uuid); mCH.setValue(data_to_write); mBG.writeCharacteristic(mCH);
@Override protected void onStart() { super.onStart(); final Intent intent = getIntent(); if (!isDeviceConnected() && intent.hasExtra(FeaturesActivity.EXTRA_ADDRESS)) { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(getIntent().getByteArrayExtra(FeaturesActivity.EXTRA_ADDRESS)); onDeviceSelected(device, device.getName()); intent.removeExtra(FeaturesActivity.EXTRA_APP); intent.removeExtra(FeaturesActivity.EXTRA_ADDRESS); } }