@@ -6,25 +6,21 @@ import android.bluetooth.BluetoothDevice
66import android.bluetooth.BluetoothGatt
77import android.bluetooth.BluetoothGattCallback
88import android.bluetooth.BluetoothGattCharacteristic
9- import android.bluetooth.BluetoothGattDescriptor
109import android.bluetooth.BluetoothManager
1110import android.bluetooth.BluetoothProfile
1211import android.bluetooth.le.ScanCallback
1312import android.bluetooth.le.ScanFilter
1413import android.bluetooth.le.ScanResult
1514import android.bluetooth.le.ScanSettings
1615import android.content.Context
17- import android.os.Build
1816import androidx.annotation.RequiresPermission
1917import androidx.appcompat.app.AppCompatActivity
2018import hmju.http.tracking_interceptor.TrackingDataManager
2119import hmju.tracking.hardware.HardwareTrackingModel
2220import timber.log.Timber
23- import java.nio.ByteBuffer
2421import java.text.SimpleDateFormat
2522import java.util.Locale
2623import java.util.UUID
27- import java.util.concurrent.Executors
2824
2925
3026class BleTestProvider (
@@ -36,7 +32,13 @@ class BleTestProvider(
3632 open fun onConnected (gatt : BluetoothGatt ) {}
3733 open fun onDisconnected (gatt : BluetoothGatt ) {}
3834 open fun onServicesDiscovered (gatt : BluetoothGatt ) {}
39- open fun onCharacteristicRead (gatt : BluetoothGatt , value : ByteArray ) {}
35+ open fun onCharacteristicRead (
36+ gatt : BluetoothGatt ,
37+ characteristic : BluetoothGattCharacteristic ,
38+ value : ByteArray
39+ ) {
40+ }
41+
4042 open fun onCharacteristicChanged (gatt : BluetoothGatt , value : ByteArray ) {}
4143
4244 val origin = object : BluetoothGattCallback () {
@@ -65,7 +67,7 @@ class BleTestProvider(
6567 status : Int
6668 ) {
6769 if (gatt == null || characteristic == null ) return
68- onCharacteristicRead(gatt, characteristic.value)
70+ onCharacteristicRead(gatt, characteristic, characteristic .value)
6971 }
7072
7173 override fun onCharacteristicRead (
@@ -74,7 +76,7 @@ class BleTestProvider(
7476 value : ByteArray ,
7577 status : Int
7678 ) {
77- onCharacteristicRead(gatt, value)
79+ onCharacteristicRead(gatt, characteristic, value)
7880 }
7981
8082 @Suppress(" DEPRECATION" )
@@ -97,15 +99,11 @@ class BleTestProvider(
9799 }
98100 }
99101
100- private val connectionSet = mutableSetOf<String >()
101102 private val manager: BluetoothManager by lazy {
102103 activity.getSystemService(Context .BLUETOOTH_SERVICE ) as BluetoothManager
103104 }
104105
105106 private val adapter: BluetoothAdapter by lazy { manager.adapter }
106- private val dateFmt: SimpleDateFormat by lazy {
107- SimpleDateFormat (" HH:mm:ss.SSS" , Locale .getDefault())
108- }
109107
110108 @SuppressLint(" MissingPermission" )
111109 fun startBleAdv () {
@@ -165,39 +163,20 @@ class BleTestProvider(
165163 }
166164 }
167165 }
168- Timber .d(" characteristic μ°Ύμ΅λλ€..$characteristic " )
169166 if (characteristic == null ) return
170- val writeBytes = ByteBuffer .allocate(2 )
171- .putShort(10000 .toShort())
172- .array() + ByteBuffer .allocate(2 )
173- .putShort(10387 .toShort()).array()
174- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
175- Timber .d(" λ°μ΄ν° μ¬μ©ν©λλ€. ${writeBytes.contentToString()} " )
176- gatt.writeCharacteristic(
177- characteristic,
178- writeBytes,
179- BluetoothGattCharacteristic .WRITE_TYPE_DEFAULT
180- )
181- Executors .newSingleThreadExecutor().submit {
182- Thread .sleep(300 )
183- gatt.setCharacteristicNotification(characteristic, true )
184- val descriptor = characteristic.getDescriptor(
185- UUID .fromString(" 00002902-0000-1000-8000-00805f9b34fb" )
186- )
187- gatt.writeDescriptor(
188- descriptor,
189- BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE
190- )
191- }
192- }
167+ gatt.readCharacteristic(characteristic)
193168 }
194169
195- override fun onCharacteristicRead (gatt : BluetoothGatt , value : ByteArray ) {
170+
171+ override fun onCharacteristicRead (
172+ gatt : BluetoothGatt ,
173+ characteristic : BluetoothGattCharacteristic ,
174+ value : ByteArray
175+ ) {
196176 TrackingDataManager .getInstance().add(HardwareTrackingModel (gatt, value))
197177 }
198178
199179 override fun onCharacteristicChanged (gatt : BluetoothGatt , value : ByteArray ) {
200- Timber .d(" onCharacteristicChanged ${value.contentToString()} " )
201180 TrackingDataManager .getInstance().add(HardwareTrackingModel (gatt, value))
202181 }
203182 }
0 commit comments