Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

11 changes: 8 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 31
defaultConfig {
applicationId "com.github.roarappstudio.btkontroller"
minSdkVersion 28
targetSdkVersion 28
minSdkVersion 29
targetSdkVersion 31
versionCode 2
versionName "0.0.2"
}
Expand Down Expand Up @@ -37,10 +37,15 @@ android {
}

dependencies {

implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation "org.jetbrains.anko:anko-sdk25:$anko_version"
implementation 'com.chibatching.kotpref:kotpref:2.6.0'
implementation 'com.android.support:localbroadcastmanager:28.0.0'



}
16 changes: 12 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.github.roarappstudio.btkontroller">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Expand All @@ -22,7 +26,8 @@

<activity
android:name=".SplashScreen"
android:theme="@style/SplashStyle">
android:theme="@style/SplashStyle"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -31,6 +36,9 @@

<activity android:name=".SelectDeviceActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible"


/>

</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.github.roarappstudio.btkontroller

import android.annotation.SuppressLint
import android.bluetooth.*
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.github.roarappstudio.btkontroller.reports.FeatureReport


@SuppressLint("StaticFieldLeak")
@Suppress("MemberVisibilityCanBePrivate")
object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.ServiceListener {
object BluetoothController : BluetoothHidDevice.Callback(), BluetoothProfile.ServiceListener {

val featureReport = FeatureReport()




override fun onSetReport(device: BluetoothDevice?, type: Byte, id: Byte, data: ByteArray?) {
Log.i("setfirst","setfirst")
Log.i("setfirst", "setfirst")
super.onSetReport(device, type, id, data)
Log.i("setreport","this $device and $type and $id and $data")
Log.i("setreport", "this $device and $type and $id and $data")

}

Expand All @@ -27,16 +32,14 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
super.onGetReport(device, type, id, bufferSize)

Log.i("get", "second")
if (type == BluetoothHidDevice.REPORT_TYPE_FEATURE) {
featureReport.wheelResolutionMultiplier = true
featureReport.acPanResolutionMultiplier = true
Log.i("getbthid","$btHid")

var wasrs=btHid?.replyReport(device, type, FeatureReport.ID, featureReport.bytes)
Log.i("replysuccess flag ",wasrs.toString())
}

if (type == BluetoothHidDevice.REPORT_TYPE_FEATURE) {
featureReport.wheelResolutionMultiplier = true
featureReport.acPanResolutionMultiplier = true
Log.i("getbthid", "$btHid")

var wasrs = btHid?.replyReport(device, type, FeatureReport.ID, featureReport.bytes)
Log.i("replysuccess flag ", wasrs.toString())
}
}


Expand All @@ -45,20 +48,21 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
var hostDevice: BluetoothDevice? = null
var autoPairFlag = false

var mpluggedDevice :BluetoothDevice? = null
var mpluggedDevice: BluetoothDevice? = null



private var deviceListener: ((BluetoothHidDevice, BluetoothDevice)->Unit)? = null
private var disconnectListener: (()->Unit)? = null
private var deviceListener: ((BluetoothHidDevice, BluetoothDevice) -> Unit)? = null
private var disconnectListener: (() -> Unit)? = null

fun init(ctx: Context) {
if (btHid != null)
return
btAdapter.getProfileProxy(ctx, this, BluetoothProfile.HID_DEVICE)
this.ctx=ctx
}

fun getSender(callback: (BluetoothHidDevice, BluetoothDevice)->Unit) {
var ctx: Context? = null

fun getSender(callback: (BluetoothHidDevice, BluetoothDevice) -> Unit) {
btHid?.let { hidd ->
hostDevice?.let { host ->
callback(hidd, host)
Expand All @@ -69,8 +73,7 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
}


fun getDisconnector(callback: ()->Unit) {

fun getDisconnector(callback: () -> Unit) {
disconnectListener = callback
}

Expand All @@ -84,6 +87,7 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
btHid = null
}


override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
Log.i(TAG, "Connected to service")
if (profile != BluetoothProfile.HID_DEVICE) {
Expand All @@ -94,100 +98,80 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
val btHid = proxy as? BluetoothHidDevice
if (btHid == null) {
Log.wtf(TAG, "WTF? Proxy received but it's not BluetoothHidDevice")

return
}
this.btHid = btHid
btHid.registerApp(sdpRecord, null, qosOut, {it.run()}, this)//--
btAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 300000)



btHid.registerApp(sdpRecord, null, qosOut, { it.run() }, this)//--

}

}


/************************************************/
/** BluetoothHidDevice.Callback implementation **/
/************************************************/



override fun onConnectionStateChanged(device: BluetoothDevice?, state: Int) {
super.onConnectionStateChanged(device, state)
Log.i(TAG, "Connection state ${when(state) {
BluetoothProfile.STATE_CONNECTING -> "CONNECTING"
BluetoothProfile.STATE_CONNECTED -> "CONNECTED"
BluetoothProfile.STATE_DISCONNECTING -> "DISCONNECTING"
BluetoothProfile.STATE_DISCONNECTED -> "DISCONNECTED"

else -> state.toString()
}}")
Log.i(
TAG, "Connection state ${
when (state) {
BluetoothProfile.STATE_CONNECTING -> "CONNECTING"
BluetoothProfile.STATE_CONNECTED -> "CONNECTED"
BluetoothProfile.STATE_DISCONNECTING -> "DISCONNECTING"
BluetoothProfile.STATE_DISCONNECTED -> "DISCONNECTED"

else -> state.toString()
}
}"
)
if (state == BluetoothProfile.STATE_CONNECTED) {
if (device != null) {
hostDevice = device

deviceListener?.invoke(btHid!!, device)

//deviceListener = null
} else {
Log.e(TAG, "Device not connected")
}
} else {
hostDevice = null
if(state == BluetoothProfile.STATE_DISCONNECTED)
{
if (state == BluetoothProfile.STATE_DISCONNECTED) {
disconnectListener?.invoke()
}

}
}


override fun onAppStatusChanged(pluggedDevice: BluetoothDevice?, registered: Boolean) {
super.onAppStatusChanged(pluggedDevice, registered)
if(registered)
{
var pairedDevices = btHid?.getDevicesMatchingConnectionStates(intArrayOf(BluetoothProfile.STATE_CONNECTING,BluetoothProfile.STATE_CONNECTED,BluetoothProfile.STATE_DISCONNECTED,BluetoothProfile.STATE_DISCONNECTING))
Log.d("paired d", "paired devices are : $pairedDevices")
Log.d("paired d","${btHid?.getConnectionState(pairedDevices?.get(0))}")
mpluggedDevice = pluggedDevice
if(btHid?.getConnectionState(pluggedDevice)==0 && pluggedDevice!= null && autoPairFlag ==true)
{
btHid?.connect(pluggedDevice)
//hostDevice.toString()


}


else if(btHid?.getConnectionState(pairedDevices?.get(0))==0 && autoPairFlag==true)
{
Log.i("ddaaqq","sssS"
if (registered) {

var pairedDevices = btHid?.getDevicesMatchingConnectionStates(
intArrayOf(
BluetoothProfile.STATE_CONNECTING,
BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.STATE_DISCONNECTED,
BluetoothProfile.STATE_DISCONNECTING
)
)
Log.d("paired d", "paired devices are : $pairedDevices")
println(pairedDevices?.size)
Log.d("paired d", "${btHid?.getConnectionState(pairedDevices?.get(0))}")
mpluggedDevice = pluggedDevice
if (btHid?.getConnectionState(pluggedDevice) == 0 && pluggedDevice != null && autoPairFlag == true) {
btHid?.connect(pluggedDevice)
//hostDevice.toString()
} else if (btHid?.getConnectionState(pairedDevices?.get(0)) == 0 && autoPairFlag == true) {
Log.i(
"ddaaqq", "sssS"
)
btHid?.connect(pairedDevices?.get(0))
}

// val intent = Intent("CUSTOM_ACTION")
// intent.putExtra("DATE", Date().toString())
// Log.d("j", "sending broadcast")
//
// // send local broadcast
// LocalBroadcastManager.getInstance(this).sendBroadcast(intent)





}


}





/*************/
/** Garbage **/
/*************/
Expand All @@ -205,7 +189,6 @@ object BluetoothController: BluetoothHidDevice.Callback(), BluetoothProfile.Serv
}



private val qosOut by lazy {
BluetoothHidDeviceAppQosSettings(
BluetoothHidDeviceAppQosSettings.SERVICE_BEST_EFFORT,
Expand Down
Loading