JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/ADC/LED

1.TextView 滚动拉动

2.byte 转 Strings

3. Thread

4.Handle

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="    android:orientation="vertical" >

    <TextView
        android:id="@+id/receiveTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sendSerial"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send" />

        <Button
            android:id="@+id/clearButton"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear" />

        <Button
            android:id="@+id/openSerial"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OPEN" />

        <Button
            android:id="@+id/closeSerial"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close" />
    </LinearLayout>
   
   
    <EditText
        android:id="@+id/sendEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
    </EditText>

   
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
           <TextView
        android:id="@+id/showTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />
           
        </LinearLayout>
    </ScrollView>

</LinearLayout>

package com.friendlyarm.LEDDemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import java.util.Arrays;
import android.widget.TextView;

import com.friendlyarm.AndroidSDK.HardwareControler;

public class serial extends Activity{
     private TextView fdText;
     private TextView showText;
     private EditText sendEditText;

     private Button closeSerial;
     private Button sendSerial;
     private Button openSerial,clearShow;
     private int fd;

     protected static final int SHOWDATA = 0x1234;
  protected static final int SENDOK = 0x1235;
  public Handler mHandler;
  
  private boolean threadDisable=false;
  String recdata;
  
/**
*接收函数
*/

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.serial);
          
           fdText=(TextView) findViewById(R.id.receiveTextView);
           showText=(TextView) findViewById(R.id.showTextView);
          
           sendEditText=(EditText) findViewById(R.id.sendEditText);
           closeSerial = (Button) findViewById(R.id.closeSerial);
           sendSerial = (Button) findViewById(R.id.sendSerial);
           openSerial = (Button) findViewById(R.id.openSerial);
           clearShow = (Button) findViewById(R.id.clearButton);
          
       closeSerial.setOnClickListener(new Button.OnClickListener(){
           @Override
           public void onClick(View v) {
            //TODO Auto-generated method stub
            HardwareControler.close(fd);
            fdText.setText("关闭串口");
            openSerial.setEnabled(true);
            threadDisable = true;
           }
       });
       openSerial.setOnClickListener(new Button.OnClickListener(){
           @Override
           public void onClick(View v) {
            //TODO Auto-generated method stub
         fd=HardwareControler.openSerialPort("/dev/s3c2410_serial3",115200,8,1);
            if(fd !=-1)
             Toast.makeText(serial.this,"OPEN SUCCESS", Toast.LENGTH_SHORT).show();
            else
             Toast.makeText(serial.this,"OPEN FAIL```", Toast.LENGTH_SHORT).show();
            fdText.setText(Integer.toString(fd));
            fdText.setText(fdText.getText() + "打开线程");           
            openSerial.setEnabled(false);

            new Thread(new Runnable() {
       @Override
       public void run() {
        while (!threadDisable) {
         
         try {
          Thread.sleep(1000);
         } catch (InterruptedException e) {
          e.printStackTrace();
         }
     
         int m=HardwareControler.select(fd,0,0);
       if(m==1)
              {
                 byte[] buf =new byte[1024];
                 try {
                  Thread.sleep(10);
                 } catch(InterruptedException e) {
                  //TODO Auto-generated catch block
                  e.printStackTrace();
                 }
                
               int n = HardwareControler.read(fd, buf, 1024);
                String ss = new String(buf,0,n);//将bytes转为 String
                Message s = new Message();//定义一个message的变量m       
                  s.what = serial.SHOWDATA;//消息的标记GUINOTIFIER在前面定义的
                  s.obj =ss; //将要传送的数据传递给 m.obj                     
                  serial.this.mHandler.sendMessage(s);//传送消息
                }       
        }
       }
      }).start();

           }
       });
       /**
        * 发送按钮
        */
       sendSerial.setOnClickListener(new Button.OnClickListener(){
          public void onClick(View v){
              SendSerial();
          }
       });
      
       clearShow.setOnClickListener(new Button.OnClickListener(){
           public void onClick(View v){
            showText.setText("");
           }
        });
      
     //创建handler
  mHandler = new Handler() {
             public void handleMessage(Message msg) {
                 switch (msg.what) {//得到Handle的通知了 这个时候你可以做相应的操作
                     case serial.SHOWDATA://tcp_server是Activity的类名 
                     // receivedata_tv.setText(""); //清空textView                   
                     // recv_tv.setText(msg.obj.toString());//设置textView显示内容 每次都清除上次的显示
                      showText.setMovementMethod(new ScrollingMovementMethod());
                      showText.append(""+ msg.obj.toString());//设置textView显示内容 下一行接着显示
                         break;
                   //  case serial.SENDOK:
                      //.setText("");
                 }
                 super.handleMessage(msg);
             }
         };
    }
  
   public void SendSerial(){
       HardwareControler.write(fd,sendEditText.getText().toString().getBytes());
       sendEditText.setText("");
    };
}
  

package com.friendlyarm.AndroidSDK;
import android.util.Log;

public class HardwareControler
{
 /* Serial Port */
 static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits );
 
 /* LED */
 static public native int setLedState( int ledID, int ledState );
   
    /* PWM */
 static public native int PWMPlay(int frequency);
 static public native int PWMStop();
   
    /* ADC */
 static public native int readADC();
 
 /* I2C */
 static public native int openI2CDevice();
 static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
 static public native int readByteDataFromI2C(int fd, int pos);
 
 /* 通用接口 */
 static public native int write(int fd, byte[] data);
 static public native int read(int fd, byte[] buf, int len);
 static public native int select(int fd, int sec, int usec);
 static public native void close(int fd);
   
    static {
        try {
         System.loadLibrary("friendlyarm-hardware");
        } catch (UnsatisfiedLinkError e) {
            Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
        }
    }
}

相关推荐