Spayker / rn-miband-connector

React-Native based connector to MiBand 3
MIT License
23 stars 14 forks source link

fetch list of available band #8

Closed Deboracgs closed 4 years ago

Deboracgs commented 4 years ago

How fetch list of available band's ? I don't want connect automatically, I wanna choose by mac address

Spayker commented 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.

Deboracgs commented 4 years ago

Thank you for you fast reply! I will try follow these steps

Spayker commented 4 years ago

No problem. If you have some difficulties with that just ping me. Will help gladly.

Deboracgs commented 4 years ago

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;
    }

}
`
Deboracgs commented 4 years ago

@Spayker I try too in this way, but I receive an error

image

Spayker commented 4 years ago

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)...

Deboracgs commented 4 years ago

@Spayker Thank you very much, it will be very useful

Spayker commented 4 years ago

Just added it. You can pull changes from master and check it. There can be some minor bugs but general logic fine.

Deboracgs commented 4 years ago

thank you it's works...