Skip to content

Commit

Permalink
more icons
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Dec 15, 2023
1 parent 368545e commit 19df059
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class ConnectionEventListener {
var onConnectionSetupComplete: ((BluetoothGatt) -> Unit)? = null
var onDisconnect: (() -> Unit)? = null
var onCharacteristicWrite: ((BluetoothDevice, BluetoothGattCharacteristic) -> Unit)? = null

var onBluetoothStatusChange: ((Boolean) -> Unit)? = null
}
42 changes: 35 additions & 7 deletions app/src/main/java/com.alphasync/bluetooth/ConnectionManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.RECEIVER_EXPORTED
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.lang.ref.WeakReference
Expand All @@ -21,6 +26,7 @@ class ConnectionManager(context: Context) {
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
}
private var btAdapter: BluetoothAdapter = btManager.adapter
private val btAdapterFilter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
private var btGatt: BluetoothGatt? = null
private var btGattIsConnecting: Boolean = false
private lateinit var callerContext: Context
Expand Down Expand Up @@ -58,6 +64,7 @@ class ConnectionManager(context: Context) {
btAddress = address
callerContext = context

ContextCompat.registerReceiver(context, bluetoothStateReceiver, btAdapterFilter, RECEIVER_EXPORTED)
val btDevice = btAdapter.getRemoteDevice(address)
if (btGattIsConnecting) {
Log.d(logTag, "Device is currently connecting.")
Expand All @@ -77,20 +84,25 @@ class ConnectionManager(context: Context) {
listeners.forEach { it.get()?.onDisconnect?.invoke() }
}

btGattIsConnecting = false
btGatt?.close()
btGatt = null
disconnect()

connect(btAddress, callerContext)
}

@SuppressLint("MissingPermission")
private fun disconnect() {
btGattIsConnecting = false
btGatt?.disconnect()
btGatt = null
}

@SuppressLint("MissingPermission")
fun writeCharacteristic(
characteristic: BluetoothGattCharacteristic,
payload: ByteArray
) {
characteristicId: UUID,
payload: ByteArray)
{
if (isConnected) {
btGatt?.findCharacteristic(characteristic.uuid)?.let { characteristic ->
btGatt?.findCharacteristic(characteristicId)?.let { characteristic ->
characteristic.value = payload
val initialSuccess = btGatt?.writeCharacteristic(characteristic)
if(initialSuccess!!) {
Expand All @@ -117,6 +129,21 @@ class ConnectionManager(context: Context) {
return null
}

private val bluetoothStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
if(state == BluetoothAdapter.STATE_OFF) {
disconnect()
listeners.forEach { it.get()?.onBluetoothStatusChange?.invoke(false) }
} else if (state == BluetoothAdapter.STATE_ON) {
reconnect()
listeners.forEach { it.get()?.onBluetoothStatusChange?.invoke(true) }
}
}
}
}

private val callback = object : BluetoothGattCallback() {
@SuppressLint("MissingPermission")
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
Expand All @@ -142,6 +169,7 @@ class ConnectionManager(context: Context) {
Log.d(logTag,"onConnectionStateChange: status $status encountered for $deviceAddress!")
reconnect()
}

}

@SuppressLint("MissingPermission")
Expand Down
53 changes: 36 additions & 17 deletions app/src/main/java/com.alphasync/cameralink/MyCameraLinkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MyCameraLinkService: Service() {
private var cameraName: String = ""
private lateinit var notificationManager: NotificationManagerCompat
private val notificationChannel: String = "MyCameraLinkNotificationChannel"
private val notificationConnectDisconnectId: Int = 1
private val notificationServiceStatusId: Int = 1
private val notificationGpsLostFoundId: Int = 2

fun isPairedToCamera(): Boolean {
Expand Down Expand Up @@ -92,7 +92,7 @@ class MyCameraLinkService: Service() {
.setContentText("Sending GPS to $cameraName")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationConnectDisconnectId, notificationBuilder)
notificationManager.notify(notificationServiceStatusId, notificationBuilder)
}
onGpsSignalLost = {
notificationManager.cancel(notificationGpsLostFoundId)
Expand All @@ -102,51 +102,49 @@ class MyCameraLinkService: Service() {
.setContentText("Sending GPS to $cameraName paused")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationConnectDisconnectId, notificationBuilder)
notificationManager.notify(notificationServiceStatusId, notificationBuilder)
}
onLocationReady = { location ->
val characteristic = connectionManager.characteristics.find { it.uuid.toString().contains("0000dd11")}
Log.d(logTag, "Writing to ${characteristic!!.uuid}: ${location.toHexString()}")
connectionManager.writeCharacteristic(characteristic, location)
connectionManager.writeCharacteristic(characteristic.uuid, location)
}
}
}

private val connectionEventListener by lazy {
ConnectionEventListener().apply {
onConnectionSetupComplete = { _ ->
notificationManager.cancel(notificationConnectDisconnectId)
notificationManager.cancel(notificationServiceStatusId)
val notificationBuilder = NotificationCompat.Builder(applicationContext, notificationChannel)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("$cameraName connected!")
.setContentText("Sending GPS coordinates")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationConnectDisconnectId, notificationBuilder)
notificationManager.notify(notificationServiceStatusId, notificationBuilder)

sendEnableGpsCommands()
}

onDisconnect = {
sonyCommandGenerator.stopLocationReporting(myCameraLinkEventListener)
notificationManager.cancel(notificationConnectDisconnectId)
notificationManager.cancel(notificationServiceStatusId)
val notificationBuilder = NotificationCompat.Builder(applicationContext, notificationChannel)
.setSmallIcon(R.drawable.ic_camera_disconnected)
.setContentTitle("$cameraName disconnected!")
.setContentText("GPS coordinates paused")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationConnectDisconnectId, notificationBuilder)
notificationManager.notify(notificationServiceStatusId, notificationBuilder)

tryReconnect()
}

onCharacteristicWrite = { _, sentCharacteristic: BluetoothGattCharacteristic ->
if (sentCharacteristic.uuid.toString().contains("0000dd30")) {
val characteristic = connectionManager.characteristics.find { it.uuid.toString().contains("0000dd31")}
if (characteristic != null) {
Log.d(logTag, "GPS Enable command: ${characteristic.uuid}")
connectionManager.writeCharacteristic(characteristic, "01".hexToBytes())
connectionManager.writeCharacteristic(characteristic.uuid, "01".hexToBytes())
} else {
Log.d(logTag, "GPS Enable command: Cannot find characteristic containing 0000dd31")
}
Expand All @@ -155,6 +153,28 @@ class MyCameraLinkService: Service() {
startSendingCoordinatesToDevice()
}
}
onBluetoothStatusChange = { isEnabled: Boolean ->
if(!isEnabled) {
sonyCommandGenerator.stopLocationReporting(myCameraLinkEventListener)
notificationManager.cancel(notificationServiceStatusId)
val notificationBuilder = NotificationCompat.Builder(applicationContext, notificationChannel)
.setSmallIcon(R.drawable.ic_bluetooth_disabled)
.setContentTitle("Bluetooth disabled")
.setContentText("GPS coordinates paused")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationServiceStatusId, notificationBuilder)
} else {
notificationManager.cancel(notificationServiceStatusId)
val notificationBuilder = NotificationCompat.Builder(applicationContext, notificationChannel)
.setSmallIcon(R.drawable.ic_camera_disconnected)
.setContentTitle("$cameraName disconnected!")
.setContentText("GPS coordinates paused")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationServiceStatusId, notificationBuilder)
}
}
}
}

