11package com.example.autodoorctrl.autodoorctrlandroid
22
33import android.Manifest
4+ import android.app.Activity
45import android.content.Intent
56import android.content.pm.PackageManager
67import androidx.appcompat.app.AppCompatActivity
@@ -12,6 +13,7 @@ import androidx.core.app.ActivityCompat
1213import androidx.core.content.ContextCompat
1314import android.location.Location
1415import android.os.Looper
16+ import android.util.Log
1517import com.google.android.gms.maps.CameraUpdateFactory
1618import com.google.android.gms.maps.GoogleMap
1719import com.google.android.gms.maps.OnMapReadyCallback
@@ -25,14 +27,23 @@ import com.google.android.gms.location.LocationCallback
2527import com.google.android.gms.location.LocationSettingsRequest
2628import com.google.android.gms.location.LocationRequest
2729import okhttp3.Callback
30+ import android.content.Context
2831import okhttp3.OkHttpClient
32+ import android.bluetooth.BluetoothAdapter
33+ import android.bluetooth.BluetoothDevice
34+ import android.bluetooth.BluetoothManager
35+ import android.bluetooth.BluetoothGatt
36+ import android.content.BroadcastReceiver
37+ import app.akexorcist.bluetotohspp.library.BluetoothState.REQUEST_ENABLE_BT
2938import okhttp3.Response
3039import org.json.JSONArray
3140import org.json.JSONException
3241import com.google.android.gms.location.FusedLocationProviderClient
3342import com.google.android.gms.location.LocationServices
3443import java.io.IOException
35-
44+ import java.util.*
45+ import kotlin.collections.ArrayList
46+ // GoogleMap.OnMarkerClickListener
3647class MapsActivity : AppCompatActivity (), OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
3748 private val updateInterval = (10 * 1000 ).toLong()
3849 private val fastInterval: Long = 2000
@@ -44,12 +55,24 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWi
4455 private lateinit var fusedLocationClient: FusedLocationProviderClient
4556 private var lastLocation: LatLng ? = null
4657 private var cameraMoved = false
58+ private var bluetoothGatt: BluetoothGatt ? = null
59+ private val myLoc = LatLng (42.7287362 ,- 73.6736838 )
60+ private val macAdress = " 88:3F:4A:E5:BE:C6"
61+ private val MY_UUID = UUID .fromString(" 0000ffe0-0000-1000-8000-00805f9b34fb" )
62+ private val bluetoothAdapter: BluetoothAdapter ? by lazy(LazyThreadSafetyMode .NONE ) {
63+ val bluetoothManager = getSystemService(Context .BLUETOOTH_SERVICE ) as BluetoothManager
64+ bluetoothManager.adapter
65+ }
66+
4767
68+ // A service that interacts with the BLE device via the Android BLE API.
69+ private lateinit var device: BluetoothDevice
70+ private val TAG = " Bluetooth"
4871 override fun onCreate (savedInstanceState : Bundle ? ) {
4972 super .onCreate(savedInstanceState)
5073 setContentView(R .layout.activity_maps)
5174 hideNavBar()
52-
75+ device = bluetoothAdapter !! .getRemoteDevice(macAdress)
5376 // Obtain the SupportMapFragment and get notified when the map is ready to be used.
5477 val mapFragment = supportFragmentManager
5578 .findFragmentById(R .id.map) as SupportMapFragment
@@ -79,6 +102,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWi
79102
80103 // add this here:
81104 val list: MutableList <LatLng > = ArrayList ()
105+
82106 request.get(url,object : Callback {
83107 override fun onResponse (call : Call , response : Response ) {
84108 try {
@@ -87,16 +111,15 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWi
87111 val item = jsonArray.getJSONObject(i)
88112 val latitude = item.getDouble(" latitude" )
89113 val longitude = item.getDouble(" longitude" )
90- val name = item.getString(" name" )
91- val tempDoor = LatLng (latitude,longitude)
92- list+ = tempDoor
93- runOnUiThread{
114+ val name = item.getString(" name" )
115+ val tempDoor = LatLng (latitude, longitude)
116+ list + = tempDoor
117+ runOnUiThread {
94118 mMap.addMarker(MarkerOptions ().position(tempDoor).title(name).snippet(" Click to open or close" ))
95119 }
96120 }
97- println (" Last location is $lastLocation " )
98121 runOnUiThread{
99- println ( " Last location is $lastLocation " )
122+ mMap.addMarker( MarkerOptions ().position(myLoc).title( " ADC " ).snippet( " Click to open or close " ) )
100123 if (lastLocation == null )
101124 {
102125 mMap.moveCamera(CameraUpdateFactory .newLatLng(list[0 ]))
@@ -117,13 +140,35 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWi
117140 }
118141 }
119142 )
120-
143+ // mMap.setOnMarkerClickListener(this)
121144 }
122145 override fun onStart () {
123146 super .onStart()
147+ if (bluetoothAdapter?.isEnabled == false ) {
148+ val enableBtIntent = Intent (BluetoothAdapter .ACTION_REQUEST_ENABLE )
149+ startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT )
150+ }
124151 startLocationUpdates()
125152 }
126-
153+ override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
154+ if (requestCode == Activity .RESULT_OK )
155+ {
156+ super .onActivityResult(requestCode, resultCode, data)
157+ }
158+ else
159+ {
160+ runOnUiThread{
161+ Toast .makeText(this ," Bluetooth is required to use this app" ,Toast .LENGTH_LONG ).show()
162+ }
163+ }
164+ }
165+ // override fun onMarkerClick(p0: Marker?): Boolean
166+ // {
167+ // println("Check1")
168+ // bluetoothGatt = device.connectGatt(this, false,BluetoothLeService(bluetoothGatt,applicationContext).gattCallback)
169+ // println("Bluetooth gat is $bluetoothGatt")
170+ // return true
171+ // }
127172 override fun onInfoWindowClick (p0 : Marker ? ) {
128173 Toast .makeText(this , " Info window clicked" ,
129174 Toast .LENGTH_SHORT ).show()
@@ -171,11 +216,17 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWi
171216 if (! cameraMoved)
172217 {
173218 lastLocation= myLocation
219+ println (" Last location is $lastLocation " )
174220 mMap.moveCamera(CameraUpdateFactory .newLatLng(myLocation))
175221 cameraMoved= true
176222 }
223+ else
224+ {
225+ lastLocation= myLocation
226+ }
177227 }
178228
229+
179230 private fun checkPermission () : Boolean {
180231 return if (ContextCompat .checkSelfPermission(this , Manifest .permission.ACCESS_FINE_LOCATION ) == PackageManager .PERMISSION_GRANTED ) {
181232 true
0 commit comments