Last active
October 26, 2021 04:54
-
-
Save pritam-quantum-inventions/12ac0822f485729c283a9f64327dcb69 to your computer and use it in GitHub Desktop.
send and receive data from and to kotlin class
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 { | |
await bluetoothPlatform.invokeMethod(INITIALIZE_BLUETOOTH_ADAPTER); | |
} | |
Future<List<BluetoothDevice>> getBondedDevices() async { | |
var devices = await bluetoothPlatform.invokeMethod(PAIRED_DEVICE); | |
if(devices != null) { | |
Iterable l = json.decode(devices.toString()); | |
return List<BluetoothDevice>.from( | |
l.map((model) => BluetoothDevice.fromJson(model))); | |
} | |
return List<BluetoothDevice>.empty(); | |
} | |
Future<List<BluetoothDevice>> getDiscoveredDevice() async { | |
var devices = await bluetoothPlatform.invokeMethod(DISCOVER_DEVICE); | |
if(devices != null) { | |
Iterable l = json.decode(devices.toString()); | |
return List<BluetoothDevice>.from( | |
l.map((model) => BluetoothDevice.fromJson(model))); | |
} | |
return List<BluetoothDevice>.empty(); | |
} | |
void connectToDevice(BluetoothDevice device) async { | |
var data = json.encode(device); | |
await bluetoothPlatform.invokeMethod(CONNECT_DEVICE, data); | |
} | |
void disconnectFromDevice() async { | |
await bluetoothPlatform.invokeMethod(DISCONNECT_DEVICE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment