MoodFragment.kt 4.6 KB
Newer Older
Andreas Schildbach's avatar
Andreas Schildbach committed
1
2
3
/*
 * Copyright by the original author or authors.
 *
4
5
6
7
8
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
Andreas Schildbach's avatar
Andreas Schildbach committed
10
 *
11
12
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
Andreas Schildbach's avatar
Andreas Schildbach committed
13
 *
14
15
16
17
18
19
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
Andreas Schildbach's avatar
Andreas Schildbach committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 */

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.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.CARD10_SERVICE_UUID
import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.ROCKETS_CHARACTERISTIC_UUID
import kotlinx.android.synthetic.main.mood_fragment.*
import java.util.concurrent.CountDownLatch

class MoodFragment : Fragment() {

    // TODO pick from list of paired card10s, possibly present selection
43
    private val REMOTE_DEVICE_BLUETOOTH_MAC = "CA:4D:10:44:02:03"
Andreas Schildbach's avatar
Andreas Schildbach committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    private lateinit var gatt: BluetoothGatt
    private var rocketsCharacteristic: BluetoothGattCharacteristic? = null
    private var writeLatch: CountDownLatch? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val callback = object : BluetoothGattCallback() {
            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 remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(REMOTE_DEVICE_BLUETOOTH_MAC)
        gatt = remoteDevice.connectGatt(activity, false, callback)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.mood_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        mood_good.setOnClickListener({
85
            writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0xffu, 0x00u, 0x00u).toByteArray())
Andreas Schildbach's avatar
Andreas Schildbach committed
86
87
        })
        mood_neutral.setOnClickListener({
88
            writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0x00u, 0xffu, 0x00u).toByteArray())
Andreas Schildbach's avatar
Andreas Schildbach committed
89
90
        })
        mood_bad.setOnClickListener({
91
            writeGatt(rocketsCharacteristic!!, ubyteArrayOf(0x00u, 0x00u, 0xffu).toByteArray())
Andreas Schildbach's avatar
Andreas Schildbach committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        })
    }

    fun writeGatt(characteristic: BluetoothGattCharacteristic, values: ByteArray) {
        characteristic.value = values
        val init = gatt.writeCharacteristic(rocketsCharacteristic)
        if (!init)
            System.out.println("Failed to initiate writing GATT attribute")
        writeLatch = CountDownLatch(1)
        writeLatch!!.await();
    }

    override fun onDestroy() {
        gatt.close();
        super.onDestroy()
    }
}