1
+ package edu.temple.vehiclecollisiondetection
2
+
3
+ import android.Manifest
4
+ import android.app.Activity
5
+ import android.bluetooth.BluetoothAdapter
6
+ import android.bluetooth.BluetoothGatt
7
+ import android.bluetooth.BluetoothManager
8
+ import android.bluetooth.le.ScanCallback
9
+ import android.bluetooth.le.ScanResult
10
+ import android.bluetooth.le.ScanSettings
11
+ import android.content.Context
12
+ import android.content.Intent
13
+ import android.content.pm.PackageManager
14
+ import android.os.Build
15
+ import android.util.Log
16
+ import android.widget.TextView
17
+ import android.widget.Toast
18
+ import androidx.annotation.RequiresApi
19
+ import androidx.core.app.ActivityCompat
20
+ import androidx.core.app.ActivityCompat.requestPermissions
21
+ import androidx.core.content.ContextCompat.startActivity
22
+
23
+ class BluetoothRunnable (currentContext : Context , currentActivity : Activity , connectionText : TextView ): Runnable{
24
+
25
+ val activeContext = currentContext
26
+ val activeActivity = currentActivity
27
+ val activeTextView = connectionText
28
+ @RequiresApi(Build .VERSION_CODES .S )
29
+ override fun run () {
30
+ hasPermissions()
31
+ var btAdapter: BluetoothAdapter ? = null
32
+
33
+ // ask user to turn on bluetooth if it isn't already
34
+ if (btAdapter?.isEnabled == false ) {
35
+ val enableBtIntent = Intent (BluetoothAdapter .ACTION_REQUEST_ENABLE )
36
+ activeContext.startActivity(enableBtIntent)
37
+ }
38
+
39
+ val bluetoothManager =
40
+ activeContext.getSystemService(Context .BLUETOOTH_SERVICE ) as BluetoothManager ?
41
+
42
+ btAdapter = bluetoothManager?.adapter
43
+
44
+
45
+ val gattCallback = MyBluetoothGattCallback (activeContext, activeActivity, activeTextView)
46
+ val bluetoothLeScanner = btAdapter?.bluetoothLeScanner
47
+ val scanCallback = object : ScanCallback () {
48
+ override fun onScanResult (callbackType : Int , result : ScanResult ) {
49
+ if (ActivityCompat .checkSelfPermission(
50
+ activeContext,
51
+ Manifest .permission.BLUETOOTH_SCAN
52
+ ) != PackageManager .PERMISSION_GRANTED
53
+ ) {
54
+ requestPermissions(activeActivity, arrayOf(Manifest .permission.BLUETOOTH_SCAN ), 10 )
55
+ // return
56
+ }
57
+ if (result.device.name != null ){
58
+ Log .d(" Device Found: " , result.device.name)
59
+ }
60
+ // Check if the scan result matches the target device UUID
61
+ if (result.device.address.equals(" E6:EC:C4:09:52:F0" )) {
62
+ Log .d(" tag" , " FOUND BLE DEVICE" )
63
+ activeActivity.runOnUiThread(Runnable () {
64
+ Toast .makeText(activeContext, " Device Found" , Toast .LENGTH_LONG ).show()
65
+ })
66
+
67
+ // Stop scanning
68
+ bluetoothLeScanner?.stopScan(this )
69
+ // Connect to the device
70
+ val device = result.device
71
+ var gatt: BluetoothGatt ? = null
72
+ gatt = device?.connectGatt(activeContext, false , gattCallback)
73
+ }
74
+ }
75
+ }
76
+ // Create a ScanSettings to control the scan parameters
77
+ val scanSettings = ScanSettings .Builder ()
78
+ .setScanMode(ScanSettings .SCAN_MODE_LOW_LATENCY )
79
+ .setMatchMode(ScanSettings .MATCH_MODE_AGGRESSIVE )
80
+ .build()
81
+ // Start scanning for devices that match the scan filter
82
+ Log .d(" tag" , " LOOKING FOR BLE DEVICE" )
83
+ activeActivity.runOnUiThread(Runnable () {
84
+ Toast .makeText(activeContext, " Scanning for Device" , Toast .LENGTH_LONG ).show()
85
+ })
86
+ bluetoothLeScanner?.startScan(null , scanSettings, scanCallback)
87
+ }
88
+ @RequiresApi(Build .VERSION_CODES .S )
89
+ private fun hasPermissions (): Boolean {
90
+ if (activeContext.checkSelfPermission(Manifest .permission.ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
91
+ requestPermissions(
92
+ activeActivity, arrayOf(Manifest .permission.ACCESS_COARSE_LOCATION ),11 )
93
+ }
94
+ if (activeContext.checkSelfPermission(
95
+ Manifest .permission.BLUETOOTH_CONNECT
96
+ ) != PackageManager .PERMISSION_GRANTED
97
+ ) {
98
+ requestPermissions( activeActivity, arrayOf(Manifest .permission.BLUETOOTH_CONNECT ), 12 )
99
+ }
100
+ if (activeContext.checkSelfPermission(
101
+ Manifest .permission.BLUETOOTH_SCAN
102
+ ) != PackageManager .PERMISSION_GRANTED
103
+ ) {
104
+ requestPermissions( activeActivity, arrayOf(Manifest .permission.BLUETOOTH_SCAN ), 13 )
105
+ }
106
+ if (activeContext.checkSelfPermission(Manifest .permission.ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
107
+ requestPermissions(
108
+ activeActivity, arrayOf(Manifest .permission.ACCESS_FINE_LOCATION ),14 )
109
+ }
110
+ if (activeContext.checkSelfPermission(Manifest .permission.CALL_PHONE ) != PackageManager .PERMISSION_GRANTED ) {
111
+ requestPermissions(
112
+ activeActivity, arrayOf(Manifest .permission.CALL_PHONE ),15 )
113
+ }
114
+ if (activeContext.checkSelfPermission(Manifest .permission.SEND_SMS ) != PackageManager .PERMISSION_GRANTED ) {
115
+ requestPermissions(
116
+ activeActivity, arrayOf(Manifest .permission.SEND_SMS ),16 )
117
+ }
118
+ return true
119
+ }
120
+ }
0 commit comments