无法使用NRF库连接到BLE设备

时间:2019-02-28 06:11:04

标签: android bluetooth-lowenergy bluetooth-gatt

任何人都可以提供一个简单的示例来说明如何连接BLE设备,使用NRF库读写数据。

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

我做了什么。

  1. 我已经使用了扫描仪库并能够扫描设备。
  2. 使用Android-BLE-Library,但不确定如何将数据连接到设备和将数据写入设备以及从设备读取数据。

试图遵循https://github.com/NordicSemiconductor/Android-nRF-Blinky示例,但这是用于LED开/关和MVVM中的。

想要一些在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();
}

}

0 个答案:

没有答案