Parcourir la source

1.增加开机自启动
2.433收发功能

weizhengliang il y a 2 ans
Parent
commit
e7b3250094

+ 14 - 1
app/src/main/AndroidManifest.xml

@@ -11,6 +11,11 @@
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 允许程序访问访问WIFI网络状态信息 -->
     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 
+    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
+    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
+
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
+
     <application
         android:name="com.wdkl.ncs.s433.receiver.Application"
         android:allowBackup="true"
@@ -27,7 +32,6 @@
             android:label="@string/title_activity_settings" />
         <activity
             android:name="com.wdkl.ncs.s433.receiver.MainActivity"
-            android:label="@string/title_activity_main"
             android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
@@ -35,6 +39,15 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <receiver
+            android:name=".receiver.WdBootReceiver"
+            android:enabled="true"
+            android:exported="true">
+            <intent-filter android:priority="1000">
+                <action android:name="android.intent.action.BOOT_COMPLETED"/>
+            </intent-filter>
+        </receiver>
     </application>
 
 </manifest>

+ 76 - 6
app/src/main/java/com/wdkl/ncs/s433/receiver/MainActivity.java

@@ -7,6 +7,7 @@ import android.net.ConnectivityManager;
 import android.net.Network;
 import android.net.NetworkRequest;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
@@ -17,10 +18,16 @@ import androidx.annotation.NonNull;
 import androidx.preference.PreferenceManager;
 
 import com.wdkl.ncs.s433.receiver.common.Constants;
+import com.wdkl.ncs.s433.receiver.data.TcpAction;
+import com.wdkl.ncs.s433.receiver.data.TcpModel;
+import com.wdkl.ncs.s433.receiver.data.TcpType;
+import com.wdkl.ncs.s433.receiver.media.RingPlayHelper;
 import com.wdkl.ncs.s433.receiver.receiver.NetworkConnectChangedReceiver;
 import com.wdkl.ncs.s433.receiver.data.EventBusModel;
 import com.wdkl.ncs.s433.receiver.utils.AutoRebootUtil;
+import com.wdkl.ncs.s433.receiver.utils.ByteUtil;
 import com.wdkl.ncs.s433.receiver.utils.NetUtil;
+import com.wdkl.ncs.s433.receiver.utils.VoiceManagerUtil;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
@@ -38,8 +45,8 @@ public class MainActivity extends SerialPortActivity  {
     EditText mReception;
     ConnectivityManager connectivityManager;
 
-    String macAddress;
-    String localIP;
+    String macAddress = "";
+    String localIP = "";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -69,7 +76,7 @@ public class MainActivity extends SerialPortActivity  {
         findViewById(R.id.btn_send_test).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                send("$" + macAddress + "{s433,ACK}#");
+                //
             }
         });
 
@@ -115,9 +122,8 @@ public class MainActivity extends SerialPortActivity  {
             tcpstatus.setTextColor(getResources().getColor(R.color.red));
         }
 
-        //todo test
-        //String hex = "2438433a46433a41303a46313a30313a41377b22616374696f6e223a2243414e43454c222c2274797065223a2253343333222c2264617461223a7b227469746c65223a223131e688bfe58f96e6b688227d7d0a23";
-
+        //音频输出100%
+        VoiceManagerUtil.setMusicVoice(getApplicationContext(), 100);
     }
 
     @Override
@@ -171,6 +177,70 @@ public class MainActivity extends SerialPortActivity  {
         });
     }
 
