Skip to content

Commit 3772ce5

Browse files
New Bluetooth changes
1 parent 37ba469 commit 3772ce5

5 files changed

Lines changed: 192 additions & 67 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Google Maps Android API v2, but you must specify either coarse or fine
88
location permissions for the 'MyLocation' functionality.
99
-->
10+
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
1011
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
1112
<uses-permission
1213
android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- Step 1: Add the following permission to the app -->
@@ -15,7 +16,8 @@
1516
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
1617
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
1718
<uses-permission android:name="android.permission.INTERNET"/>
18-
19+
<uses-permission android:name="android.permission.BLUETOOTH" />
20+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
1921
<uses-feature
2022
android:name="android.hardware.fingerprint"
2123
android:required="false"/>
@@ -30,7 +32,6 @@
3032
android:supportsRtl="true"
3133
android:networkSecurityConfig="@xml/network_security_config"
3234
android:theme="@style/AppTheme">
33-
3435
<uses-library
3536
android:name="org.apache.http.legacy"
3637
android:required="false"/>
@@ -66,6 +67,7 @@
6667
sign the APK for publishing.
6768
You can define the keys for the debug and release targets in src/debug/ and src/release/.
6869
-->
70+
<service android:name="BluetoothLeService" />
6971
<meta-data
7072
android:name="com.google.android.geo.API_KEY"
7173
android:value="@string/google_maps_key"/>

app/src/main/java/com/example/autodoorctrl/autodoorctrlandroid/Bluetooth.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.example.autodoorctrl.autodoorctrlandroid
2+
3+
import android.app.Activity
4+
import android.bluetooth.BluetoothGatt
5+
import android.bluetooth.BluetoothGattCharacteristic
6+
import android.bluetooth.BluetoothGattCallback
7+
import android.bluetooth.BluetoothProfile
8+
import android.app.Service
9+
import android.content.BroadcastReceiver
10+
import android.content.Intent
11+
import android.content.Context
12+
import android.os.IBinder
13+
import android.util.Log
14+
15+
class BluetoothLeService(private var bluetoothGatt: BluetoothGatt?,private var context: Context):Service(){
16+
private val STATE_DISCONNECTED = 0
17+
private val STATE_CONNECTED = 2
18+
val ACTION_GATT_CONNECTED = "com.example.bluetooth.le.ACTION_GATT_CONNECTED"
19+
val ACTION_GATT_DISCONNECTED = "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED"
20+
val ACTION_GATT_SERVICES_DISCOVERED =
21+
"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED"
22+
val ACTION_DATA_AVAILABLE = "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"
23+
val EXTRA_DATA = "com.example.bluetooth.le.EXTRA_DATA"
24+
private var connectionState = STATE_DISCONNECTED
25+
private val TAG = BluetoothLeService::class.java.simpleName
26+
27+
val gattCallback = object : BluetoothGattCallback() {
28+
override fun onConnectionStateChange(
29+
gatt: BluetoothGatt,
30+
status: Int,
31+
newState: Int
32+
) {
33+
val intentAction: String
34+
when (newState) {
35+
BluetoothProfile.STATE_CONNECTED -> {
36+
intentAction = ACTION_GATT_CONNECTED
37+
connectionState = STATE_CONNECTED
38+
broadcastUpdate(intentAction)
39+
Log.i(TAG, "Connected to GATT server.")
40+
Log.i(TAG, "Attempting to start service discovery: " +
41+
bluetoothGatt?.discoverServices())
42+
}
43+
BluetoothProfile.STATE_DISCONNECTED -> {
44+
intentAction = ACTION_GATT_DISCONNECTED
45+
connectionState = STATE_DISCONNECTED
46+
Log.i(TAG, "Disconnected from GATT server.")
47+
broadcastUpdate(intentAction)
48+
}
49+
}
50+
}
51+
// New services discovered
52+
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
53+
when (status) {
54+
BluetoothGatt.GATT_SUCCESS -> broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED)
55+
else -> Log.w(TAG, "onServicesDiscovered received: $status")
56+
}
57+
}
58+
// Result of a characteristic read operation
59+
override fun onCharacteristicRead(
60+
gatt: BluetoothGatt,
61+
characteristic: BluetoothGattCharacteristic,
62+
status: Int
63+
) {
64+
when (status) {
65+
BluetoothGatt.GATT_SUCCESS -> {
66+
println("Yes")
67+
broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic)
68+
}
69+
}
70+
}
71+
}
72+
// private val gattUpdateReceiver = object : BroadcastReceiver() {
73+
//
74+
// private lateinit var bluetoothLeService: BluetoothLeService
75+
//
76+
// override fun onReceive(context: Context, intent: Intent) {
77+
// val action = intent.action
78+
// when (action){
79+
// ACTION_GATT_CONNECTED -> {
80+
// (context as? Activity)?.invalidateOptionsMenu()
81+
// }
82+
// ACTION_GATT_DISCONNECTED -> {
83+
// (context as? Activity)?.invalidateOptionsMenu()
84+
// }
85+
// ACTION_GATT_SERVICES_DISCOVERED -> {
86+
// // Show all the supported services and characteristics on the
87+
// // user interface.
88+
//
89+
// }
90+
// ACTION_DATA_AVAILABLE -> {
91+
//
92+
// displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA))
93+
// }
94+
// }
95+
// }
96+
// }
97+
override fun onBind(p0: Intent?): IBinder? {
98+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
99+
}
100+
private fun broadcastUpdate(action: String) {
101+
val intent = Intent(action)
102+
context.sendBroadcast(intent)
103+
}
104+
private fun broadcastUpdate(action: String,characteristic: BluetoothGattCharacteristic) {
105+
val intent = Intent(action)
106+
val data: ByteArray? = characteristic.value
107+
if (data?.isNotEmpty() == true) {
108+
val hexString: String = data.joinToString(separator = " ") {
109+
String.format("%02X", it)
110+
}
111+
println("Data is \"$data\\n$hexString\"")
112+
intent.putExtra(EXTRA_DATA, "$data\n$hexString")
113+
}
114+
context.sendBroadcast(intent)
115+
}
116+
}

