Hi, In this tutorial we will learn how to get device mac address in flutter without using any plugin like get_mac.
Let’s get Started
Video Tutorial – To get mac address in flutter
Learn more on How to call android native code from flutter
So In the below code we will call android native code where we will fetch android device MAC Address.
Flutter main.dart code
Flutter code will call android platform code
In below code we have a button, when pressed call a method that will invoke android native code to get mac address for us
project > lib > main.dart
main.dart
import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), debugShowCheckedModeBanner: false , ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { // MethodChannel used to call platform independent code static const Channel = MethodChannel('com.example.getmac'); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Get MAC Address"), centerTitle: true, ), body: Center( child: ElevatedButton(onPressed: () { getmac(); },child: const Text("Flutter Get MAC Address"),), ) ); } // a method that calls android code Future<void> getmac() async { String mac = await Channel.invokeMethod('getMAC'); print(mac); } }
Android Kotlin code
Android native will get invoked from flutter code, when the user make a call, In our case we can calling android code to get mac address.
project > Android > app > src > main > kotlin
MainActivity.kt
package com.example.backpress import android.util.Log import android.widget.Toast import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel import java.lang.reflect.Method import java.net.NetworkInterface import java.util.* class MainActivity: FlutterActivity() { private val CHANNEL = "com.example.getmac"; private lateinit var channel: MethodChannel override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,CHANNEL) channel.setMethodCallHandler { call, result -> if(call.method == "getMAC"){ var mac = getMacAddress(); result.success(mac); Toast.makeText(this, mac, Toast.LENGTH_LONG).show() } } } // a function that return mac private fun getMacAddress(): String? { try { val all: List<NetworkInterface> = Collections.list<NetworkInterface>(NetworkInterface.getNetworkInterfaces()) for (nif in all) { if (!nif.getName().equals("wlan0", ignoreCase = true)) continue val macBytes: ByteArray = nif.getHardwareAddress() ?: return "" val res1 = StringBuilder() for (b in macBytes) { res1.append(String.format("%02X:", b)) } if (res1.length > 0) { res1.deleteCharAt(res1.length - 1) } return res1.toString() } } catch (ex: java.lang.Exception) { } return "02:00:00:00:00:00" } }
Note: Since Android 6 & IOS 7, Apple & Google has turned off the access to programmatically get device MAC Address, Therefore if you try to get mac address you will always get response as 02:00:00:00:00:00
.
Please read below Saying of Google & Apple:
Apple Saying’s
In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02:00:00:00:00:00
. If you need to identify the device, use the identifierForVendor property of UIDevice instead. (Apps that need an identifier for their own advertising purposes should consider using the advertisingIdentifier property of ASIdentifierManager instead.)
Google Saying’s
To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00
.