Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new feature in plugin #15

Closed
dharmita1990 opened this issue Jan 30, 2019 · 1 comment
Closed

Adding new feature in plugin #15

dharmita1990 opened this issue Jan 30, 2019 · 1 comment

Comments

@dharmita1990
Copy link

dharmita1990 commented Jan 30, 2019

I'm new in flutter and wanted to add some new feature in this plugin ,

  1. Created a new eventchannel lib/flutter_bluetooth_serial.dart

static const EventChannel _discoveryChannel = const EventChannel('$namespace/discovery');

//Doubt : how to listen to List of bluetooth devices instead of single Map object
Stream<dynamic> onDiscovery() { return _discoveryChannel.receiveBroadcastStream().map((map) => BluetoothDevice.fromMap(map)); }

  1. wrote new event channel in FlutterBluetoothSerialPlugin class

`
private final StreamHandler discoveryStreamHandler = new StreamHandler() {

    List<Map<String, Object>> list = new ArrayList<>();

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("discoveryStreamHandler ","onReceive");
            final String action = intent.getAction();

            Log.d(TAG, action);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                  // Get the BluetoothDevice object from the Intent
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    // Add the name and address to an array adapter to show in a ListView

    Map<String, Object> ret = new HashMap<>();
    ret.put("address", device.getAddress());
    ret.put("name", device.getName());
    ret.put("type", device.getType());
    list.add(ret);


    // Log.d("list",list.toString());
    discoverySink.success(ret);
            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){

/* want to return list here while listening to it getting error as "List not a subtype of map<dynamic,dynamic>
*/
// discoverySink.success(list);
}
}
};

    @Override
    public void onListen(Object o, EventSink eventSink) {
        discoverySink = eventSink;

          if (ContextCompat.checkSelfPermission(registrar.activity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(registrar.activity(),
                            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                            REQUEST_COARSE_LOCATION_PERMISSIONS);

//Doubt : what shall i return here?

                }
        Log.e("discoveryStreamHandler ","onListen"+ registrar.activeContext());
        registrar.activeContext().registerReceiver(mReceiver,
        new IntentFilter(BluetoothDevice.ACTION_FOUND));
        registrar.activeContext().registerReceiver(mReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
                mBluetoothAdapter.startDiscovery();

     

       
    }

    @Override
    public void onCancel(Object o) {
        Log.d("discoveryStreamHandler", "onCancel");
        discoverySink = null;
        registrar.activity().unregisterReceiver(mReceiver);
    }
};`

Called from main.dart as
bluettoth.getdevices().listen.
I'm getting list of devices but have some doubt which i listed in code snippet. Please revert

@edufolly
Copy link
Owner

edufolly commented Feb 5, 2019

Hi, @dharmita1990

create a fork from this repository and make your own changes. After your tests, you can provide a pull request to improve this plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants