Created
September 26, 2021 10:07
-
-
Save pritam-quantum-inventions/ff220dfb62a705151a3b58f1611fafab to your computer and use it in GitHub Desktop.
method fpr connecting and disconnecting device
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 | |
break | |
} | |
} | |
} | |
try { | |
socket = device?.createRfcommSocketToServiceRecord(UUID.fromString(device.uuids[0].uuid.toString())) | |
socket?.connect() | |
result.success(true) | |
} catch (e: IOException) { | |
e.printStackTrace() | |
socket?.close() | |
result.success(false) | |
} | |
} | |
private fun disconnectSocket(result: MethodChannel.Result) { | |
BluetoothAdapter.getDefaultAdapter().cancelDiscovery() | |
socket?.close()?.also { | |
result.success(true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment