andoid 串口通讯发射AT指令

 

1:下载 android_serialport_api 源码 提取如下代码,添加到自己的代码

实现 

cm10_2v16:/ # busybox stty -F /dev/ttyS1 ispeed 9600 ospeed 9600 cs8
cm10_2v16:/ # echo AT+DMOSETGROUP=0,409.75,409.75,0,0,4,1 > /dev/ttyS1

 

andoid 串口通讯发射AT指令_第1张图片 标题

 

   public static void JniSendCommand(String command)
    {
        try {
            mSp=new SerialPort(new File("/dev/ttyS1"),9600,0);
            LogUitl.LogUitl_d("wuyu","open /dev/ttyS1 ok");
        } catch (SecurityException e) {
            e.printStackTrace();
            LogUitl.LogUitl_e("wuyu","open /dev/ttyS1 SecurityException");
        } catch (IOException e) {
            e.printStackTrace();
            LogUitl.LogUitl_e("wuyu","open /dev/ttyS1 IOException");
        }

        mOutputStream=(FileOutputStream) mSp.getOutputStream();

        mInputStream=(FileInputStream) mSp.getInputStream();


        try {
            mOutputStream.write(new String(command).getBytes());
            LogUitl.LogUitl_d("wuyu","write /dev/ttyS1 ok");
        } catch (IOException e) {
            e.printStackTrace();
            LogUitl.LogUitl_e("wuyu","write /dev/ttyS1 fail");
        }

        if(mSp !=null) {
            mSp.close();
        }
    }

如要直接使用SO文件,路径在app\build\intermediates\ndk\debug\lib

配值build.gradle

android {
    .........
    defaultConfig {
        applicationId "com.wy.interphone"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            moduleName "serial_port"
            ldLibs "log"
        }
    }

    sourceSets{
        main {
            jni.srcDirs = []
        }
    }
    .........
}

上面的方法要被弃用

更新使用CMakeLists.txt(文件放在app根目录)

# For more information about using CMake with Android Studio, read the

#documentation: https://d.android.com/studio/projects/add-native-code.html
#Sets the minimum version of CMake required to build the native library.
#CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)

#Creates and names a library, sets it as either STATIC
#or SHARED, and provides the relative paths to its source code.
#You can define multiple libraries, and CMake builds them for you.
#Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
# 设置so文件名称.

serial_port

# Sets the library as a shared library.

SHARED

# 设置这个so文件为共享.
# Provides a relative path to your source file(s).
# 设置这个so文件为共享.

src/main/jni/SerialPort.c)

#Searches for a specified prebuilt library and stores the path as a
#variable. Because CMake includes system libraries in the search path by
#default, you only need to specify the name of the public NDK library
#you want to add. CMake verifies that the library exists before
#completing its build.

find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )

#Specifies libraries CMake should link to your target library. You
#can link multiple libraries, such as libraries you define in this
#build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.

# 制定目标库.

serial_port

# Links the target library to the log library
# included in the NDK.
${log-lib} )

配值build.gradle

externalNativeBuild {
    cmake {
        //abiFilters 'armeabi-v7a'
        cppFlags ""
        arguments "-DANDROID_STL=c++_shared"
    }
}

 

// 配置CMakeLists.txt路径
externalNativeBuild {
    cmake {
        path "CMakeLists.txt" //编译后so文件的名字
    }
}
    defaultConfig {
        applicationId "com.wy.interphone"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//        ndk {
//            moduleName "serial_port"
//            ldLibs "log"
//        }
        externalNativeBuild {
            cmake {
                //abiFilters 'armeabi-v7a'
                cppFlags ""
                arguments "-DANDROID_STL=c++_shared"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.reselase
        }
        debug {
            signingConfig signingConfigs.debug
        }

//        sourceSets{
//            main {
//                jni.srcDirs = []
//                jniLibs.srcDirs = ['src/main/jniLibs']
//            }
//        }
    }

    // 配置CMakeLists.txt路径
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt" //编译后so文件的名字
        }
    }

权限

注意在驱动在直接修改节点权限,会上层访问的时候没权限

要值init.rc中修改节点权限


    增加对讲机权限

diff --git a/frameworks/base/data/etc/privapp-permissions-platform.xml b/frameworks/base/data/etc/privapp-permissions-platform.xml
index f847d63..47cf2ba 100755
--- a/frameworks/base/data/etc/privapp-permissions-platform.xml
+++ b/frameworks/base/data/etc/privapp-permissions-platform.xml
@@ -401,5 +401,9 @@ applications that come with the platform
     
         
     
+
+    
+        
+    

 





commit f376082c377e2ece0b36f3968043f855c1483a0c
Author: fuzhong 
Date:   Sat Nov 24 09:04:37 2018 +0800

    增加对讲机权限

diff --git a/frameworks/base/data/etc/privapp-permissions-platform.xml b/frameworks/base/data/etc/privapp-permissions-platform.xml
index f847d63..47cf2ba 100755
--- a/frameworks/base/data/etc/privapp-permissions-platform.xml
+++ b/frameworks/base/data/etc/privapp-permissions-platform.xml
@@ -401,5 +401,9 @@ applications that come with the platform
     
         
     
+
+    
+        
+    

 
wuyu@ubuntu:~/wuyu_share/CM10$ git show 4b5edc852d73532d5625456623a326468ef98418
commit 4b5edc852d73532d5625456623a326468ef98418
Author: fangwj 
Date:   Sat Nov 10 14:00:26 2018 +0800

    CM10项目解决对讲模块APK调用文件节点权限修改  20181110

diff --git a/device/sprd/sharkle/common/rootdir/root/init.common.rc b/device/sprd/sharkle/common/rootdir/root/init.common.rc
index a14444f..54b3108 100755
--- a/device/sprd/sharkle/common/rootdir/root/init.common.rc
+++ b/device/sprd/sharkle/common/rootdir/root/init.common.rc
@@ -420,6 +420,13 @@ on early-boot
     chown system system /sys/class/modem/debug-log/freq
     chown system system /sys/class/modem/debug-log/channel

+#WUYU  interphone
+       chmod 0666 /dev/ttyS1
+       chmod 0666 /sys/devices/virtual/interphone/am2120u/am2120u_mode
+       chmod 0666 /sys/devices/virtual/interphone/am2120u/am2120u_send
+       chmod 0666 /sys/devices/virtual/interphone/am2120u/am2120u_receive
+       chmod 0666 /sys/devices/virtual/amplifier/aw87319/mode
+
     # ETB info file
     chown system system /dev/1003000.tmc
     chmod 660 /dev/1003000.tmc
diff --git a/device/sprd/sharkle/common/sepolicy/file.te b/device/sprd/sharkle/common/sepolicy/file.te
index 0aa92d8..331b733 100644
--- a/device/sprd/sharkle/common/sepolicy/file.te
+++ b/device/sprd/sharkle/common/sepolicy/file.te
@@ -64,4 +64,6 @@ type proc_wifi_dbg, fs_type;
 type ylog_lite_file,file_type, data_file_type;
 type debugfs_binder, fs_type, debugfs_type;

+#add for interphone
+type sysfs_interphone, fs_type, sysfs_type, mlstrustedobject;

diff --git a/device/sprd/sharkle/common/sepolicy/file_contexts b/device/sprd/sharkle/common/sepolicy/file_contexts
index ac26d9e..eae2248 100644
--- a/device/sprd/sharkle/common/sepolicy/file_contexts
+++ b/device/sprd/sharkle/common/sepolicy/file_contexts
@@ -277,3 +277,10 @@

 #add for face unlock
 /dev/map_user           u:object_r:map_device:s0
+
+#add for interphone
+/sys/devices/virtual/interphone/am2120u/am2120u_mode                   u:object_r:sysfs_interphone:s0
+/sys/devices/virtual/interphone/am2120u/am2120u_send                   u:object_r:sysfs_interphone:s0
+/sys/devices/virtual/interphone/am2120u/am2120u_receive                u:object_r:sysfs_interphone:s0
+/sys/devices/virtual/amplifier/aw87319/mode                                    u:object_r:sysfs_interphone:s0
+
diff --git a/device/sprd/sharkle/common/sepolicy/priv_app.te b/device/sprd/sharkle/common/sepolicy/priv_app.te
index 0c895fd..0a60049 100755
--- a/device/sprd/sharkle/common/sepolicy/priv_app.te
+++ b/device/sprd/sharkle/common/sepolicy/priv_app.te
@@ -24,3 +24,8 @@ allow priv_app security_service:service_manager { find };

 # add for coredump
 allow priv_app coredump_file:dir { write add_name };
+
+#add for interphone
+allow priv_app serial_device:chr_file {open read write ioctl getattr};
+allow priv_app sysfs_interphone:file write;
+
diff --git a/device/sprd/sharkle/common/sepolicy/system_app.te b/device/sprd/sharkle/common/sepolicy/system_app.te
index e22fef9..444ea86 100644
--- a/device/sprd/sharkle/common/sepolicy/system_app.te
+++ b/device/sprd/sharkle/common/sepolicy/system_app.te
@@ -107,3 +107,7 @@ allow system_app ir_device:chr_file rw_file_perms;

 # for keybox property get
 allow system_app teetz_device:chr_file  {read write open ioctl };
+
+#add for interphone
+allow system_app serial_device:chr_file {open read write ioctl getattr};
+allow system_app sysfs_interphone:file write;

 

 

