Commit f137076e authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

UpdateClockJob, ScannerFragment: Apply Bluetooth MAC filter for CA:4D:10 prefix.

parent 33d02845
......@@ -24,6 +24,8 @@ package de.ccc.events.badge.card10
import java.util.*
const val CARD10_BLUETOOTH_MAC_PREFIX = "CA:4D:10"
val GENERIC_ACCESS_SERVICE_UUID = UUID.fromString("00001800-0000-1000-8000-00805f9b34fb")
val DEVICE_NAME_CHARACTERISTIC_UUID = UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb")
......
......@@ -29,6 +29,7 @@ import android.bluetooth.BluetoothGattCharacteristic
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
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.TIME_CHARACTERISTIC_UUID
import java.nio.ByteBuffer
......@@ -41,55 +42,57 @@ class UpdateClockJob(context: Context, workerParameters: WorkerParameters) : Wor
override fun doWork(): Result {
System.out.println("=== doWork() job started")
for (device in bluetoothAdapter.bondedDevices) {
System.out.println("=== doWork() ${device}")
var gatt: BluetoothGatt? = null
try {
val gattLatch = CountDownLatch(1);
var writeLatch: CountDownLatch? = null
var timeCharacteristic: BluetoothGattCharacteristic? = null
val callback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
System.out.println("=== doWork() onConnectionStateChange " + gatt + " / " + status + " / " + newState)
if (newState == BluetoothGatt.STATE_CONNECTED)
gatt.discoverServices()
}
if (device.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true)) {
System.out.println("=== doWork() ${device}")
var gatt: BluetoothGatt? = null
try {
val gattLatch = CountDownLatch(1);
var writeLatch: CountDownLatch? = null
var timeCharacteristic: BluetoothGattCharacteristic? = null
val callback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
System.out.println("=== doWork() onConnectionStateChange " + gatt + " / " + status + " / " + newState)
if (newState == BluetoothGatt.STATE_CONNECTED)
gatt.discoverServices()
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
System.out.println("=== doWork() onServicesDiscovered " + gatt + " / " + status)
val card10Service = gatt.getService(CARD10_SERVICE_UUID)
timeCharacteristic = card10Service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
gattLatch.countDown()
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
System.out.println("=== doWork() onServicesDiscovered " + gatt + " / " + status)
val card10Service = gatt.getService(CARD10_SERVICE_UUID)
timeCharacteristic = card10Service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
gattLatch.countDown()
}
override fun onCharacteristicWrite(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?,
status: Int
) {
writeLatch?.countDown()
override fun onCharacteristicWrite(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?,
status: Int
) {
writeLatch?.countDown()
}
}
}
gatt = device.connectGatt(applicationContext, true, callback)
if (!gattLatch.await(1, TimeUnit.MINUTES))
return Result.retry()
gatt = device.connectGatt(applicationContext, true, callback)
if (!gattLatch.await(1, TimeUnit.MINUTES))
return Result.retry()
val buffer = ByteBuffer.allocate(8)
buffer.putLong(System.currentTimeMillis())
timeCharacteristic?.value = buffer.array()
if (gatt.writeCharacteristic(timeCharacteristic)) {
writeLatch = CountDownLatch(1)
if (!writeLatch.await(1, TimeUnit.MINUTES))
return Result.failure()
System.out.println("=== doWork() updated time")
val buffer = ByteBuffer.allocate(8)
buffer.putLong(System.currentTimeMillis())
timeCharacteristic?.value = buffer.array()
if (gatt.writeCharacteristic(timeCharacteristic)) {
writeLatch = CountDownLatch(1)
if (!writeLatch.await(1, TimeUnit.MINUTES))
return Result.failure()
System.out.println("=== doWork() updated time")
}
} catch (x: Exception) {
x.printStackTrace()
} finally {
gatt?.close();
}
} catch (x: Exception) {
x.printStackTrace()
} finally {
gatt?.close();
System.out.println("=== doWork() job finished")
}
}
System.out.println("=== doWork() job finished (success)")
return Result.success()
}
}
......@@ -40,7 +40,7 @@ import java.util.concurrent.CountDownLatch
class MoodFragment : Fragment() {
// TODO pick from list of paired card10s, possibly present selection
private val REMOTE_DEVICE_BLUETOOTH_MAC = "00:05:8B:44:02:03"
private val REMOTE_DEVICE_BLUETOOTH_MAC = "CA:4D:10:44:02:03"
private lateinit var gatt: BluetoothGatt
private var rocketsCharacteristic: BluetoothGattCharacteristic? = null
......
......@@ -36,6 +36,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.R
import kotlinx.android.synthetic.main.scanner_fragment.*
......@@ -47,15 +48,15 @@ class ScannerFragment : Fragment() {
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
val device = result.device
if (device.name == null)
return
listAdapter.put(
Device(
btMac = device.address,
name = device.name,
paired = device.bondState == BluetoothDevice.BOND_BONDED
if (device.address.startsWith(CARD10_BLUETOOTH_MAC_PREFIX, true)) {
listAdapter.put(
Device(
btMac = device.address,
name = device.name,
paired = device.bondState == BluetoothDevice.BOND_BONDED
)
)
)
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment