Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Stefan Zabka
Companion App Android
Commits
0374d7a1
Commit
0374d7a1
authored
Aug 22, 2019
by
Anon
Browse files
Fix listener handling.
parent
7cff04ec
Changes
5
Show whitespace changes
Inline
Side-by-side
app/src/main/java/de/ccc/events/badge/card10/common/ConnectionService.kt
View file @
0374d7a1
...
...
@@ -42,7 +42,7 @@ object ConnectionService {
private
var
connection
:
BluetoothGatt
?
=
null
private
var
connectionState
=
BluetoothGatt
.
STATE_DISCONNECTED
private
var
gattListeners
=
mutable
ListOf
<
GattListener
>()
private
var
gattListeners
=
mutable
MapOf
<
String
,
GattListener
>()
private
val
fileServiceUuid
=
UUID
.
fromString
(
"42230100-2342-2342-2342-234223422342"
)
...
...
@@ -58,8 +58,8 @@ object ConnectionService {
fun
isConnected
()
=
connectionState
==
BluetoothGatt
.
STATE_CONNECTED
fun
addGattListener
(
listener
:
GattListener
)
{
gattListeners
.
add
(
listener
)
fun
addGattListener
(
tag
:
String
,
listener
:
GattListener
)
{
gattListeners
[
tag
]
=
listener
}
fun
connect
(
context
:
Context
)
{
...
...
@@ -113,8 +113,6 @@ object ConnectionService {
connectionState
=
newState
connection
=
gatt
gattListeners
.
map
{
it
.
onConnectionStateChange
(
newState
)
}
when
(
newState
)
{
BluetoothGatt
.
STATE_CONNECTED
->
{
gatt
?.
discoverServices
()
...
...
@@ -132,6 +130,8 @@ object ConnectionService {
mtu
=
newMtu
-
3
// Very precise science
leService
?.
enableNotify
(
gatt
)
gattListeners
.
values
.
map
{
it
.
onConnectionReady
()
}
}
override
fun
onCharacteristicWrite
(
...
...
@@ -145,7 +145,7 @@ object ConnectionService {
connection
=
gatt
gattListeners
.
map
{
it
.
onCharacteristicWrite
(
characteristic
,
status
)
}
gattListeners
.
values
.
map
{
it
.
onCharacteristicWrite
(
characteristic
,
status
)
}
}
override
fun
onCharacteristicChanged
(
gatt
:
BluetoothGatt
?,
characteristic
:
BluetoothGattCharacteristic
?)
{
...
...
@@ -155,7 +155,7 @@ object ConnectionService {
throw
IllegalStateException
()
}
gattListeners
.
map
{
it
.
onCharacteristicChanged
(
characteristic
)
}
gattListeners
.
values
.
map
{
it
.
onCharacteristicChanged
(
characteristic
)
}
}
}
...
...
app/src/main/java/de/ccc/events/badge/card10/common/GattListener.kt
View file @
0374d7a1
...
...
@@ -27,5 +27,5 @@ import android.bluetooth.BluetoothGattCharacteristic
interface
GattListener
{
fun
onCharacteristicWrite
(
characteristic
:
BluetoothGattCharacteristic
,
status
:
Int
)
{}
fun
onCharacteristicChanged
(
characteristic
:
BluetoothGattCharacteristic
)
{}
fun
onConnection
StateChange
(
state
:
Int
)
{}
fun
onConnection
Ready
(
)
{}
}
\ No newline at end of file
app/src/main/java/de/ccc/events/badge/card10/filetransfer/BatchTransferFragment.kt
View file @
0374d7a1
...
...
@@ -77,7 +77,7 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
private
fun
initConnection
()
{
val
ctx
=
context
?:
throw
IllegalStateException
()
ConnectionService
.
addGattListener
(
this
)
ConnectionService
.
addGattListener
(
"batchfiletransfer"
,
this
)
ConnectionService
.
connect
(
ctx
)
}
...
...
@@ -91,6 +91,7 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
}
private
fun
transferNext
()
{
Thread
.
sleep
(
1000
)
val
item
=
queue
.
dequeue
()
if
(
item
==
null
||
isCancelled
)
{
...
...
@@ -122,11 +123,9 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
}
}
override
fun
onConnectionStateChange
(
state
:
Int
)
{
if
(
state
==
BluetoothGatt
.
STATE_CONNECTED
)
{
override
fun
onConnectionReady
()
{
startTransfer
()
}
}
override
fun
onError
()
{
activity
?.
runOnUiThread
{
...
...
app/src/main/java/de/ccc/events/badge/card10/filetransfer/FileTransfer.kt
View file @
0374d7a1
...
...
@@ -41,7 +41,7 @@ class FileTransfer(
private
var
currentState
=
TransferState
.
IDLE
init
{
service
.
add
OnPacketReceivedListener
(
this
)
service
.
set
OnPacketReceivedListener
(
this
)
}
override
fun
onPacketReceived
(
packet
:
Packet
)
{
...
...
@@ -98,13 +98,13 @@ class FileTransfer(
throw
IllegalStateException
()
}
currentState
=
TransferState
.
START_SENT
service
.
sendPacket
(
Packet
(
PacketType
.
START
,
destinationPath
.
toByteArray
(
Charset
.
forName
(
"ASCII"
))
)
)
currentState
=
TransferState
.
START_SENT
}
private
fun
sendNext
()
{
...
...
app/src/main/java/de/ccc/events/badge/card10/filetransfer/LowEffortService.kt
View file @
0374d7a1
...
...
@@ -43,7 +43,7 @@ class LowEffortService(
private
val
centralRxCharacteristicUuid
=
UUID
.
fromString
(
"42230102-2342-2342-2342-234223422342"
)
private
var
notifyEnabled
=
false
private
va
l
listener
s
=
mutableListOf
<
OnPacketReceivedListener
>()
private
va
r
listener
:
OnPacketReceivedListener
?
=
null
init
{
val
tx
=
service
.
getCharacteristic
(
centralTxCharacteristicUuid
)
...
...
@@ -58,7 +58,7 @@ class LowEffortService(
centralTx
=
tx
centralRx
=
rx
ConnectionService
.
addGattListener
(
this
)
ConnectionService
.
addGattListener
(
"filetransfer"
,
this
)
}
fun
enableNotify
(
gatt
:
BluetoothGatt
)
{
...
...
@@ -89,8 +89,8 @@ class LowEffortService(
return
status
}
fun
add
OnPacketReceivedListener
(
l
istener
:
OnPacketReceivedListener
)
{
listener
s
.
add
(
l
istener
)
fun
set
OnPacketReceivedListener
(
packetL
istener
:
OnPacketReceivedListener
)
{
listener
=
packetL
istener
}
// GattListener methods
...
...
@@ -99,6 +99,6 @@ class LowEffortService(
}
override
fun
onCharacteristicChanged
(
characteristic
:
BluetoothGattCharacteristic
)
{
listener
s
.
map
{
it
.
onPacketReceived
(
Packet
.
fromBytes
(
characteristic
.
value
))
}
listener
?
.
onPacketReceived
(
Packet
.
fromBytes
(
characteristic
.
value
))
}
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment