|
|
@@ -13,6 +13,7 @@ import android.os.Handler
|
|
|
import android.os.Looper
|
|
|
import android.provider.Settings
|
|
|
import android.provider.Settings.SettingNotFoundException
|
|
|
+import android.view.KeyEvent
|
|
|
import android.view.View
|
|
|
import android.view.WindowManager
|
|
|
import android.widget.Button
|
|
|
@@ -20,19 +21,23 @@ import android.widget.TextView
|
|
|
import android.widget.Toast
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
-import androidx.fragment.app.FragmentManager
|
|
|
import androidx.fragment.app.FragmentPagerAdapter
|
|
|
import androidx.viewpager.widget.ViewPager
|
|
|
import com.android.chileaf.WearManager
|
|
|
+import com.android.chileaf.fitness.callback.WearManagerCallbacks
|
|
|
+import com.example.watch.BuildConfig
|
|
|
import com.example.watch.R
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
|
|
import com.yanzhenjie.permission.AndPermission
|
|
|
import com.yanzhenjie.permission.runtime.Permission
|
|
|
import kotlinx.android.synthetic.main.activity_main.*
|
|
|
+import org.w3c.dom.Text
|
|
|
import timber.log.Timber
|
|
|
|
|
|
-class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListener {
|
|
|
- private val mDeviceConnected = false
|
|
|
+
|
|
|
+class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListener,
|
|
|
+ WearManagerCallbacks {
|
|
|
+ private var mDeviceConnected = false
|
|
|
private val mTvAppVersion: TextView? = null
|
|
|
private val mTvDeviceName: TextView? = null
|
|
|
private val mTvVersion: TextView? = null
|
|
|
@@ -45,6 +50,10 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
|
|
|
private val viewPager: ViewPager? = null
|
|
|
private val bottomNavigationView: BottomNavigationView? = null
|
|
|
private val mPagerAdapter: FragmentPagerAdapter? = null
|
|
|
+
|
|
|
+ var toast: Toast? = null
|
|
|
+ var mExitTime: Long = 0
|
|
|
+
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
@@ -77,9 +86,9 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
|
|
|
chainBtn.setOnClickListener {
|
|
|
|
|
|
val dialog = ScannerFragment.getInstance()
|
|
|
- if(dialog == null){
|
|
|
+ if (dialog == null) {
|
|
|
println("1122")
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
dialog.show(supportFragmentManager, "scan_fragment")
|
|
|
}
|
|
|
|
|
|
@@ -114,7 +123,9 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
|
|
|
|
|
|
private fun initView() {
|
|
|
|
|
|
-
|
|
|
+ mManager = WearManager.getInstance(this)
|
|
|
+ mManager!!.setDebug(BuildConfig.DEBUG)
|
|
|
+ mManager!!.setManagerCallbacks(this)
|
|
|
}
|
|
|
|
|
|
private fun login() {
|
|
|
@@ -141,9 +152,9 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private var mManager: WearManager? = null
|
|
|
- private var mLoading: LoadingDialog? = null
|
|
|
- private var mHandler = Handler(Looper.getMainLooper())
|
|
|
+ protected var mManager: WearManager? = null
|
|
|
+ protected var mLoading: LoadingDialog? = null
|
|
|
+ protected var mHandler = Handler(Looper.getMainLooper())
|
|
|
|
|
|
private fun showLoading() {
|
|
|
showLoading(getString(R.string.loading))
|
|
|
@@ -276,25 +287,73 @@ class MainActivity : AppCompatActivity(), ScannerFragment.OnDeviceSelectedListen
|
|
|
|
|
|
override fun onDeviceSelected(device: BluetoothDevice?, name: String?) {
|
|
|
mManager!!.connect(device, false)
|
|
|
-// mTvDeviceName?.setText(getString(R.string.device_name, name))
|
|
|
+// mTvDeviceName!!.text = getString(R.string.device_name, name)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDeviceConnected(device: BluetoothDevice) {
|
|
|
+ mDeviceConnected = true
|
|
|
+// runOnUiThread { mBtnConnect!!.setText(R.string.action_disconnect) }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDeviceDisconnected(device: BluetoothDevice) {
|
|
|
+ runOnUiThread { defaultUI() }
|
|
|
+ mDeviceConnected = false
|
|
|
+ mManager!!.close()
|
|
|
}
|
|
|
|
|
|
- fun onError(device: BluetoothDevice, message: String, errorCode: Int) {
|
|
|
+
|
|
|
+ override fun onError(device: BluetoothDevice, message: String, errorCode: Int) {
|
|
|
Timber.e("onError: ($errorCode)")
|
|
|
showToast("$message ($errorCode)")
|
|
|
}
|
|
|
|
|
|
- fun onDeviceNotSupported(device: BluetoothDevice) {
|
|
|
+ override fun onDeviceNotSupported(device: BluetoothDevice) {
|
|
|
showToast(getString(R.string.not_supported))
|
|
|
}
|
|
|
|
|
|
- fun onSoftwareVersion(device: BluetoothDevice, software: String) {
|
|
|
+ override fun onSoftwareVersion(device: BluetoothDevice, software: String) {
|
|
|
runOnUiThread { mTvVersion?.setText("Version:$software") }
|
|
|
}
|
|
|
|
|
|
- fun onRssiRead(device: BluetoothDevice, rssi: Int) {
|
|
|
+ override fun onRssiRead(device: BluetoothDevice, rssi: Int) {
|
|
|
runOnUiThread { mTvRssi?.setText("Rssi:" + rssi + "dBm") }
|
|
|
}
|
|
|
+
|
|
|
+ // 连续两次回退
|
|
|
+ override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
|
|
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
+ if (System.currentTimeMillis().minus(mExitTime) <= 3000) {
|
|
|
+ finish()
|
|
|
+ } else {
|
|
|
+ mExitTime = System.currentTimeMillis()
|
|
|
+ showToast("再按一次退出程序")
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return super.onKeyDown(keyCode, event)
|
|
|
+ }
|
|
|
+
|
|
|
+// 获取心率数据
|
|
|
+ override fun onHeartRateMeasurementReceived(
|
|
|
+ device: BluetoothDevice,
|
|
|
+ heartRate: Int,
|
|
|
+ contactDetected: Boolean?,
|
|
|
+ energyExpanded: Int?,
|
|
|
+ rrIntervals: MutableList<Int>?
|
|
|
+ ) {
|
|
|
+ super.onHeartRateMeasurementReceived(
|
|
|
+ device,
|
|
|
+ heartRate,
|
|
|
+ contactDetected,
|
|
|
+ energyExpanded,
|
|
|
+ rrIntervals
|
|
|
+ )
|
|
|
+
|
|
|
+ //心跳
|
|
|
+ findViewById<TextView>(R.id.heartReal).text = heartRate.toString()
|
|
|
+// 运动强度
|
|
|
+ findViewById<TextView>(R.id.activLevel).text = "10"
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|