+    @Override
+    protected void onDataReceivedString(String data) {
+        //$8C:FC:A0:F1:01:A7{"action":"SOS_CALL","type":"S433","data":{"title":"12房紧急呼叫"}}#
+        Log.e("Application", "receiveData: " + data);
+
+        try {
+            //去掉'$'起始符和‘#’结束符
+            if (data.startsWith("$") && data.endsWith("#")) {
+                String tempData = data.substring(1, data.length()-1);
+                int sepIndex = tempData.indexOf("{");
+                String mac = tempData.substring(0, sepIndex);
+                String tcpData = tempData.substring(sepIndex);
+                Log.e("Application", "tempData: " + tempData + ", mac: " + mac + ", tcpData: " + tcpData);
+
+                //如果是发送给本机,则返回ack,并处理点阵屏数据
+                if (macAddress.equals(mac)) {
+                    TcpModel tcpModel = TcpModel.getModelByJson(tcpData);
+                    if (tcpModel != null) {
+                        parseTcpData(tcpModel);
+                    }
+
+                    ackCallback(macAddress);
+                }
+
+                if (BuildConfig.DEBUG) {
+                    //调试版本输出信息
+                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                    final String showMsg = sdf.format(new Date()) + " 收到数据:" + data + "\n";
+
+                    runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            if (mReception != null) {
+                                mReception.append(showMsg);
+                            }
+                        }
+                    });
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            Log.e("Application", "onDataReceivedString Exception...");
+        }
+    }
+
+    private void parseTcpData(TcpModel tcpModel) {
+        Log.e("Application", "action: " + tcpModel.getAction() + ", type: " + tcpModel.getType() + ", data: " + tcpModel.getData());
+        if (TcpType.S433 == tcpModel.getType()) {
+            if (TcpAction.S433Action.CALL == tcpModel.getAction() || TcpAction.S433Action.SOS_CALL == tcpModel.getAction()) {
+                //上屏
+            } else if (TcpAction.S433Action.CANCEL == tcpModel.getAction()) {
+                //清屏
+            }
+        }
+    }
+
+    private void ackCallback(String mac) {
+        TcpModel tcpModel = new TcpModel();
+        tcpModel.setType(TcpType.S433);
+        tcpModel.setAction(TcpAction.S433Action.ACK);
+        String tcpData = tcpModel.toJson();
+        sendString("$" + mac + tcpData + "#");
+    }
+
     private void openNetworkDebug() {
         try {
             Process p = Runtime.getRuntime().exec("su");

+ 66 - 6
app/src/main/java/com/wdkl/ncs/s433/receiver/SerialPortActivity.java

@@ -24,6 +24,11 @@ public abstract class SerialPortActivity extends Activity {
     private InputStream mInputStream;
     private ReadThread mReadThread;
 
+    private byte[] buffer = new byte[1024];
+    private byte[] re_buffer = new byte[2048];
+    String data = "";
+    public int writeIndex = 0;
+
     private class ReadThread extends Thread {
 
         @Override
@@ -35,17 +40,24 @@ public abstract class SerialPortActivity extends Activity {
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
-                int size;
+
                 try {
-                    byte[] buffer = new byte[1024];
-                    if (mInputStream == null) return;
-                    if(mInputStream.available()>0) {
+                    if (mInputStream == null) {
+                        return;
+                    }
+
+                    /*if(mInputStream.available()>0) {
                         size = mInputStream.read(buffer);
                         if (size > 0) {
                             onDataReceived(buffer, size);
                         }
+                    }*/
+
+                    if (mInputStream.available() > 0) {
+                        int size = mInputStream.read(buffer);
+                        parseData(size);
                     }
-                } catch (IOException e) {
+                } catch (Exception e) {
                     e.printStackTrace();
                     return;
                 }
@@ -53,11 +65,57 @@ public abstract class SerialPortActivity extends Activity {
         }
     }
 
+    public void parseData(int size) {
+        Log.e("Serial", "received data size: " + size);
+        if (size > 0) {
+            for (int i = 0; i < size; i++) {
+                //过滤掉换行符
+                if (buffer[i] != '\r' && buffer[i] != '\n') {
+                    if (buffer[i] == '$') {
+                        //‘$’符开头
+                        writeIndex = 0;
+                        re_buffer[writeIndex] = buffer[i];
+                        writeIndex++;
+                    } else if (buffer[i] == '#') {
+                        //"#"符结尾
+                        re_buffer[writeIndex] = buffer[i];
+
+                        //解析完成
+                        data = new String(re_buffer, 0, writeIndex+1);
+                        onDataReceivedString(data);
+                        data = "";
+                        writeIndex = 0;
+                        break;
+                    } else {
+                        re_buffer[writeIndex] = buffer[i];
+                        writeIndex++;
+                        if (writeIndex > 2047) {
+                            writeIndex = 0;
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     protected void send(String data) {
         try {
             if (Constants.SERIAL_PORT_STATUS) {
                 String s = ByteUtil.str2HexStr(data);
-                byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
+                byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
+                mOutputStream.write(bytes);
+                Log.d("serialPort","==send data==" + data + ", hex: " + s);
+            }
+        } catch (IOException e) {
+            Log.e("serialPort","==send data exception: " + data);
+            e.printStackTrace();
+        }
+    }
+
+    protected void sendString(String data) {
+        try {
+            if (Constants.SERIAL_PORT_STATUS) {
+                byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
                 mOutputStream.write(bytes);
                 Log.d("serialPort","==send data==" + data);
             }
@@ -112,6 +170,8 @@ public abstract class SerialPortActivity extends Activity {
 
     protected abstract void onDataReceived(final byte[] buffer, final int size);
 
+    protected abstract void onDataReceivedString(String data);
+
     @Override
     protected void onDestroy() {
         if (mReadThread != null) mReadThread.interrupt();

+ 82 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/data/TcpAction.java

@@ -0,0 +1,82 @@
+package com.wdkl.ncs.s433.receiver.data;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * TCP传输类型的动作
+ *
+ * @author allen
+ * 2021-03-30 15:30
+ */
+public interface TcpAction {
+    String getName();
+    String getDescription();
+
+    enum CallbackAction implements TcpAction {
+        ACK("反馈"),
+        SUCCESS("同步"),
+        FAILED("失败"),
+        NO_MATCH("没有匹配");
+        private final String description;
+        CallbackAction(String description){
+            this.description = description;
+        }
+        public String getDescription() {
+            return description;
+        }
+
+        public String getName(){
+            return this.name();
+        }
+
+        private final static Map<String , CallbackAction> ENUM_MAP = new HashMap<String, CallbackAction>();
+        static {
+            for(CallbackAction v : values()) {
+                ENUM_MAP.put(v.toString() , v);
+            }
+        }
+        public static CallbackAction fromString(String v) {
+            CallbackAction userOptionEnum = ENUM_MAP.get(v);
+            return userOptionEnum == null ? NO_MATCH :userOptionEnum;
+        }
+    }
+
+
+    /**
+     * 433相关action
+      */
+    enum S433Action implements TcpAction {
+        CALL("呼叫"),
+        SOS_CALL("紧急呼叫"),
+        CANCEL("取消"),
+        SYNC_TIME("同步时间"),
+        DATA("同步数据"),
+        EXIST("调试"),
+        ACK("返回"),
+        NO_MATCH("没有匹配");
+
+        private final String description;
+        S433Action(String description){
+            this.description = description;
+        }
+        public String getDescription() {
+            return description;
+        }
+
+        public String getName(){
+            return this.name();
+        }
+
+        private final static Map<String , S433Action> ENUM_MAP = new HashMap<String, S433Action>();
+        static {
+            for(S433Action v : values()) {
+                ENUM_MAP.put(v.toString() , v);
+            }
+        }
+        public static S433Action fromString(String v) {
+            S433Action userOptionEnum = ENUM_MAP.get(v);
+            return userOptionEnum == null ? NO_MATCH :userOptionEnum;
+        }
+    }
+}

+ 91 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/data/TcpModel.java

@@ -0,0 +1,91 @@
+package com.wdkl.ncs.s433.receiver.data;
+
+import android.text.TextUtils;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+
+import java.io.Serializable;
+
+/**
+ * tcp传输对象
+ *
+ * @author allen
+ * 2021-03-30 11:49
+ */
+public class TcpModel implements Serializable {
+
+    /**
+     * TCP传输对象的类型
+     */
+    private TcpType type;
+    /**
+     * TCP传输对象类型的动作
+     */
+    private TcpAction action;
+
+    /**
+     * TCP传输对象的数据
+     */
+    private Object data;
+
+    public TcpModel(){
+    }
+
+    public TcpType getType() {
+        return type;
+    }
+
+    public void setType(TcpType type) {
+        this.type = type;
+    }
+
+    public TcpAction getAction() {
+        return action;
+    }
+
+    public void setAction(TcpAction action) {
+        this.action = action;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+
+    public String toJson() {
+        return JSON.toJSONString(this);
+    }
+
+    public static TcpModel getModelByJson(String tcpModelJsonString) {
+        if (TextUtils.isEmpty(tcpModelJsonString)) {
+            return null;
+        }
+        TcpModel tcpModel = new TcpModel();
+
+        JSONObject jsonObject = JSON.parseObject(tcpModelJsonString);
+        String type = jsonObject.getString("type");
+        String action = jsonObject.getString("action");
+        String dataString = jsonObject.getString("data");
+
+        TcpType tcpType = TcpType.fromString(type);
+        TcpAction tcpAction = null;
+        switch (tcpType) {
+            case CALLBACK:
+                tcpAction = TcpAction.CallbackAction.fromString(action);
+                break;
+            case S433:
+                tcpAction = TcpAction.S433Action.fromString(action);
+                break;
+        }
+
+        tcpModel.setType(tcpType);
+        tcpModel.setAction(tcpAction);
+        tcpModel.setData(dataString);
+
+        return tcpModel;
+    }
+}

+ 31 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/data/TcpType.java

@@ -0,0 +1,31 @@
+package com.wdkl.ncs.s433.receiver.data;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * TCP传输对象的类型
+ */
+public enum TcpType {
+    CALLBACK("TCP反馈"),
+    S433("433");
+
+    private final String description;
+    TcpType(String description){
+        this.description = description;
+    }
+    public String getDescription() {
+        return description;
+    }
+
+    private final static Map<String , TcpType> ENUM_MAP = new HashMap<String, TcpType>();
+    static {
+        for(TcpType v : values()) {
+            ENUM_MAP.put(v.toString() , v);
+        }
+    }
+    public static TcpType fromString(String v) {
+        TcpType userOptionEnum = ENUM_MAP.get(v);
+        return userOptionEnum == null ? CALLBACK :userOptionEnum;
+    }
+}

+ 187 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/media/AsyncPlayer.java

@@ -0,0 +1,187 @@
+package com.wdkl.ncs.s433.receiver.media;
+
+import android.content.Context;
+import android.media.AudioDeviceInfo;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.os.PowerManager;
+import android.os.SystemClock;
+import android.util.Log;
+
+import java.util.LinkedList;
+
+/**
+ * 响铃相关类
+ */
+public class AsyncPlayer {
+    private static final int PLAY = 1;
+    private static final int STOP = 2;
+    private AudioManager audioManager;
+
+    private static final class Command {
+        int code;
+        Context context;
+        int resId;
+        boolean looping;
+        int stream;
+        long requestTime;
+
+        public String toString() {
+            return "{ code=" + code + " looping=" + looping + " stream=" + stream + " resId=" + resId + " }";
+        }
+    }
+
+    private final LinkedList mCmdQueue = new LinkedList();
+
+    private void startSound(Command cmd) {
+
+        try {
+            //MediaPlayer player = new MediaPlayer();
+            MediaPlayer player = MediaPlayer.create(cmd.context, cmd.resId);
+            player.setAudioStreamType(cmd.stream);
+            //player.setDataSource(cmd.context, cmd.uri);
+            player.setLooping(cmd.looping);
+            player.setVolume(1.0f, 1.0f);
+            //player.prepare();
+            player.start();
+            if (mPlayer != null) {
+                mPlayer.release();
+            }
+            mPlayer = player;
+            Log.w(mTag, "start sound " + cmd.resId);
+        } catch (Exception e) {
+            Log.w(mTag, "error loading sound for " + cmd.resId, e);
+        }
+    }
+
+    private final class Thread extends java.lang.Thread {
+        Thread() {
+            super("AsyncPlayer-" + mTag);
+        }
+
+        public void run() {
+            while (true) {
+                Command cmd = null;
+
+                synchronized (mCmdQueue) {
+
+                    cmd = (Command) mCmdQueue.removeFirst();
+                }
+
+                switch (cmd.code) {
+                    case PLAY:
+                        startSound(cmd);
+                        break;
+                    case STOP:
+
+                        if (mPlayer != null) {
+                            mPlayer.stop();
+                            mPlayer.reset();
+                            mPlayer.release();
+                            mPlayer = null;
+                        } else {
+                            Log.w(mTag, "STOP command without a player");
+                        }
+                        break;
+                }
+
+                synchronized (mCmdQueue) {
+                    if (mCmdQueue.size() == 0) {
+
+                        mThread = null;
+                        releaseWakeLock();
+                        return;
+                    }
+                }
+            }
+        }
+    }
+
+    private String mTag;
+    private Thread mThread;
+    private MediaPlayer mPlayer;
+    private PowerManager.WakeLock mWakeLock;
+
+    private int mState = STOP;
+
+    public AsyncPlayer(String tag) {
+        if (tag != null) {
+            mTag = tag;
+        } else {
+            mTag = "AsyncPlayer";
+        }
+    }
+
+    public void play(Context context, int res, boolean looping, int stream) {
+        Command cmd = new Command();
+        cmd.requestTime = SystemClock.uptimeMillis();
+        cmd.code = PLAY;
+        cmd.context = context;
+        cmd.resId = res;
+        cmd.looping = looping;
+        cmd.stream = stream;
+        synchronized (mCmdQueue) {
+            enqueueLocked(cmd);
+            mState = PLAY;
+        }
+    }
+
+    public void stop() {
+        synchronized (mCmdQueue) {
+            if (mState != STOP) {
+                Command cmd = new Command();
+                cmd.requestTime = SystemClock.uptimeMillis();
+                cmd.code = STOP;
+                enqueueLocked(cmd);
+                mState = STOP;
+            }
+        }
+    }
+
+    private void enqueueLocked(Command cmd) {
+        mCmdQueue.add(cmd);
+        if (mThread == null) {
+            acquireWakeLock();
+            mThread = new Thread();
+            mThread.start();
+        }
+    }
+
+    public void setUsesWakeLock(Context context) {
+        if (mWakeLock != null || mThread != null) {
+            throw new RuntimeException("assertion failed mWakeLock=" + mWakeLock + " mThread=" + mThread);
+        }
+        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag);
+    }
+
+    private void acquireWakeLock() {
+        if (mWakeLock != null) {
+            mWakeLock.acquire();
+        }
+    }
+
+    private void releaseWakeLock() {
+        if (mWakeLock != null) {
+            mWakeLock.release();
+        }
+    }
+
+    private boolean isHeadphonesPlugged(Context context) {
+        if (audioManager == null) {
+            audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        }
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
+            AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
+            for (AudioDeviceInfo deviceInfo : audioDevices) {
+                if (deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
+                        || deviceInfo.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
+                    return true;
+                }
+            }
+            return false;
+        } else {
+            return audioManager.isWiredHeadsetOn();
+        }
+    }
+}

+ 22 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/media/RingPlayHelper.java

@@ -0,0 +1,22 @@
+package com.wdkl.ncs.s433.receiver.media;
+
+import android.content.Context;
+import android.media.AudioManager;
+
+public class RingPlayHelper {
+
+    private static AsyncPlayer ringPlayer;
+
+    public static void playRingTone(Context context, int res, boolean loop) {
+        if (ringPlayer == null) {
+            ringPlayer = new AsyncPlayer(null);
+        }
+        ringPlayer.play(context, res, loop, AudioManager.STREAM_MUSIC);
+    }
+
+    public static void stopRingTone() {
+        if (ringPlayer != null) {
+            ringPlayer.stop();
+        }
+    }
+}

+ 33 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/receiver/WdBootReceiver.java

@@ -0,0 +1,33 @@
+package com.wdkl.ncs.s433.receiver.receiver;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+import android.os.Handler;
+import android.util.Log;
+
+import com.wdkl.ncs.s433.receiver.MainActivity;
+
+public class WdBootReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(final Context context, Intent intent) {
+        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
+            Log.d("wdBoot", "收到开机广播,启动app");
+            new Handler().postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    startHome(context);
+                }
+            }, 5000L);
+        }
+    }
+
+    private void startHome(Context context) {
+        Log.d("wdBoot", "start home activity");
+        Intent startIntent= new Intent(context, MainActivity.class);
+        startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        context.startActivity(startIntent);
+    }
+}

+ 181 - 0
app/src/main/java/com/wdkl/ncs/s433/receiver/utils/VoiceManagerUtil.java

@@ -0,0 +1,181 @@
+package com.wdkl.ncs.s433.receiver.utils;
+
+import android.content.Context;
+import android.media.AudioManager;
+
+public class VoiceManagerUtil {
+    /**
+     * 获取提示音音量最大值
+     *
+     * @param context
+     */
+    public static int getAlarmMax(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
+    }
+
+    /**
+     * 获取提示音音量当前值
+     *
+     * @param context
+     */
+    public static int getAlarmNow(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
+    }
+
+    /**
+     * 获取多媒体音量最大值
+     *
+     * @param context
+     */
+    public static int getMusicMax(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+    }
+
+    /**
+     * 获取多媒体音量当前值
+     *
+     * @param context
+     */
+    public static int getMusicNow(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+    }
+
+    /**
+     * 获取铃声音量最大值
+     *
+     * @param context
+     */
+    public static int getRingMax(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
+    }
+
+    /**
+     * 获取铃声音量当前值
+     *
+     * @param context
+     */
+    public static int getRingNow(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
+    }
+
+    /**
+     * 获取系统音量最大值
+     *
+     * @param context
+     */
+    public static int getSystemMax(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
+    }
+
+    /**
+     * 获取系统音量当前值
+     *
+     * @param context
+     */
+    public static int getSystemNow(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
+    }
+
+    /**
+     * 获取通话音量最大值
+     *
+     * @param context
+     */
+    public static int getCallMax(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
+    }
+
+    /**
+     * 获取通话音量当前值
+     *
+     * @param context
+     */
+    public static int getCallNow(Context context) {
+        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        return mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
+    }
+
+    /**
+     * 设置提示音音量
+     *
+     * @param context
+     * @param percent (百分比;只能0--100之间)
+     */
+    public static void setAlarmVoice(Context context, int percent) {
+        float vPercent=((float)percent)/100f;
+        vPercent = vPercent < 0 ? 0 : vPercent;
+        vPercent = vPercent > 1 ? 1 : vPercent;
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, (int) (getAlarmMax(context) * vPercent), 0);
+    }
+
+    /**
+     * 设置多媒体音量
+     *
+     * @param context
+     * @param percent (百分比;只能0--100之间)
+     */
+    public static void setMusicVoice(Context context, int percent) {
+        float vPercent=((float)percent)/100f;
+        vPercent = vPercent < 0 ? 0 : vPercent;
+        vPercent = vPercent > 1 ? 1 : vPercent;
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) (getMusicMax(context) * vPercent), 0);
+    }
+
+    /**
+     * 设置铃声音量
+     *
+     * @param context
+     * @param percent (百分比;只能0--100之间)
+     */
+    public static void setRingVoice(Context context, int percent) {
+        float vPercent=((float)percent)/100f;
+        vPercent = vPercent < 0 ? 0 : vPercent;
+        vPercent = vPercent > 1 ? 1 : vPercent;
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setStreamVolume(AudioManager.STREAM_RING, (int) (getRingMax(context) * vPercent), 0);
+    }
+
+    /**
+     * 设置系统音量
+     *
+     * @param context
+     * @param percent (百分比;只能0--100之间)
+     */
+    public static void setSystemVoice(Context context, int percent) {
+        float vPercent=((float)percent)/100f;
+        vPercent = vPercent < 0 ? 0 : vPercent;
+        vPercent = vPercent > 1 ? 1 : vPercent;
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, (int) (getSystemMax(context) * vPercent), 0);
+    }
+
+    /**
+     * 设置通话音量
+     *
+     * @param context
+     * @param percent (百分比;只能0--100之间)
+     */
+    public static void setCallVoice(Context context, int percent) {
+        float vPercent=((float)percent)/100f;
+        vPercent = vPercent < 0 ? 0 : vPercent;
+        vPercent = vPercent > 1 ? 1 : vPercent;
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, (int) (getCallMax(context) * vPercent), 0);
+    }
+
+    public static void setAudioMode(Context context, int mode) {
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setMode(mode);
+    }
+}

+ 5 - 1
app/src/main/res/layout/activity_main.xml

@@ -75,7 +75,8 @@
             android:layout_height="wrap_content"
             android:layout_marginBottom="10dp"
             android:layout_marginTop="10dp"
-            android:text="发送测试" />
+            android:text="发送测试"
+            android:visibility="gone"/>
 
         <Button
             android:id="@+id/ButtonSetup"
@@ -110,6 +111,9 @@
         android:gravity="top"
         android:hint="接收区"
         android:editable="false"
+        android:singleLine="false"
+        android:maxLines="25"
+        android:scrollbars="vertical"
         android:scrollbarStyle="insideOverlay"
         android:isScrollContainer="true">
     </EditText>

BIN
app/src/main/res/raw/incoming_call.mp3


BIN
app/src/main/res/raw/sos.mp3


+ 1 - 2
app/src/main/res/values/strings.xml

@@ -1,9 +1,8 @@
 <resources>
-    <string name="app_name">LoraGateway</string>
+    <string name="app_name">433无线接收器</string>
     <string name="error_configuration">请配置串口号</string>
     <string name="error_security">无法读取串口</string>
     <string name="error_unknown">串口无法打开</string>
-    <string name="title_activity_main">433接收器</string>
     <string name="title_activity_settings">系统通信设置</string>
 
     <!-- Preference Titles -->