app/src/main/java/com/example/autodoorctrl/autodoorctrlandroid/MapsActivity.kt

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.autodoorctrl.autodoorctrlandroid
22

33
import android.Manifest
4+
import android.app.Activity
45
import android.content.Intent
56
import android.content.pm.PackageManager
67
import androidx.appcompat.app.AppCompatActivity
@@ -12,6 +13,7 @@ import androidx.core.app.ActivityCompat
1213
import androidx.core.content.ContextCompat
1314
import android.location.Location
1415
import android.os.Looper
16+
import android.util.Log
1517
import com.google.android.gms.maps.CameraUpdateFactory
1618
import com.google.android.gms.maps.GoogleMap
1719
import com.google.android.gms.maps.OnMapReadyCallback
@@ -25,14 +27,23 @@ import com.google.android.gms.location.LocationCallback
2527
import com.google.android.gms.location.LocationSettingsRequest
2628
import com.google.android.gms.location.LocationRequest
2729
import okhttp3.Callback
30+
import android.content.Context
2831
import 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
2938
import okhttp3.Response
3039
import org.json.JSONArray
3140
import org.json.JSONException
3241
import com.google.android.gms.location.FusedLocationProviderClient
3342
import com.google.android.gms.location.LocationServices
3443
import java.io.IOException
35-
44+
import java.util.*
45+
import kotlin.collections.ArrayList
46+
//GoogleMap.OnMarkerClickListener
3647
class 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

app/src/main/java/com/example/autodoorctrl/autodoorctrlandroid/SendFeedback.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import android.widget.EditText
1111
import android.widget.ImageView
1212
import android.widget.Toast
1313
import androidx.appcompat.app.AppCompatActivity
14+
import okhttp3.OkHttpClient
1415

1516
class SendFeedback : AppCompatActivity() {
16-
17-
17+
private var client = OkHttpClient()
18+
private var request = OkHttpRequest(client)
1819
override fun onCreate(savedInstanceState: Bundle?) {
1920
super.onCreate(savedInstanceState)
2021
setContentView(R.layout.feedback)
@@ -33,14 +34,15 @@ class SendFeedback : AppCompatActivity() {
3334

3435
val autodoor = "automaticdoorcontrol@gmail.com"
3536
val email = Intent(Intent.ACTION_SEND)
36-
email.putExtra(Intent.EXTRA_EMAIL, arrayOf(autodoor))
37-
email.putExtra(Intent.EXTRA_SUBJECT, "Feedback for Automatic Door Control")
38-
email.putExtra(Intent.EXTRA_TEXT, msg)
39-
40-
email.type = "message/rfc822"
41-
startActivity(Intent.createChooser(email, "Choose an Email client :"))
42-
Log.v("EditText", name.text.toString())
37+
// email.putExtra(Intent.EXTRA_EMAIL, arrayOf(autodoor))
38+
// email.putExtra(Intent.EXTRA_SUBJECT, "Feedback for Automatic Door Control")
39+
// email.putExtra(Intent.EXTRA_TEXT, msg)
40+
// email.type = "message/rfc822"
41+
//
42+
// Log.v("EditText", name.text.toString())
43+
// Toast.makeText(this@SendFeedback, "Sending email to Automatic Door Control", Toast.LENGTH_SHORT).show()
4344
Toast.makeText(this@SendFeedback, "Sending email to Automatic Door Control", Toast.LENGTH_SHORT).show()
45+
4446
}
4547

4648
}

0 commit comments

Comments
 (0)