Expand All @@ -166,14 +186,13 @@ class MyCameraLinkService: Service() {
createNotificationChannel()

val notification = NotificationCompat.Builder(this, notificationChannel)
.setContentTitle("Camera GPS link running")
.setContentText("Paired to $cameraName")
.setSmallIcon(R.drawable.ic_camera_back)
.setContentTitle("Connecting to $cameraName")
.setSmallIcon(R.drawable.ic_device_connecting)
.build()

ServiceCompat.startForeground(
this,
100,
notificationServiceStatusId,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION or ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
Expand All @@ -186,7 +205,7 @@ class MyCameraLinkService: Service() {
val characteristic = connectionManager.characteristics.find { it.uuid.toString().contains("0000dd30")}
if (characteristic != null) {
Log.d(logTag, "GPS Enable command: ${characteristic.uuid}")
connectionManager.writeCharacteristic(characteristic, "01".hexToBytes())
connectionManager.writeCharacteristic(characteristic.uuid, "01".hexToBytes())
} else {
Log.d(logTag, "GPS Enable command: Cannot find characteristic containing 0000dd31")
}
Expand Down Expand Up @@ -218,7 +237,7 @@ class MyCameraLinkService: Service() {
}

override fun onBind(intent: Intent?): IBinder? {
Log.d("MyService","Service being bound")
Log.d(logTag,"Service is being bound")
return binder
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/drawable-anydpi/ic_bluetooth_disabled.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF">
<group android:scaleX="0.92"
android:scaleY="0.92"
android:translateX="0.96"
android:translateY="0.96">
<path
android:fillColor="@android:color/white"
android:pathData="M13,5.83l1.88,1.88 -1.6,1.6 1.41,1.41 3.02,-3.02L12,2h-1v5.03l2,2v-3.2zM5.41,4L4,5.41 10.59,12 5,17.59 6.41,19 11,14.41V22h1l4.29,-4.29 2.3,2.29L20,18.59 5.41,4zM13,18.17v-3.76l1.88,1.88L13,18.17z"/>
</group>
</vector>
16 changes: 16 additions & 0 deletions app/src/main/res/drawable-anydpi/ic_device_connecting.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF"
android:autoMirrored="true">
<group android:scaleX="0.92"
android:scaleY="0.92"
android:translateX="0.96"
android:translateY="0.96">
<path
android:fillColor="@android:color/white"
android:pathData="M14.24,12.01l2.32,2.32c0.28,-0.72 0.44,-1.51 0.44,-2.33 0,-0.82 -0.16,-1.59 -0.43,-2.31l-2.33,2.32zM19.53,6.71l-1.26,1.26c0.63,1.21 0.98,2.57 0.98,4.02s-0.36,2.82 -0.98,4.02l1.2,1.2c0.97,-1.54 1.54,-3.36 1.54,-5.31 -0.01,-1.89 -0.55,-3.67 -1.48,-5.19zM15.71,7.71L10,2L9,2v7.59L4.41,5 3,6.41 8.59,12 3,17.59 4.41,19 9,14.41L9,22h1l5.71,-5.71 -4.3,-4.29 4.3,-4.29zM11,5.83l1.88,1.88L11,9.59L11,5.83zM12.88,16.29L11,18.17v-3.76l1.88,1.88z"/>
</group>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 19df059

Please sign in to comment.