feat: connect to device
This commit is contained in:
@@ -5,7 +5,11 @@ import android.annotation.SuppressLint
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
|
import android.bluetooth.BluetoothDevice
|
||||||
|
import android.bluetooth.BluetoothGatt
|
||||||
|
import android.bluetooth.BluetoothGattCallback
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
|
import android.bluetooth.BluetoothProfile
|
||||||
import android.bluetooth.le.ScanCallback
|
import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
import android.bluetooth.le.ScanSettings
|
import android.bluetooth.le.ScanSettings
|
||||||
@@ -72,11 +76,56 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var kirbyScanResults = ArrayList<ScanResult>();
|
private val kirbyScanResults = ArrayList<ScanResult>();
|
||||||
|
|
||||||
|
private val gattCallback = object : BluetoothGattCallback() {
|
||||||
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||||
|
val deviceAddress = gatt.device.address
|
||||||
|
val deviceName = gatt.device.name
|
||||||
|
|
||||||
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
|
Log.i(
|
||||||
|
"BluetoothGattCallback",
|
||||||
|
"Successfully connected to $deviceAddress (${deviceName})"
|
||||||
|
)
|
||||||
|
// TODO: Store a reference to BluetoothGatt
|
||||||
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
|
Log.w(
|
||||||
|
"BluetoothGattCallback",
|
||||||
|
"Successfully disconnected from $deviceAddress (${deviceName})"
|
||||||
|
)
|
||||||
|
gatt.close()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.w(
|
||||||
|
"BluetoothGattCallback",
|
||||||
|
"Error $status encountered for $deviceAddress (${deviceName})! Disconnecting..."
|
||||||
|
)
|
||||||
|
gatt.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val scanResultAdapter: ScanResultAdapter by lazy {
|
private val scanResultAdapter: ScanResultAdapter by lazy {
|
||||||
ScanResultAdapter(kirbyScanResults) {
|
ScanResultAdapter(kirbyScanResults) { scanResult ->
|
||||||
// TODO: Implement
|
// User tapped on a scan result
|
||||||
|
if (isScanning) {
|
||||||
|
stopBleScan()
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(
|
||||||
|
"ConnectCallback",
|
||||||
|
"Start connecting to ${scanResult.device} (${scanResult.device.name})"
|
||||||
|
)
|
||||||
|
|
||||||
|
scanResult.device.connectGatt(
|
||||||
|
applicationContext,
|
||||||
|
false,
|
||||||
|
gattCallback,
|
||||||
|
BluetoothDevice.TRANSPORT_LE
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user