Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
genofire
donkey
Commits
a9a9d954
Commit
a9a9d954
authored
Aug 06, 2019
by
Anon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add dedicated send activity
parent
a68b812c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
160 additions
and
133 deletions
+160
-133
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
app/src/main/java/com/github/antweb/donkey/MainActivity.kt
app/src/main/java/com/github/antweb/donkey/MainActivity.kt
+1
-126
app/src/main/java/com/github/antweb/donkey/ScanActivity.kt
app/src/main/java/com/github/antweb/donkey/ScanActivity.kt
+15
-7
app/src/main/java/com/github/antweb/donkey/SendActivity.kt
app/src/main/java/com/github/antweb/donkey/SendActivity.kt
+133
-0
app/src/main/res/layout/activity_send.xml
app/src/main/res/layout/activity_send.xml
+10
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
a9a9d954
...
...
@@ -18,6 +18,7 @@
</activity>
<activity
android:name=
".ScanActivity"
/>
<activity
android:name=
".SendActivity"
/>
</application>
<uses-permission
android:name=
"android.permission.BLUETOOTH"
/>
...
...
app/src/main/java/com/github/antweb/donkey/MainActivity.kt
View file @
a9a9d954
package
com.github.antweb.donkey
import
android.bluetooth.*
import
android.content.Context
import
android.content.Intent
import
androidx.appcompat.app.AppCompatActivity
import
android.os.Bundle
import
android.os.Handler
import
android.util.Log
import
android.widget.Button
import
android.widget.TextView
import
java.lang.NullPointerException
import
androidx.appcompat.app.AppCompatActivity
private
const
val
TAG
=
"MainActivity"
class
MainActivity
:
AppCompatActivity
()
{
private
val
targetDeviceName
=
"card10"
private
val
mtu
=
128
private
val
serviceUuid
=
"00422342-2342-2342-2342-234223422342"
private
val
dataCharacteristicUuid
=
"01422342-2342-2342-2342-234223422342"
private
val
bluetoothAdapter
:
BluetoothAdapter
?
by
lazy
(
LazyThreadSafetyMode
.
NONE
)
{
val
bluetoothManager
=
getSystemService
(
Context
.
BLUETOOTH_SERVICE
)
as
BluetoothManager
bluetoothManager
.
adapter
}
private
var
bluetoothGatt
:
BluetoothGatt
?
=
null
private
var
fileService
:
BluetoothGattService
?
=
null
private
var
dataCharacteristic
:
BluetoothGattCharacteristic
?
=
null
private
var
mGatt
:
BluetoothGatt
?
=
null
private
var
fileTransferService
:
FileTransfer
?
=
null
private
var
mScanning
:
Boolean
=
false
private
var
connected
=
false
private
lateinit
var
tvConnection
:
TextView
private
lateinit
var
tvValue
:
TextView
private
lateinit
var
buttonConnect
:
Button
...
...
@@ -48,109 +24,8 @@ class MainActivity : AppCompatActivity() {
buttonConnect
=
findViewById
(
R
.
id
.
button_connect
)
buttonConnect
.
setOnClickListener
{
// scanLeDevice()
val
intent
=
Intent
(
this
,
ScanActivity
::
class
.
java
)
startActivity
(
intent
)
}
}
private
fun
scanLeDevice
()
{
val
gattCallback
=
object
:
BluetoothGattCallback
()
{
override
fun
onServicesDiscovered
(
gatt
:
BluetoothGatt
?,
status
:
Int
)
{
super
.
onServicesDiscovered
(
gatt
,
status
)
if
(
gatt
==
null
)
{
throw
NullPointerException
()
}
for
(
service
in
gatt
.
services
)
{
Log
.
d
(
TAG
,
"Found service: ${service.uuid}"
)
if
(
service
.
uuid
.
toString
()
==
serviceUuid
)
{
fileService
=
service
}
for
(
characteristic
in
service
.
characteristics
)
{
Log
.
d
(
TAG
,
"Characteristic: ${characteristic.uuid}"
)
if
(
characteristic
.
uuid
.
toString
()
==
dataCharacteristicUuid
)
{
dataCharacteristic
=
characteristic
}
}
}
if
(
fileService
==
null
||
dataCharacteristic
==
null
)
{
Log
.
e
(
TAG
,
"Could not find file transfer service"
)
return
}
gatt
.
requestMtu
(
mtu
)
}
override
fun
onConnectionStateChange
(
gatt
:
BluetoothGatt
?,
status
:
Int
,
newState
:
Int
)
{
super
.
onConnectionStateChange
(
gatt
,
status
,
newState
)
when
(
newState
)
{
BluetoothGatt
.
STATE_CONNECTED
->
{
runOnUiThread
{
tvConnection
.
text
=
"STATE_CONNECTED"
}
mGatt
=
gatt
gatt
?.
discoverServices
()
}
BluetoothGatt
.
STATE_DISCONNECTED
->
tvConnection
.
text
=
"STATE_DISCONNECTED"
BluetoothGatt
.
STATE_CONNECTING
->
tvConnection
.
text
=
"STATE_CONNECTING"
BluetoothGatt
.
STATE_DISCONNECTING
->
tvConnection
.
text
=
"STATE_DISCONNECTING"
}
}
override
fun
onMtuChanged
(
gatt
:
BluetoothGatt
?,
mtu
:
Int
,
status
:
Int
)
{
Log
.
d
(
TAG
,
"MTU changed to: $mtu"
)
runOnUiThread
{
tvValue
.
text
=
"MTU: $mtu"
}
val
lData
=
dataCharacteristic
if
(
gatt
!=
null
&&
lData
!=
null
)
{
fileTransferService
=
FileTransfer
(
gatt
,
lData
,
mtu
)
fileTransferService
?.
sendFile
()
}
}
override
fun
onReliableWriteCompleted
(
gatt
:
BluetoothGatt
?,
status
:
Int
)
{
// Last chunk sent successfully. Send next chunk
fileTransferService
?.
sendNext
()
}
override
fun
onCharacteristicWrite
(
gatt
:
BluetoothGatt
?,
characteristic
:
BluetoothGattCharacteristic
?,
status
:
Int
)
{
gatt
?.
executeReliableWrite
()
}
}
val
leScanCallback
=
BluetoothAdapter
.
LeScanCallback
{
device
,
_
,
_
->
if
(
device
.
name
==
targetDeviceName
)
{
if
(!
connected
)
{
connected
=
true
bluetoothGatt
=
device
.
connectGatt
(
this
,
true
,
gattCallback
,
BluetoothDevice
.
TRANSPORT_LE
)
}
}
}
// Stops scanning after a pre-defined period
Handler
().
postDelayed
({
mScanning
=
false
bluetoothAdapter
?.
stopLeScan
(
leScanCallback
)
},
5000
)
mScanning
=
true
bluetoothAdapter
?.
startLeScan
(
leScanCallback
)
}
}
app/src/main/java/com/github/antweb/donkey/ScanActivity.kt
View file @
a9a9d954
...
...
@@ -14,6 +14,11 @@ private const val TAG = "ScanActivity"
class
ScanActivity
:
AppCompatActivity
()
{
// TODO: Figure out how to transfer this later
companion
object
{
var
selectedDevice
:
BluetoothDevice
?
=
null
}
private
lateinit
var
listView
:
ListView
private
lateinit
var
listAdapter
:
DeviceListAdapter
...
...
@@ -32,6 +37,16 @@ class ScanActivity : AppCompatActivity() {
listAdapter
=
DeviceListAdapter
(
applicationContext
)
listView
.
adapter
=
listAdapter
listView
.
setOnItemClickListener
{
adapterView
,
view
,
i
,
l
->
val
item
=
adapterView
.
adapter
.
getItem
(
i
)
as
?
BluetoothDevice
if
(
item
!=
null
)
{
selectedDevice
=
item
val
intent
=
Intent
(
this
,
SendActivity
::
class
.
java
)
startActivity
(
intent
)
}
}
checkPermissions
()
scan
()
}
...
...
@@ -47,13 +62,6 @@ class ScanActivity : AppCompatActivity() {
val
foundDevices
=
mutableSetOf
<
BluetoothDevice
>()
val
leScanCallback
=
BluetoothAdapter
.
LeScanCallback
{
device
,
_
,
_
->
// if (device.name == targetDeviceName) {
// if (!connected) {
// connected = true
// bluetoothGatt = device.connectGatt(this, true, gattCallback, BluetoothDevice.TRANSPORT_LE)
// }
// }
if
(!
foundDevices
.
contains
(
device
))
{
foundDevices
.
add
(
device
)
listAdapter
.
add
(
device
)
...
...
app/src/main/java/com/github/antweb/donkey/SendActivity.kt
0 → 100644
View file @
a9a9d954
package
com.github.antweb.donkey
import
android.bluetooth.*
import
android.os.Bundle
import
android.util.Log
import
android.widget.TextView
import
androidx.appcompat.app.AppCompatActivity
import
com.github.antweb.donkey.file.FileTransfer
import
java.lang.NullPointerException
import
java.util.*
private
const
val
TAG
=
"SendActivity"
class
SendActivity
:
AppCompatActivity
()
{
private
val
mtu
=
128
private
val
serviceUuid
=
"00422342-2342-2342-2342-234223422342"
private
val
centralTxCharacteristicUuid
=
UUID
.
fromString
(
"01422342-2342-2342-2342-234223422342"
)
private
val
centralRxCharacteristicUuid
=
UUID
.
fromString
(
"02422342-2342-2342-2342-234223422342"
)
private
var
bluetoothGatt
:
BluetoothGatt
?
=
null
private
var
fileService
:
BluetoothGattService
?
=
null
private
var
mGatt
:
BluetoothGatt
?
=
null
private
var
fileTransferService
:
FileTransfer
?
=
null
private
lateinit
var
tvConnection
:
TextView
private
lateinit
var
tvValue
:
TextView
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_send
)
tvValue
=
findViewById
(
R
.
id
.
text_value
)
tvConnection
=
findViewById
(
R
.
id
.
text_connection_status
)
tvConnection
.
text
=
"STATE_DISCONNECTED"
val
device
=
ScanActivity
.
selectedDevice
if
(
device
!=
null
)
{
connect
(
device
)
}
else
{
Log
.
e
(
TAG
,
"Device is NULL!"
)
}
}
fun
connect
(
device
:
BluetoothDevice
)
{
val
gattCallback
=
object
:
BluetoothGattCallback
()
{
override
fun
onServicesDiscovered
(
gatt
:
BluetoothGatt
?,
status
:
Int
)
{
if
(
gatt
==
null
)
{
throw
NullPointerException
()
}
for
(
service
in
gatt
.
services
)
{
Log
.
d
(
TAG
,
"Found service: ${service.uuid}"
)
if
(
service
.
uuid
.
toString
()
==
serviceUuid
)
{
fileService
=
service
}
}
if
(
fileService
==
null
)
{
Log
.
e
(
TAG
,
"Could not find file transfer service"
)
return
}
gatt
.
requestMtu
(
mtu
)
}
override
fun
onConnectionStateChange
(
gatt
:
BluetoothGatt
?,
status
:
Int
,
newState
:
Int
)
{
when
(
newState
)
{
BluetoothGatt
.
STATE_CONNECTED
->
{
runOnUiThread
{
tvConnection
.
text
=
"STATE_CONNECTED"
}
mGatt
=
gatt
gatt
?.
discoverServices
()
}
BluetoothGatt
.
STATE_DISCONNECTED
->
tvConnection
.
text
=
"STATE_DISCONNECTED"
BluetoothGatt
.
STATE_CONNECTING
->
tvConnection
.
text
=
"STATE_CONNECTING"
BluetoothGatt
.
STATE_DISCONNECTING
->
tvConnection
.
text
=
"STATE_DISCONNECTING"
}
}
override
fun
onCharacteristicChanged
(
gatt
:
BluetoothGatt
?,
characteristic
:
BluetoothGattCharacteristic
?)
{
super
.
onCharacteristicChanged
(
gatt
,
characteristic
)
}
override
fun
onCharacteristicRead
(
gatt
:
BluetoothGatt
?,
characteristic
:
BluetoothGattCharacteristic
?,
status
:
Int
)
{
super
.
onCharacteristicRead
(
gatt
,
characteristic
,
status
)
}
override
fun
onMtuChanged
(
gatt
:
BluetoothGatt
?,
mtu
:
Int
,
status
:
Int
)
{
Log
.
d
(
TAG
,
"MTU changed to: $mtu"
)
runOnUiThread
{
tvValue
.
text
=
"MTU: $mtu"
}
val
tx
=
fileService
?.
getCharacteristic
(
centralTxCharacteristicUuid
)
val
rx
=
fileService
?.
getCharacteristic
(
centralRxCharacteristicUuid
)
if
(
gatt
!=
null
&&
tx
!=
null
&&
rx
!=
null
)
{
val
notifySuccess
=
gatt
.
setCharacteristicNotification
(
rx
,
true
)
if
(!
notifySuccess
)
{
Log
.
e
(
TAG
,
"Notify enable failed"
)
}
// fileTransferService = FileTransfer("/Download/SEND.txt", mtu)
// fileTransferService?.sendFile()
}
}
override
fun
onCharacteristicWrite
(
gatt
:
BluetoothGatt
?,
characteristic
:
BluetoothGattCharacteristic
?,
status
:
Int
)
{
// Thread.sleep(3000)
// fileTransferService?.sendNext()
// return
}
}
bluetoothGatt
=
device
.
connectGatt
(
this
,
true
,
gattCallback
,
BluetoothDevice
.
TRANSPORT_LE
)
}
}
\ No newline at end of file
app/src/main/res/layout/activity_send.xml
View file @
a9a9d954
...
...
@@ -4,4 +4,14 @@
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/text_connection_status"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/text_value"
/>
</LinearLayout>
\ No newline at end of file
Write
Preview
Markdown
is supported
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