FlutterBleLib icon indicating copy to clipboard operation
FlutterBleLib copied to clipboard

[Question] get Paired Devices

Open BrunoEleodoro opened this issue 5 years ago • 12 comments

Hello you all, firstly let me say that you guys did an awesome job creating this library, the example folder is helpful as well.

But I'm not sure where I can find how to get the Bonded devices (Paired ones), and how to connect to this devices?

Thanks a lot.

BrunoEleodoro avatar Feb 21 '20 18:02 BrunoEleodoro

This library uses RxAndroidBle and RxBluetoothKit. This librarires offers this feature but unfortunately I didn't find it* in FlutterBleLib... Don't sure why. EDIT: *for android

For RxBluetoothKit: https://polidea.github.io/RxBluetoothKit/Classes/CentralManager.html#/s:14RxBluetoothKit14CentralManagerC28retrieveConnectedPeripherals12withServicesSayAA10PeripheralCGSaySo6CBUUIDCG_tF

For RxAndroidBle: https://github.com/Polidea/RxAndroidBle/pull/46

For native iOS: https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals

For native Android: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getBondedDevices()

dannyalbuquerque avatar Mar 04 '20 09:03 dannyalbuquerque

The iOS method is exposed as BleManager().connectedPeripherals(). I think it may not be possible to get them on Android at the moment, but it looks doable. I'll give you an update once I get to it.

mikolak avatar Mar 04 '20 09:03 mikolak

Thank you.

@mikolak @dannyalbuquerque

BrunoEleodoro avatar Mar 04 '20 12:03 BrunoEleodoro

Ah, this explains why BleManager().connectedPeripherals() was giving me empty lists when I know it shouldn't on Android.

flutter_blue does it like this: https://github.com/pauldemarco/flutter_blue/blob/2ccdc68d474c158ae7cfbcae993e430a578173ec/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java#L173

Peetz0r avatar Mar 16 '20 23:03 Peetz0r

What about this:

https://gist.github.com/joinAero/64e68c9d63987284f7f0#file-bluetoothprofileproxy-java-L234

Ahmadre avatar Jul 12 '20 11:07 Ahmadre

I wrote a method in Java to handle fetching boundedDevices:

    @RequiresPermission(Manifest.permission.BLUETOOTH)
    private void getPairedDevices(@NonNull final MethodChannel.Result result) {
        Log.d(TAG, "Get paired devices");


        bleAdapter.getKnownDevices(deviceIdentifiers.toArray(new String[deviceIdentifiers.size()]) ,new OnSuccessCallback<Device[]>() {	        final SafeMainThreadResolver resolver = new SafeMainThreadResolver<>(new OnSuccessCallback<Device[]>() {
            @Override	            @Override
            public void onSuccess(Device[] devices) {	            public void onSuccess(Device[] devices) {
                Log.d(TAG, "Found known devices" + devices.length);	                try {
                resolver.onSuccess(devices);	                    result.success(devicesResultJsonConverter.toJson(devices));
                } catch (JSONException e) {
                    e.printStackTrace();
                    result.error(null, e.getMessage(), null);
                }
            }	            }
        }, new OnErrorCallback() {	        }, new OnErrorCallback() {
            @Override	            @Override
            public void onError(BleError error) {	            public void onError(BleError error) {
                resolver.onError(error);	                Log.e(TAG, "Get paired devices error " + error.reason + "  " + error.internalMessage);
                result.error(String.valueOf(error.errorCode.code), error.reason, bleErrorJsonConverter.toJson(error));
            }	            }
        });	        });

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        List<Device> devices = new ArrayList<>();

        for (int i = 0; i < pairedDevices.size(); i++) {
            BluetoothDevice blDevice =  pairedDevices.iterator().next();
            Device device = new Device(blDevice.getAddress(), blDevice.getName());
            devices.add(device);
        }

        Log.d(TAG, "Found paired devices" + devices.size());
        resolver.onSuccess(devices);
    }	    
}

But I am getting an error from my forked Branch:

MissingPluginException(Noimplementation found for method getPairedDevices on channel flutter_ble_lib)

Ahmadre avatar Jul 12 '20 18:07 Ahmadre

@Ahmadre You need to handle the method call in the plugin class: https://github.com/Polidea/FlutterBleLib/blob/develop/android/src/main/java/com/polidea/flutter_ble_lib/FlutterBleLibPlugin.java#L120 (same class name for iOS)

This issue has to be fixed in MultiPlatformBleAdapter, but we didn't have the time to take care of it yet.

mikolak avatar Jul 12 '20 19:07 mikolak

Are there any updates on this? I'm currently in the situation that I need to get a device that could potentially already be paired with my android phone.

ChrisMarxDev avatar Dec 02 '20 13:12 ChrisMarxDev

Sorry, no updates yet. I finally have some time for the libraries, so work on FlutterBleLib and BLEmulator has started, but I'm unable to give you any timeline for this issue right now.

mikolak avatar Dec 11 '20 09:12 mikolak

Same problem, hope update to can getting Paired Devices, thanks.

kapibara91 avatar Jan 18 '21 07:01 kapibara91

Also need this functionality

nchsoft avatar Feb 05 '21 11:02 nchsoft

any updated regarding this

tohami avatar Apr 09 '23 22:04 tohami