5

Can anybody provide an simple example how to connect BLE device, Read and write data using NRF library.

https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library https://github.com/NordicSemiconductor/Android-BLE-Library

What I have done.

  1. I have used the scanner library and able to scan the devices.
  2. Using Android-BLE-Library but not sure how to connect and write data to device and read data from it.

Tried to follow the https://github.com/NordicSemiconductor/Android-nRF-Blinky example but it's for LED on/OFF and in MVVM.

Want some example for connecting part in MVC.

public class MainActivity extends AppCompatActivity {
public final String TAG = "MainActivity";

Context mContext = null;
TextView data;
String text = "";
boolean isConnecting = false;
private final List<BluetoothDevice> mDevices = new ArrayList<>();
/** Flag set to true when scanner is active. */
private boolean mScanning;
private Handler mHandler;
private final static long SCAN_DURATION = 5000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContext = this;

    final Button connect = findViewById(R.id.button);
    data = findViewById(R.id.data);
    mHandler = new Handler();

    connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                connectNow();
            }
        }
    });
}

private void connectNow() {
    if (mScanning) {
        // Extend scanning for some time more
        mHandler.removeCallbacks(mStopScanTask);
        mHandler.postDelayed(mStopScanTask, SCAN_DURATION);
        return;
    }

    BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    ScanSettings settings = new ScanSettings.Builder()
            .setLegacy(false)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .setReportDelay(1000)
            .setPhy(ScanSettings.PHY_LE_ALL_SUPPORTED)
            .build();
    List<ScanFilter> filters = new ArrayList<>();
    ScanFilter filter = new ScanFilter.Builder().setDeviceAddress("00:0B:57:47:E1:7B").build();
    filters.add(filter);
    scanner.startScan(filters, settings, mScanCallback);

    // Setup timer that will stop scanning
    mHandler.postDelayed(mStopScanTask, SCAN_DURATION);
    mScanning = true;
}

public void stopLeScan() {
    if (!mScanning)
        return;

    final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
    scanner.stopScan(mScanCallback);

    mHandler.removeCallbacks(mStopScanTask);
    mScanning = false;
}

private Runnable mStopScanTask = new Runnable() {
    @Override
    public void run() {
        MainActivity.this.stopLeScan();
    }
};

private ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(final int callbackType, final ScanResult result) {
        // empty
        Log.e("onScanResult","onScanResult : " + result.getDevice().getName() + " " + result.getDevice().getAddress());
    }

    @Override
    public void onBatchScanResults(final List<ScanResult> results) {
        final int size = mDevices.size();
        BluetoothDevice device;
        for (final ScanResult result : results) {
            device = result.getDevice();
            if (!mDevices.contains(device))
                mDevices.add(device);

            if (size != mDevices.size()) {
                data.setText(data.getText() +"\n"+ device.getAddress());
            }
        }

    }

    @Override
    public void onScanFailed(final int errorCode) {
        // empty
    }
};

@Override
protected void onPause() {
    super.onPause();
    stopLeScan();
}

}

  • it is not MVVM or MVC-specific, just use the official-documented way to connect and read/write – Vladyslav Matviienko Feb 28 '19 at 07:04
  • 2
    Thanks Vladyslav Matviienko I know it's official document and they followed view model pattern and it's quite confusing to me. so if you can simplify it in single java class the connection part then it would be great help. – RakeshGupta Feb 28 '19 at 11:10
  • @RakeshGupta did u find something for connection ? – Noman Sep 14 '21 at 19:48

0 Answers0