Skip to content
Snippets Groups Projects
Commit f7215a13 authored by Anon's avatar Anon
Browse files

Share common connection.

parent a4ae7683
No related branches found
No related tags found
No related merge requests found
...@@ -57,7 +57,6 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener { ...@@ -57,7 +57,6 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
inflater.inflate(R.layout.batch_transfer_fragment, container, false) inflater.inflate(R.layout.batch_transfer_fragment, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
label_status.text = getString(R.string.batch_transfer_label_initializing)
progress.max = queue.size progress.max = queue.size
button_cancel.setOnClickListener { button_cancel.setOnClickListener {
...@@ -72,13 +71,7 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener { ...@@ -72,13 +71,7 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
.commit() .commit()
} }
initConnection() startTransfer()
}
private fun initConnection() {
val ctx = context ?: throw IllegalStateException()
ConnectionService.addGattListener("batchfiletransfer", this)
ConnectionService.connect(ctx)
} }
private fun startTransfer() { private fun startTransfer() {
...@@ -123,10 +116,6 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener { ...@@ -123,10 +116,6 @@ class BatchTransferFragment : Fragment(), FileTransferListener, GattListener {
} }
} }
override fun onConnectionReady() {
startTransfer()
}
override fun onError() { override fun onError() {
activity?.runOnUiThread { activity?.runOnUiThread {
label_status.text = getString(R.string.batch_transfer_label_error) label_status.text = getString(R.string.batch_transfer_label_error)
......
...@@ -80,19 +80,12 @@ class FileTransferFragment : Fragment(), GattListener, FileTransferListener{ ...@@ -80,19 +80,12 @@ class FileTransferFragment : Fragment(), GattListener, FileTransferListener{
buttonStartStop = view.findViewById(R.id.button_start_stop_transfer) buttonStartStop = view.findViewById(R.id.button_start_stop_transfer)
try { try {
initConnection()
toggleControls() toggleControls()
} catch (e: ConnectionException) { } catch (e: ConnectionException) {
showError(e.message) showError(e.message)
} catch (e: Exception) { } catch (e: Exception) {
showError(getString(R.string.connection_error_generic)) showError(getString(R.string.connection_error_generic))
} }
}
private fun initConnection() {
val ctx = context ?: throw IllegalStateException()
ConnectionService.connect(ctx)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
......
...@@ -22,60 +22,23 @@ ...@@ -22,60 +22,23 @@
package de.ccc.events.badge.card10.mood package de.ccc.events.badge.card10.mood
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.CARD10_SERVICE_UUID
import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.ROCKETS_CHARACTERISTIC_UUID import de.ccc.events.badge.card10.common.ConnectionService
import de.ccc.events.badge.card10.time.Card10Service
import kotlinx.android.synthetic.main.mood_fragment.* import kotlinx.android.synthetic.main.mood_fragment.*
import java.util.concurrent.CountDownLatch
class MoodFragment : Fragment() { class MoodFragment : Fragment() {
private var card10Service: Card10Service? = null
private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
private lateinit var gatt: BluetoothGatt
private var rocketsCharacteristic: BluetoothGattCharacteristic? = null
private var writeLatch: CountDownLatch? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val callback = object : BluetoothGattCallback() { card10Service = ConnectionService.card10Service
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
System.out.println("===== onConnectionStateChange " + gatt + " / " + status + " / " + newState)
if (newState == BluetoothGatt.STATE_CONNECTED)
gatt.discoverServices()
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
System.out.println("===== onServicesDiscovered " + gatt + " / " + status)
val card10Service = gatt.getService(CARD10_SERVICE_UUID)
rocketsCharacteristic = card10Service.getCharacteristic(ROCKETS_CHARACTERISTIC_UUID)
}
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
System.out.println("===== onCharacteristicWrite " + characteristic.uuid.toString() + " / " + characteristic.value + " / " + status)
writeLatch?.countDown();
}
}
val remoteDevices =
bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) }
if (remoteDevices.isEmpty())
activity!!.finish()
gatt = remoteDevices.get(0).connectGatt(activity, false, callback)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
...@@ -83,28 +46,14 @@ class MoodFragment : Fragment() { ...@@ -83,28 +46,14 @@ class MoodFragment : Fragment() {
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mood_good.setOnClickListener({ mood_good.setOnClickListener{
writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0xffu, 0x00u, 0x00u).toByteArray()) card10Service?.setRocketValue(ubyteArrayOf(0xffu, 0x00u, 0x00u).toByteArray())
})
mood_neutral.setOnClickListener({
writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0x00u, 0xffu, 0x00u).toByteArray())
})
mood_bad.setOnClickListener({
writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0x00u, 0x00u, 0xffu).toByteArray())
})
} }
mood_neutral.setOnClickListener{
fun writeGatt(characteristic: BluetoothGattCharacteristic, values: ByteArray) { card10Service?.setRocketValue(ubyteArrayOf(0x00u, 0xffu, 0x00u).toByteArray())
characteristic.value = values }
val init = gatt.writeCharacteristic(rocketsCharacteristic) mood_bad.setOnClickListener{
if (!init) card10Service?.setRocketValue(ubyteArrayOf(0x00u, 0x00u, 0xffu).toByteArray())
System.out.println("Failed to initiate writing GATT attribute")
writeLatch = CountDownLatch(1)
writeLatch!!.await();
} }
override fun onDestroy() {
gatt.close();
super.onDestroy()
} }
} }
...@@ -22,84 +22,40 @@ ...@@ -22,84 +22,40 @@
package de.ccc.events.badge.card10.sparkle package de.ccc.events.badge.card10.sparkle
import android.bluetooth.*
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.CARD10_SERVICE_UUID
import de.ccc.events.badge.card10.LEDS_ABOVE_CHARACTERISTIC_UUID
import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.R
import java.util.concurrent.CountDownLatch import de.ccc.events.badge.card10.common.ConnectionService
import de.ccc.events.badge.card10.time.Card10Service
import kotlin.random.Random import kotlin.random.Random
class BeautifulFragment : Fragment(), Runnable { class BeautifulFragment : Fragment(), Runnable {
private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
private lateinit var gatt: BluetoothGatt
private var ledsAboveCharacteristic: BluetoothGattCharacteristic? = null
private @Volatile var writeLatch: CountDownLatch? = null
private val handler = Handler() private val handler = Handler()
private var card10Service: Card10Service? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val callback = object : BluetoothGattCallback() { card10Service = ConnectionService.card10Service
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
println("===== onConnectionStateChange ${gatt} ${if (status == BluetoothGatt.GATT_SUCCESS) "SUCCESS" else status} ${if (newState == BluetoothProfile.STATE_CONNECTED) "CONNECTED" else if (newState == BluetoothProfile.STATE_DISCONNECTED) "DISCONNECTED" else newState}")
if (newState == BluetoothGatt.STATE_CONNECTED)
gatt.requestMtu(64)
}
override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
println("===== onMtuChanged ${gatt} ${if (status == BluetoothGatt.GATT_SUCCESS) "SUCCESS" else status} ${mtu}")
gatt.discoverServices()
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
println("===== onServicesDiscovered ${gatt} ${if (status == BluetoothGatt.GATT_SUCCESS) "SUCCESS" else status}")
val card10Service = gatt.getService(CARD10_SERVICE_UUID)
ledsAboveCharacteristic = card10Service.getCharacteristic(LEDS_ABOVE_CHARACTERISTIC_UUID)
handler.post(this@BeautifulFragment) handler.post(this@BeautifulFragment)
} }
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
println("=== onCharacteristicWrite ${gatt} ${if (status == BluetoothGatt.GATT_SUCCESS) "SUCCESS" else status} ${characteristic.uuid} ${characteristic.value}")
writeLatch?.countDown();
}
}
val remoteDevices =
bluetoothAdapter.bondedDevices.filter { it.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true) }
if (remoteDevices.isEmpty())
activity!!.finish()
gatt = remoteDevices.get(0).connectGatt(activity, false, callback)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.beautiful_fragment, container, false) return inflater.inflate(R.layout.beautiful_fragment, container, false)
} }
override fun run() { override fun run() {
ledsAboveCharacteristic!!.value = Random.nextBytes(33); card10Service?.setLeds(Random.nextBytes(33))
writeLatch = CountDownLatch(1)
val init = gatt.writeCharacteristic(ledsAboveCharacteristic)
if (!init)
println("=== Failed to initiate writing GATT attribute")
writeLatch!!.await();
handler.postDelayed(this, 100) handler.postDelayed(this, 100)
} }
override fun onDestroy() { override fun onDestroy() {
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
gatt.close();
super.onDestroy() super.onDestroy()
} }
} }
...@@ -24,6 +24,8 @@ package de.ccc.events.badge.card10.time ...@@ -24,6 +24,8 @@ package de.ccc.events.badge.card10.time
import android.bluetooth.BluetoothGattCharacteristic import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattService import android.bluetooth.BluetoothGattService
import de.ccc.events.badge.card10.LEDS_ABOVE_CHARACTERISTIC_UUID
import de.ccc.events.badge.card10.ROCKETS_CHARACTERISTIC_UUID
import de.ccc.events.badge.card10.TIME_CHARACTERISTIC_UUID import de.ccc.events.badge.card10.TIME_CHARACTERISTIC_UUID
import de.ccc.events.badge.card10.common.ConnectionService import de.ccc.events.badge.card10.common.ConnectionService
import java.nio.ByteBuffer import java.nio.ByteBuffer
...@@ -31,11 +33,14 @@ import java.nio.ByteBuffer ...@@ -31,11 +33,14 @@ import java.nio.ByteBuffer
class Card10Service( class Card10Service(
service: BluetoothGattService service: BluetoothGattService
) { ) {
private val timeCharacteristic: BluetoothGattCharacteristic private val timeCharacteristic = service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
private val rocketsCharacteristic = service.getCharacteristic(ROCKETS_CHARACTERISTIC_UUID)
private var ledsAboveCharacteristic = service.getCharacteristic(LEDS_ABOVE_CHARACTERISTIC_UUID)
init { init {
timeCharacteristic = service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
timeCharacteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE timeCharacteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
rocketsCharacteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
ledsAboveCharacteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
} }
fun setTime() { fun setTime() {
...@@ -44,4 +49,14 @@ class Card10Service( ...@@ -44,4 +49,14 @@ class Card10Service(
timeCharacteristic.value = buffer.array() timeCharacteristic.value = buffer.array()
ConnectionService.writeCharacteristic(timeCharacteristic) ConnectionService.writeCharacteristic(timeCharacteristic)
} }
fun setRocketValue(value: ByteArray) {
rocketsCharacteristic.value = value
ConnectionService.writeCharacteristic(rocketsCharacteristic)
}
fun setLeds(value: ByteArray) {
ledsAboveCharacteristic.value = value
ConnectionService.writeCharacteristic(ledsAboveCharacteristic)
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment