DolDurma
DolDurma

Reputation: 17380

Flutter Checking enabled USB debugging programmatical

In Android we can check USB Debugging with this code easily

if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) == 1) {
    // debugging enabled 
} else {
    //;debugging does not enabled
}

Now my question is how can we check that in Flutter? i can't find any sample code for implementing it.

Upvotes: 0

Views: 1596

Answers (1)

Jaydeep chatrola
Jaydeep chatrola

Reputation: 2721

you can use platform channel like below

import android.os.Bundle;
import android.provider.Settings;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    private String CHANNEL = "adb";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        new MethodChannel(getFlutterView(),CHANNEL).setMethodCallHandler((call, result) -> {
            if (call.method.equals("checkingadb")) {
                checkingadb(call,result);
            } else {
                result.notImplemented();
            }
        });
    }

    private void checkingadb(MethodCall call, MethodChannel.Result result) {
        if (Settings.Secure.getInt(this.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) == 1) {
            // debugging enabled
            result.success(1);
        } else {
            //;debugging does not enabled
            result.success(0);
        }
    }
}

in flutter side

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const platform = const MethodChannel('adb');
  String isenable = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(isenable),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          checkadb();
        },
      ),
    );
  }

  checkadb() async {
    try {
      final int result = await platform.invokeMethod('checkingadb');
      print(result);
      if (result == 1) {
        setState(() {
          isenable = 'it is enabled';
        });
      } else {
        isenable = 'it is not enabled';
      }
    } on PlatformException catch (e) {
      print(e.message);
    }
  }
}

Upvotes: 2

Related Questions