Skip to content

Instantly share code, notes, and snippets.

@dcalsky
Last active April 29, 2022 01:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcalsky/f151cebdfe66e4b4e1d774e497f83953 to your computer and use it in GitHub Desktop.
Save dcalsky/f151cebdfe66e4b4e1d774e497f83953 to your computer and use it in GitHub Desktop.
Bluetooth Service for Android with Kotlin
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.util.Log
import com.google.common.primitives.Bytes
import kotlinx.coroutines.*
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.*
object BluetoothService {
private const val TAG = "BTCOMService"
private val uuid: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
private lateinit var outputStream: OutputStream
private lateinit var inputStream: InputStream
private lateinit var socket: BluetoothSocket
suspend fun sendData(data: ByteArray, startBytes: ByteArray, untilBytes: ByteArray) =
coroutineScope {
withContext(Dispatchers.IO) {
outputStream.write(data)
listenData(startBytes, untilBytes = untilBytes)
}
}
public fun connected() = this::socket.isInitialized && socket.isConnected
private suspend fun listenData(
startBytes: ByteArray,
untilBytes: ByteArray
): ByteArray {
var buffer = byteArrayOf()
withContext(Dispatchers.IO) {
var startReady = false
while (true) {
val bytes = inputStream.available()
if (bytes != 0) {
var tempBuffer = ByteArray(bytes)
inputStream.read(tempBuffer)
val index = Bytes.indexOf(tempBuffer, startBytes)
if (index != -1) {
startReady = true
buffer = byteArrayOf()
tempBuffer = tempBuffer.sliceArray(index until bytes - 1)
} else if (!startReady) {
continue
}
buffer = Bytes.concat(buffer, tempBuffer)
val i = Bytes.indexOf(tempBuffer, untilBytes)
if (i != -1) {
buffer = Bytes.concat(
buffer,
tempBuffer.sliceArray(0 until i + untilBytes.size)
)
break
} else {
buffer = Bytes.concat(buffer, tempBuffer)
}
}
delay(300L)
}
}
return buffer
}
suspend fun connectDevice(device: BluetoothDevice) {
BluetoothAdapter.getDefaultAdapter()?.cancelDiscovery()
withContext(Dispatchers.IO) {
socket = device.createInsecureRfcommSocketToServiceRecord(uuid)
try {
if (socket.isConnected) {
socket.close()
}
socket.connect()
outputStream = socket.outputStream
inputStream = socket.inputStream
Log.d(TAG, socket.isConnected.toString())
} catch (e: IOException) {
// Error
}
}
}
}
@osunick
Copy link

osunick commented Jan 29, 2022

What license is this code under? I'd like to use it as a basis for connecting to an ELM327 bluetooth device for my candash project. Thanks!

@dcalsky
Copy link
Author

dcalsky commented Jan 29, 2022

@osunick No license. You can use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment