Skip to content

Instantly share code, notes, and snippets.

@pritam-quantum-inventions
Created September 26, 2021 10:07
Show Gist options
  • Save pritam-quantum-inventions/ff220dfb62a705151a3b58f1611fafab to your computer and use it in GitHub Desktop.
Save pritam-quantum-inventions/ff220dfb62a705151a3b58f1611fafab to your computer and use it in GitHub Desktop.
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
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