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) } }