Bladeren bron

蓝牙权限获取

Signed-off-by: duanchangpeng <838560574@qq.com>
duanchangpeng 5 jaren geleden
bovenliggende
commit
77fdce1bd6

+ 49 - 5
app/src/main/java/com/example/watch/ui/activity/MainActivity.kt

@@ -1,7 +1,9 @@
 package com.example.watch.ui.activity
 
+import android.bluetooth.BluetoothAdapter
 import android.content.Intent
 import android.os.Bundle
+import android.widget.Button
 import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
 import com.example.watch.R
@@ -17,6 +19,21 @@ class MainActivity : AppCompatActivity() {
 //        登录判断
         login()
 
+//        连接心率带
+        linkHeartBand()
+
+        //判断是否有权限
+//        if (ContextCompat.checkSelfPermission(this,
+//                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+////请求权限
+//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
+//                    MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
+////判断是否需要 向用户解释,为什么要申请该权限
+//            if(ActivityCompat.shouldShowRequestPermissionRationale(this,
+//                            Manifest.permission.READ_CONTACTS)) {
+//                Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();
+//            }
+//        }
 
 //        点击按钮Toast
 //        val button4 = findViewById<Button>(R.id.CONNECT)
@@ -41,10 +58,36 @@ class MainActivity : AppCompatActivity() {
     }
 
 
-    private fun startScan() {
-        TODO("Not yet implemented")
+    private fun linkHeartBand() {
+        val that = this;
+        val linkHeartBandBtn = findViewById<Button>(R.id.linkBtn)
+        linkHeartBandBtn.setOnClickListener {
+            val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
+            if (mBluetoothAdapter == null) {
+                Toast.makeText(this, "当前设备不支持蓝牙!", Toast.LENGTH_SHORT).show()
+            }
+            if (!mBluetoothAdapter.isEnabled) {
+                val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
+                val REQUEST_ENABLE_BT = 1;
+                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
+            }else{
+                val mBluetoothLeScanner = mBluetoothAdapter.bluetoothLeScanner
+                Toast.makeText(this, "蓝牙何在?", Toast.LENGTH_SHORT).show()
+
+//                https://www.cnblogs.com/arci/p/8145145.html
+
+//                val bleScanCallback = BleScanCallback(bleScanResults)
+//                bleScanCallback.setContext(this.applicationContext)
+//                bleScanner.startScan(bleScanCallback)
+//                Toast.makeText(this.applicationContext, "蓝牙BLE扫描开始", Toast.LENGTH_SHORT).show()
+//                bleScanHandler.postDelayed(bleStopScan, this.BLE_SCAN_PERIOD)
+                }
+        }
     }
 
+
+
+
     private fun initView() {
 
 //        val navBatton = findViewById<Button>(R.id.navigation_second)
@@ -56,7 +99,7 @@ class MainActivity : AppCompatActivity() {
 
     }
 
-    private fun login(){
+    private fun login() {
         //        val login = 1
 //        if(login == 1){
 //            val LoginActivityIntent = Intent(this, LoginActivity::class.java)
@@ -64,7 +107,7 @@ class MainActivity : AppCompatActivity() {
 //        }
     }
 
-    private fun FirstRun(){
+    private fun FirstRun() {
         val sharedPreferences = getSharedPreferences("FirstRun", 0)
         val first_run = sharedPreferences.getBoolean("First", true)
         if (first_run) {
@@ -76,4 +119,5 @@ class MainActivity : AppCompatActivity() {
         }
     }
 
-}
+
+}

BIN
app/src/main/res/drawable/circle_btn.png


BIN
app/src/main/res/drawable/head.png


BIN
app/src/main/res/drawable/male.png


+ 187 - 20
app/src/main/res/layout/activity_main.xml

@@ -54,15 +54,16 @@
                             android:layout_height="82dp"
                             android:layout_weight="1"
                             android:gravity="center"
-                            android:text="39"
+                            android:text="0"
                             android:textSize="50dp" />
 
                         <TextView
                             android:id="@+id/textView8"
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            android:layout_weight="1"
+                            android:layout_width="90dp"
+                            android:layout_height="30dp"
                             android:gravity="center"
+                            android:layout_gravity="center"
+                            android:background="@drawable/circle_btn"
                             android:text="激活放松" />
 
                     </LinearLayout>
@@ -80,7 +81,7 @@
                             android:layout_centerHorizontal="true"
                             android:layout_weight="1"
                             android:gravity="center"
-                            tools:srcCompat="@tools:sample/avatars" />
+                            android:src="@drawable/head" />
 
                         <TextView
                             android:id="@+id/textView10"
@@ -103,15 +104,16 @@
                             android:layout_height="96dp"
                             android:layout_weight="1"
                             android:gravity="center"
-                            android:text="120"
+                            android:text="0"
                             android:textSize="50dp" />
 
                         <TextView
                             android:id="@+id/textView9"
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            android:layout_weight="1"
+                            android:layout_width="90dp"
+                            android:layout_height="30dp"
                             android:gravity="center"
+                            android:layout_gravity="center"
+                            android:background="@drawable/circle_btn"
                             android:text="瞬时心率" />
                     </LinearLayout>
 
@@ -121,6 +123,100 @@
 
         </LinearLayout>
 
+        <LinearLayout
+            android:id="@+id/main_bottom"
+            android:layout_width="match_parent"
+            android:layout_height="132dp"
+            android:layout_marginTop="5dp"
+            android:layout_marginBottom="5dp"
+            android:background="#fff"
+            android:orientation="horizontal">
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="10"
+                android:orientation="vertical">
+
+                <TextView
+                    android:id="@+id/calNum"
+                    android:layout_width="match_parent"
+                    android:layout_height="82dp"
+                    android:layout_weight="1"
+                    android:gravity="center"
+                    android:text="0"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+                <TextView
+                    android:id="@+id/caltext"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="2"
+                    android:gravity="center"
+                    android:text="卡路里"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+            </LinearLayout>
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="10"
+                android:orientation="vertical">
+
+                <TextView
+                    android:id="@+id/sportNum"
+                    android:layout_width="match_parent"
+                    android:layout_height="82dp"
+                    android:layout_weight="1"
+                    android:gravity="center"
+                    android:text="0"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+                <TextView
+                    android:id="@+id/sportText"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="2"
+                    android:gravity="center"
+                    android:text="运动卡路里"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+            </LinearLayout>
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="10"
+                android:orientation="vertical">
+
+                <TextView
+                    android:id="@+id/textView7"
+                    android:layout_width="match_parent"
+                    android:layout_height="82dp"
+                    android:layout_weight="1"
+                    android:gravity="center"
+                    android:text="0"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+                <TextView
+                    android:id="@+id/textView11"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="2"
+                    android:gravity="center"
+                    android:text="CK"
+                    android:textColor="#333"
+                    android:textSize="20dp" />
+
+            </LinearLayout>
+        </LinearLayout>
+
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="70dp"
@@ -150,7 +246,7 @@
                             android:layout_width="match_parent"
                             android:layout_height="wrap_content"
                             android:gravity="center"
-                            android:text="147"
+                            android:text="0"
                             android:textColor="#000"
                             android:textSize="29dp" />
 
@@ -181,7 +277,7 @@
                             android:layout_width="match_parent"
                             android:layout_height="wrap_content"
                             android:gravity="center"
-                            android:text="7"
+                            android:text="0"
                             android:textColor="#000"
                             android:textSize="29dp" />
 
@@ -211,7 +307,7 @@
                             android:layout_width="match_parent"
                             android:layout_height="wrap_content"
                             android:gravity="center"
-                            android:text="38"
+                            android:text="0"
                             android:textColor="#000"
                             android:textSize="29dp" />
 
@@ -241,7 +337,7 @@
                             android:layout_width="match_parent"
                             android:layout_height="wrap_content"
                             android:gravity="center"
-                            android:text="79"
+                            android:text="0"
                             android:textColor="#000"
                             android:textSize="29dp" />
 
@@ -259,23 +355,94 @@
         </LinearLayout>
 
         <LinearLayout
-            android:id="@+id/main_bottom"
             android:layout_width="match_parent"
-            android:layout_height="168dp"
+            android:layout_height="10dp"
+            android:layout_weight="1"
+            android:background="#fff"
             android:orientation="horizontal">
 
             <LinearLayout
                 android:layout_width="match_parent"
-                android:layout_height="647dp"
-                android:layout_weight="10"
-                android:orientation="vertical"></LinearLayout>
+                android:layout_height="44dp"
+                android:layout_weight="2"
+                android:orientation="horizontal">
+
+                <Button
+                    android:id="@+id/button"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:background="#FF0000"
+                    android:text="停止运动" />
+
+            </LinearLayout>
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="44dp"
+                android:layout_weight="1"
+                android:background="#fff"
+                android:orientation="horizontal">
+
+                <Button
+                    android:id="@+id/linkBtn"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:background="#EFEFEF"
+                    android:textColor="#333"
+                    android:text="开始链接心率带" />
+            </LinearLayout>
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="44dp"
+                android:layout_weight="2"
+                android:orientation="horizontal">
+
+                <Button
+                    android:id="@+id/button3"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_weight="1"
+                    android:background="#E2E2E2"
+                    android:textColor="#fff"
+                    android:text="心率上报" />
+            </LinearLayout>
         </LinearLayout>
 
+        <TextView
+            android:id="@+id/textView12"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:background="#fff"
+            android:textColor="#000"
+            android:textSize="20dp"
+            android:text="5分钟心率趋势图" />
+
+        <TextView
+            android:id="@+id/textView21"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:background="#fff"
+            android:textColor="#000"
+            android:textSize="15dp"
+            android:text="最大心率 : 0      平均心率 : 0" />
+
         <LinearLayout
             android:layout_width="match_parent"
-            android:layout_height="match_parent"
+            android:layout_height="wrap_content"
             android:layout_weight="1"
-            android:orientation="horizontal"></LinearLayout>
+            android:orientation="horizontal">
+
+            <SurfaceView
+                android:id="@+id/surfaceView2"
+                android:layout_width="wrap_content"
+                android:layout_height="73dp"
+                android:layout_weight="1" />
+        </LinearLayout>
 
         <com.google.android.material.bottomnavigation.BottomNavigationView
             android:id="@+id/nav_view"