Closed Deboracgs closed 4 years ago
My PoC does not provide such functionality unfortunately. To be honest I was focusing on establishing of work between native and react parts mostly. However I do not see serious difficulties to add it. More likely 5 steps must be done to achieve it:
1) Rewrite DeviceScanCallback class to return list of found devices when @ReactMethod public void enableBTAndDiscover(Callback successCallback)
method is called by React side;
2) Block onScanResult(int callbackType, ScanResult result)
method from auto connection to first found device;
3) Handle list of found devices in searchBluetoothDevices
function we have in bandConnector.jsx. By handling I mean: to show somehow devices on screen. Some 'connect' button can be put toward each found device item.
4) Declare a new react function that gets mac address as a parameter and transfers it to native (java) side.
5) Create '@ReactMethod public void connectByMac(String macAddress, Callback successCallback)' method in DeviceConnector.java class. It will just look for found device by provided MAC and link with one matched.
Thank you for you fast reply! I will try follow these steps
No problem. If you have some difficulties with that just ping me. Will help gladly.
I trying to implement that, but I'm not android developer, and I've difficulties with some things,
I created deviceList, but on DeviceConnector.java I really don't know how return that, I try in this way, but is not correct
final ScanCallback deviceScanCallback = new DeviceScanCallback(this, successCallback).deviceList(successCallback);
deviceScanCallback.java
`package com.sbp.bluetooth;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.util.Log;
import com.facebook.react.bridge.Callback;
import com.sbp.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class DeviceScanCallback extends ScanCallback {
class DeviceInterface {
String address = null;
String name = null;
}
private DeviceConnector deviceConnector;
private Callback successCallback;
private List<DeviceInterface> deviceList= new ArrayList<>();
DeviceScanCallback(DeviceConnector deviceConnector, Callback successCallback){
this.deviceConnector = deviceConnector;
this.successCallback = successCallback;
}
private void AddDevice(String name, String address){
DeviceInterface deviceObj = new DeviceInterface();
deviceObj.address = address;
deviceObj.name = name;
boolean alreadyAdded = deviceList.contains(deviceObj);
if(!alreadyAdded){
deviceList.add(deviceObj);
}
}
@Override
public void onScanResult(int callbackType, ScanResult result) {
String deviceName = result.getDevice().getName();
if (deviceName != null && deviceName.equalsIgnoreCase(
Objects.requireNonNull(
deviceConnector.getApplicationContext())
.getString(R.string.mi_band_device_name_3))){
Log.d("TAG", "Device found " + result.getDevice().getAddress() +
" " + deviceName);
deviceConnector.setBluetoothDevice(result.getDevice());
deviceConnector.getBluetoothAdapter()
.getBluetoothLeScanner()
.stopScan(this);
deviceConnector.getSearchProgressDialog().dismiss();
deviceConnector.connectDevice(successCallback);
}
}
public List<DeviceInterface> deviceList(ScanResult result) {
Log.d("TAG DEBORA", "Device found " + result.getDevice().getAddress() + result.getDevice().getName());
String deviceName = result.getDevice().getName();
String deviceAddress = result.getDevice().getAddress();
if (deviceName != null && (deviceName.equalsIgnoreCase(
Objects.requireNonNull(
deviceConnector.getApplicationContext())
.getString(R.string.mi_band_device_name_3)) ||
deviceName.equalsIgnoreCase(
Objects.requireNonNull(
deviceConnector.getApplicationContext())
.getString(R.string.mi_band_device_name_4)))) {
this.AddDevice(deviceName, deviceAddress);
Log.d("TAG", "Device List " + deviceList);
}
return deviceList;
}
}
`
@Spayker I try too in this way, but I receive an error
Let me create a separate PR where the device list will be implemented. Can take couple/few days (depends on my load among other projects)...
@Spayker Thank you very much, it will be very useful
Just added it. You can pull changes from master and check it. There can be some minor bugs but general logic fine.
thank you it's works...
How fetch list of available band's ? I don't want connect automatically, I wanna choose by mac address