I am able to connect Android TV and Android Phone App using bluetooth socket.
- Step 1 --> Searching device --> Done
- Step 2 --> Paired device using createbound --> Done
- Step 3 --> Got android tv uuid on chanel 4 and can see chanel 4 that is used for RF_COMM
- Step 5 --> I am trying to send a volume up command by using bluetoothsocket?.outputStream?.write("VolumeUp".toByteArray()) But there is no action on android tv
I am attaching the code here.
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) !=
PackageManager.PERMISSION_GRANTED
) {
return
}
Toast.makeText(this, deviceToPair.name, Toast.LENGTH_LONG).show()
bluetoothAdapter.cancelDiscovery()
try {
val paringRequired = deviceToPair.createBond()
val value = if (paringRequired) "Haa" else "Na"
Log.d("Log", "Device name " + deviceToPair.name)
Log.d("Log", "Device deviceAddress " + deviceToPair.address)
Log.d("Log", "Device deviceClass " + deviceToPair.bluetoothClass)
Log.d("Log", "Device bondState " + deviceToPair.bondState)
Log.d("Log", "Device deviceType " + deviceToPair.type)
Log.d("Log", deviceToPair.name + " ke Paring ke jarurat hai ho? " + value)
} catch (e: Exception) {
Log.d("Log", "Exception while using socket connection : " + e.message)
}
try {
val uuid = getWorkingUUIDFromDevice(deviceToPair)
if (uuid != null) {
val socket = deviceToPair.createRfcommSocketToServiceRecord(UUID.fromString(uuid))
val thread = Thread {
Thread.sleep(5000) // 5 seconds delay
socket.connect()
Log.d("Log", "Socket connect done : ")
// socket?.outputStream?.write(KeyEvent.KEYCODE_HOME)
val commandByte = AnymoteCommand.VOLUME_UP.commandName.toByteArray(Charsets.UTF_8)
Log.d("Log", "Volume up sent : ")
socket?.outputStream?.write("powerOff".toByteArray())
Log.d("Log", "Data sending to tv : ")
val inputStream = socket?.inputStream
socket?.outputStream?.flush()
Log.d("Log", "Got data from tv " + inputStream?.read())
socket.close()
}
thread.start()