This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val CHANNEL = "bluetooth.channel" | |
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { | |
GeneratedPluginRegistrant.registerWith(flutterEngine) | |
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> | |
when (call.method) { | |
"getBlue" -> bluetoothWrapper(result) | |
"discoverBlue" -> discoverDevices(result) | |
"allPaired" -> getConnectedDevices(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun bluetoothWrapper(result: MethodChannel.Result) { | |
val defaultAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter() | |
if (defaultAdapter == null) { | |
result.error("Bluetooth adapter doesn't exist on this device", null, null) | |
} else { | |
result.success("bluetooth adapter exists on device") | |
} | |
defaultAdapter?.let { | |
val requestEnableBt = 1 | |
if (!bluetoothAdapter.isEnabled) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun discoverDevices(result: MethodChannel.Result) { | |
deviceFoundList.clear() | |
val list: MutableList<String> = arrayListOf() | |
val s = JSONObject() | |
val myAdapter = BluetoothAdapter.getDefaultAdapter() | |
myAdapter.startDiscovery() | |
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND) | |
receiver = object : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
when (intent.action) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const String CHANNEL_NAME = 'bluetooth.channel'; | |
const bluetoothPlatform = const MethodChannel(CHANNEL_NAME); | |
const String BLUETOOTH_LIST_HEADER = "Bluetooth Devices"; | |
const String INITIALIZE_BLUETOOTH_ADAPTER = "getBlue"; | |
const String CONNECT_DEVICE = "connectDevice"; | |
const String DISCONNECT_DEVICE = "disconnectSocket"; | |
const String DISCOVER_DEVICE = "discoverBlue"; | |
const String PAIRED_DEVICE = "allPaired"; | |
void initBluetooth() async { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun connectToDevice(arguments: String, result: MethodChannel.Result) { | |
val toConnectDevice = JSONObject(arguments) | |
var device: BluetoothDevice? = null | |
if (deviceFoundList.isNotEmpty()) { | |
deviceBondedList.addAll(deviceFoundList) | |
} | |
if(deviceBondedList.isNotEmpty()) { | |
for (btDevice in deviceBondedList) { | |
if(toConnectDevice.getString("address").equals(btDevice.address, ignoreCase = true)) { | |
device = btDevice |