Skip to content

Instantly share code, notes, and snippets.

View pritam-quantum-inventions's full-sized avatar

Pritam Dasgupta pritam-quantum-inventions

View GitHub Profile
@pritam-quantum-inventions
pritam-quantum-inventions / Connect_disconnect_device.kt
Created September 26, 2021 10:07
method fpr connecting and disconnecting device
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
@pritam-quantum-inventions
pritam-quantum-inventions / bluetooth_viewmodel.dart
Last active October 26, 2021 04:54
send and receive data from and to kotlin class
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 {
@pritam-quantum-inventions
pritam-quantum-inventions / Discover Devices
Last active September 26, 2021 09:56
find the bluetooth devices available
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) {
@pritam-quantum-inventions
pritam-quantum-inventions / BluetoothAdapterEnable
Last active September 26, 2021 09:45
enable bluetooth adapter
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) {
@pritam-quantum-inventions
pritam-quantum-inventions / MethodNames
Last active September 26, 2021 12:12
get method names passed from dart
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)