补充一些framework按键的修改

            case KeyEvent.KEYCODE_WINDOW: {
                if (mShortPressWindowBehavior == SHORT_PRESS_WINDOW_PICTURE_IN_PICTURE) {
                    if (mPictureInPictureVisible) {
                        // Consumes the key only if picture-in-picture is visible to show
                        // picture-in-picture control menu. This gives a chance to the foreground
                        // activity to customize PIP key behavior.
                        if (!down) {
                            showPictureInPictureMenu(event);
                        }
                        result &= ~ACTION_PASS_TO_USER;
                    }
                }
                break;
            }
			case KeyEvent.KEYCODE_F8: {//add by wuyu for interphone
			    Log.e("wuyu","KEYCODE_F8 down="+down);
                
			    WindowManager.LayoutParams attr = mFocusedWindow != null ? mFocusedWindow.getAttrs() : null;
					if((attr.packageName.startsWith("com.sprd.validationtools")) || (attr.packageName.startsWith("com.sprd.factorymode"))){
							Log.e("wuyu","KEYCODE_F8 down 2333333="+down);
					}else{
						if (attr.packageName.startsWith("com.wy.interphone")){
							if (!down) {
								Log.e("wuyu","F8 111111111111111 up");
								final Intent intent_up = new Intent("com.wy.interphone.InterphoneReceiver_F8up");
								intent_up.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
								intent_up.setPackage("com.wy.interphone");
								mContext.sendBroadcast(intent_up);
							}
							else
							{		
								Log.e("wuyu","F8 111111111111111 down");
								final Intent intent_down = new Intent("com.wy.interphone.InterphoneReceiver_F8down");
								intent_down.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
								intent_down.setPackage("com.wy.interphone");
								mContext.sendBroadcast(intent_down);
							
							}
						}else{
							if (down) {
		         		if (event.getRepeatCount() == 0) {
		            	Log.d(TAG, "event.getRepeatCount() == 0");
		          		mHandler.postDelayed(mSentCaronPTT, 1000);
		  					}
		       		}else{
		            	mHandler.removeCallbacks(mSentCaronPTT);                 	
		       		}
		  			}
  			}
				break;
            }
			case KeyEvent.KEYCODE_F3: {//add by wuyu for interphone
				Log.e("wuyu","KEYCODE_F3 down="+down);
				//WindowManager.LayoutParams attr = mFocusedWindow != null ? mFocusedWindow.getAttrs() : null;
				//if (attr == null || attr.packageName == null || attr.packageName.equals("com.wy.interphone")) {
					//do nothing
				//}
				//else			
                
				{	
					if (!down) {
						Log.e("wuyu","F3 111111111111111 up");
						final Intent intent_up = new Intent("com.wy.interphone.InterphoneReceiver_F3up");
						intent_up.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
						intent_up.setPackage("com.wy.interphone");
						mContext.sendBroadcast(intent_up);
					}
					else
					{		
						Log.e("wuyu","F3 111111111111111 down");
						final Intent intent_down = new Intent("com.wy.interphone.InterphoneReceiver_F3down");
						intent_down.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
						intent_down.setPackage("com.wy.interphone");
						mContext.sendBroadcast(intent_down);
					
					}
				}
				break;
            }
            // Caron-fuzhong added on 2018-11-14 start:
            /*case KeyEvent.KEYCODE_F9:{
                if(event.getAction() == KeyEvent.ACTION_DOWN){									
                    pttDownSendBroadcast();                         
                }else if(event.getAction() == KeyEvent.ACTION_UP){                                  
                    pttUpSendBroadcast();                               
                }
                break;
            }*/
            // Caron-fuzhong added on 2018-11-14 end
			case KeyEvent.KEYCODE_F5: {//add by wuyu for interphone
				Log.e("wuyu","KEYCODE_F5 down="+down);
				//WindowManager.LayoutParams attr = mFocusedWindow != null ? mFocusedWindow.getAttrs() : null;
				//if (attr == null || attr.packageName == null || attr.packageName.equals("com.wy.interphone")) {
					//do nothing
				//}
				//else
				{	
					if (!down) {
						Log.e("wuyu","F5 111111111111111 up");
						final Intent intent_up = new Intent("com.wy.interphone.InterphoneReceiver_F5up");
						intent_up.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
						intent_up.setPackage("com.wy.interphone");
						mContext.sendBroadcast(intent_up);
					}
				}
				break;
            }
			case KeyEvent.KEYCODE_F6: {//add by wuyu for interphone
				Log.e("wuyu","KEYCODE_F6 down="+down);
				//WindowManager.LayoutParams attr = mFocusedWindow != null ? mFocusedWindow.getAttrs() : null;
				//if (attr == null || attr.packageName == null || attr.packageName.equals("com.wy.interphone")) {
					//do nothing
				//}
				//else
				{	
					if (!down) {
						Log.e("wuyu","F6 111111111111111 up");
						final Intent intent_up = new Intent("com.wy.interphone.InterphoneReceiver_F6up");
						intent_up.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
						intent_up.setPackage("com.wy.interphone");
						mContext.sendBroadcast(intent_up);
					}
				}
				break;
            }

 

你可能感兴趣的:(andoid 串口通讯发射AT指令)