Android Service和Activity基于串口蓝牙模块的双向通信【第二篇】

一直以来都想利用手机来控制一些东西,比如电灯,电风扇等家电或者智能小车等.

驱动蓝牙模块可以在Activity中直接调用,也可以在多线程下直接使用,但这样会存在一个缺陷:当你按下手机的Home或者Back键的时候.程序退出了,下次你重新启动软件的时候又需要重新建立蓝牙的链接了.

     为了克服以上问题,我把蓝牙模块的调用放到Service里面使用.首先对Service说明下:(来源于http://tianrui-wang-163-com.iteye.com/blog/983099)

Service介绍
Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序。

由于没有可视化界面,Service都是从其它程序组件中启动、停止和控制,这些组件包括其它的Service、Activity和Broadcast Receiver。如果你的应用程序正常且不间断的运行,而不直接依赖于用户输入,Service是你最佳的选择。

Service生命周期

服务常用生命周期回调方法如下:
onCreate() 该方法在服务被创建时调用,该方法只会被调用一次,无论调用多少次startService()或bindService()方法,服务也只被创建一次。
onDestroy()该方法在服务被终止时调用。

Service对象不能自己启动,需要通过某个Activity、Service或者其他Context对象来启动。启动的方法有两种,Context.startService和Context.bindService()。两种方式的生命周期是不同的,具体如下所示。
Context.startService方式的生命周期:
启动时,startService –> onCreate() –> onStart()
停止时,stopService –> onDestroy()
Context.bindService方式的生命周期:
绑定时,bindService  -> onCreate() –> onBind()
解绑定时,unbindService –>onUnbind() –> onDestory()

Service实现
定义一个Service只需要如下两步:

第一步:继承Service类
public class SMSService extends Service { } 这里可以选择要实现的方法
第二步:在AndroidManifest.xml文件中的<application>节点里对服务进行配置:
<service android:name=".SMSService" ”></service>

 

好了,废话少说,下面从我的代码直接开始:

[java] view plain copy print ?
  1. package com.lxx;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.util.UUID;  
  8.   
  9. import android.app.Service;  
  10. import android.bluetooth.BluetoothAdapter;  
  11. import android.bluetooth.BluetoothDevice;  
  12. import android.bluetooth.BluetoothSocket;  
  13. import android.content.BroadcastReceiver;  
  14. import android.content.Context;  
  15. import android.content.Intent;  
  16. import android.content.IntentFilter;  
  17. import android.os.IBinder;  
  18. import android.util.Log;  
  19.   
  20.   
  21. public class MyService extends Service{  
  22.   
  23.     public boolean threadFlag = true;  
  24.     MyThread myThread;  
  25.     CommandReceiver cmdReceiver;//继承自BroadcastReceiver对象,用于得到Activity发送过来的命令   
  26.   
  27.     /**************service 命令*********/   
  28.     static final int CMD_STOP_SERVICE = 0x01;  
  29.     static final int CMD_SEND_DATA = 0x02;  
  30.     static final int CMD_SYSTEM_EXIT =0x03;  
  31.     static final int CMD_SHOW_TOAST =0x04;  
  32.       
  33.      private BluetoothAdapter mBluetoothAdapter = null;  
  34.      private BluetoothSocket btSocket = null;  
  35.      private OutputStream outStream = null;  
  36.      private InputStream  inStream = null;  
  37.      public  boolean bluetoothFlag  = true;  
  38.      private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  
  39.      private static String address = "00:19:5D:EE:9B:8F"// <==要连接的蓝牙设备MAC地址   
  40.       
  41.     @Override  
  42.     public IBinder onBind(Intent intent) {  
  43.         // TODO Auto-generated method stub   
  44.         return null;  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onCreate() {  
  49.         // TODO Auto-generated method stub   
  50.         super.onCreate();  
  51.           
  52.     }  
  53.       
  54.       
  55.       
  56.     //前台Activity调用startService时,该方法自动执行   
  57.     @Override  
  58.     public int onStartCommand(Intent intent, int flags, int startId) {  
  59.         // TODO Auto-generated method stub   
  60.         cmdReceiver = new CommandReceiver();  
  61.         IntentFilter filter = new IntentFilter();//创建IntentFilter对象   
  62.         //注册一个广播,用于接收Activity传送过来的命令,控制Service的行为,如:发送数据,停止服务等   
  63.         filter.addAction("android.intent.action.cmd");  
  64.         //注册Broadcast Receiver   
  65.         registerReceiver(cmdReceiver, filter);  
  66.         doJob();//调用方法启动线程   
  67.         return super.onStartCommand(intent, flags, startId);  
  68.   
  69.     }  
  70.       
  71.       
  72.   
  73.     @Override  
  74.     public void onDestroy() {  
  75.         // TODO Auto-generated method stub   
  76.         super.onDestroy();  
  77.         this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver   
  78.         threadFlag = false;  
  79.         boolean retry = true;  
  80.         while(retry){  
  81.             try{   
  82.                  myThread.join();  
  83.                  retry = false;  
  84.             }catch(Exception e){  
  85.                 e.printStackTrace();  
  86.             }  
  87.               
  88.         }  
  89.     }  
  90.       
  91.     public class MyThread extends Thread{          
  92.         @Override  
  93.         public void run() {  
  94.             // TODO Auto-generated method stub   
  95.             super.run();  
  96.             connectDevice();//连接蓝牙设备   
  97.          while(threadFlag){  
  98.              int value = readByte();              
  99.             if(value != -1){                  
  100.                 DisplayToast(value + "");         
  101.             }  
  102.               
  103.             try{  
  104.                 Thread.sleep(50);  
  105.             }catch(Exception e){  
  106.                 e.printStackTrace();  
  107.             }              
  108.          }  
  109.        }      
  110.     }  
  111.       
  112.     public void doJob(){      
  113.          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  114.         if (mBluetoothAdapter == null) {   
  115.             DisplayToast("蓝牙设备不可用,请打开蓝牙!");  
  116.             bluetoothFlag  = false;  
  117.             return;  
  118.         }  
  119.   
  120.         if (!mBluetoothAdapter.isEnabled()) {  
  121.             DisplayToast("请打开蓝牙并重新运行程序!");  
  122.             bluetoothFlag  = false;  
  123.             stopService();  
  124.             showToast("请打开蓝牙并重新运行程序!");  
  125.             return;  
  126.         }        
  127.         showToast("搜索到蓝牙设备!");        
  128.         threadFlag = true;    
  129.         myThread = new MyThread();  
  130.         myThread.start();  
  131.           
  132.     }  
  133.     public  void connectDevice(){   
  134.         DisplayToast("正在尝试连接蓝牙设备,请稍后····");  
  135.         BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);  
  136.         try {  
  137.            btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);  
  138.         } catch (IOException e) {  
  139.             DisplayToast("套接字创建失败!");  
  140.             bluetoothFlag = false;  
  141.         }  
  142.         DisplayToast("成功连接蓝牙设备!");  
  143.         mBluetoothAdapter.cancelDiscovery();  
  144.         try {  
  145.                 btSocket.connect();  
  146.                 DisplayToast("连接成功建立,可以开始操控了!");  
  147.                 showToast("连接成功建立,可以开始操控了!");  
  148.                 bluetoothFlag = true;  
  149.         } catch (IOException e) {  
  150.                try {  
  151.                     btSocket.close();  
  152.                     bluetoothFlag = false;  
  153.                 } catch (IOException e2) {                          
  154.                    DisplayToast("连接没有建立,无法关闭套接字!");  
  155.                 }  
  156.         }     
  157.           
  158.         if(bluetoothFlag){  
  159.             try {  
  160.                 inStream = btSocket.getInputStream();  
  161.               } catch (IOException e) {  
  162.                   e.printStackTrace();  
  163.               } //绑定读接口   
  164.                 
  165.               try {  
  166.                     outStream = btSocket.getOutputStream();  
  167.                 } catch (IOException e) {  
  168.                     e.printStackTrace();  
  169.                 } //绑定写接口   
  170.               
  171.         }  
  172.     }    
  173.       
  174.     public void sendCmd(byte cmd, int value)//串口发送数据   
  175.     {     
  176.         if(!bluetoothFlag){  
  177.             return;  
  178.         }  
  179.         byte[] msgBuffer = new byte[5];                                   
  180.           msgBuffer[0] = cmd;  
  181.           msgBuffer[1] = (byte)(value >> 0  & 0xff);  
  182.           msgBuffer[2] = (byte)(value >> 8  & 0xff);  
  183.           msgBuffer[3] = (byte)(value >> 16 & 0xff);  
  184.           msgBuffer[4] = (byte)(value >> 24 & 0xff);  
  185.             
  186.           try {  
  187.             outStream.write(msgBuffer, 05);  
  188.             outStream.flush();  
  189.           } catch (IOException e) {  
  190.               e.printStackTrace();  
  191.           }           
  192.     }    
  193.       
  194.     public int readByte(){//return -1 if no data   
  195.         int ret = -1;     
  196.         if(!bluetoothFlag){  
  197.             return ret;  
  198.         }  
  199.         try {  
  200.               ret = inStream.read();  
  201.             } catch (IOException e) {  
  202.               e.printStackTrace();  
  203.             }              
  204.         return ret;  
  205.     }  
  206.       
  207.     public void stopService(){//停止服务       
  208.         threadFlag = false;//停止线程   
  209.         stopSelf();//停止服务   
  210.     }  
  211.       
  212.     public void showToast(String str){//显示提示信息   
  213.         Intent intent = new Intent();  
  214.         intent.putExtra("cmd", CMD_SHOW_TOAST);  
  215.         intent.putExtra("str", str);  
  216.         intent.setAction("android.intent.action.lxx");  
  217.         sendBroadcast(intent);    
  218.     }  
  219.       
  220.     public void DisplayToast(String str)  
  221.     {  
  222.         Log.d("Season",str);      
  223.     }  
  224.       
  225.      //接收Activity传送过来的命令   
  226.     private class CommandReceiver extends BroadcastReceiver{  
  227.         @Override  
  228.         public void onReceive(Context context, Intent intent) {  
  229.             if(intent.getAction().equals("android.intent.action.cmd")){  
  230.                 int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息                               
  231.                   if(cmd == CMD_STOP_SERVICE){  
  232.                       stopService();  
  233.                   }    
  234.                     
  235.                   if(cmd == CMD_SEND_DATA)  
  236.                   {  
  237.                      byte command = intent.getByteExtra("command", (byte0);  
  238.                      int value =  intent.getIntExtra("value"0);  
  239.                       sendCmd(command,value);  
  240.                   }  
  241.                           
  242.             }     
  243.         }                          
  244.     }  
  245.   
  246.       
  247.       
  248. }  
package com.lxx; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.util.Log; public class MyService extends Service{ public boolean threadFlag = true; MyThread myThread; CommandReceiver cmdReceiver;//继承自BroadcastReceiver对象,用于得到Activity发送过来的命令 /**************service 命令*********/ static final int CMD_STOP_SERVICE = 0x01; static final int CMD_SEND_DATA = 0x02; static final int CMD_SYSTEM_EXIT =0x03; static final int CMD_SHOW_TOAST =0x04; private BluetoothAdapter mBluetoothAdapter = null; private BluetoothSocket btSocket = null; private OutputStream outStream = null; private InputStream inStream = null; public boolean bluetoothFlag = true; private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private static String address = "00:19:5D:EE:9B:8F"; // <==要连接的蓝牙设备MAC地址 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } //前台Activity调用startService时,该方法自动执行 @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub cmdReceiver = new CommandReceiver(); IntentFilter filter = new IntentFilter();//创建IntentFilter对象 //注册一个广播,用于接收Activity传送过来的命令,控制Service的行为,如:发送数据,停止服务等 filter.addAction("android.intent.action.cmd"); //注册Broadcast Receiver registerReceiver(cmdReceiver, filter); doJob();//调用方法启动线程 return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver threadFlag = false; boolean retry = true; while(retry){ try{ myThread.join(); retry = false; }catch(Exception e){ e.printStackTrace(); } } } public class MyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); connectDevice();//连接蓝牙设备 while(threadFlag){ int value = readByte(); if(value != -1){ DisplayToast(value + ""); } try{ Thread.sleep(50); }catch(Exception e){ e.printStackTrace(); } } } } public void doJob(){ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { DisplayToast("蓝牙设备不可用,请打开蓝牙!"); bluetoothFlag = false; return; } if (!mBluetoothAdapter.isEnabled()) { DisplayToast("请打开蓝牙并重新运行程序!"); bluetoothFlag = false; stopService(); showToast("请打开蓝牙并重新运行程序!"); return; } showToast("搜索到蓝牙设备!"); threadFlag = true; myThread = new MyThread(); myThread.start(); } public void connectDevice(){ DisplayToast("正在尝试连接蓝牙设备,请稍后····"); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { DisplayToast("套接字创建失败!"); bluetoothFlag = false; } DisplayToast("成功连接蓝牙设备!"); mBluetoothAdapter.cancelDiscovery(); try { btSocket.connect(); DisplayToast("连接成功建立,可以开始操控了!"); showToast("连接成功建立,可以开始操控了!"); bluetoothFlag = true; } catch (IOException e) { try { btSocket.close(); bluetoothFlag = false; } catch (IOException e2) { DisplayToast("连接没有建立,无法关闭套接字!"); } } if(bluetoothFlag){ try { inStream = btSocket.getInputStream(); } catch (IOException e) { e.printStackTrace(); } //绑定读接口 try { outStream = btSocket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } //绑定写接口 } } public void sendCmd(byte cmd, int value)//串口发送数据 { if(!bluetoothFlag){ return; } byte[] msgBuffer = new byte[5]; msgBuffer[0] = cmd; msgBuffer[1] = (byte)(value >> 0 & 0xff); msgBuffer[2] = (byte)(value >> 8 & 0xff); msgBuffer[3] = (byte)(value >> 16 & 0xff); msgBuffer[4] = (byte)(value >> 24 & 0xff); try { outStream.write(msgBuffer, 0, 5); outStream.flush(); } catch (IOException e) { e.printStackTrace(); } } public int readByte(){//return -1 if no data int ret = -1; if(!bluetoothFlag){ return ret; } try { ret = inStream.read(); } catch (IOException e) { e.printStackTrace(); } return ret; } public void stopService(){//停止服务 threadFlag = false;//停止线程 stopSelf();//停止服务 } public void showToast(String str){//显示提示信息 Intent intent = new Intent(); intent.putExtra("cmd", CMD_SHOW_TOAST); intent.putExtra("str", str); intent.setAction("android.intent.action.lxx"); sendBroadcast(intent); } public void DisplayToast(String str) { Log.d("Season",str); } //接收Activity传送过来的命令 private class CommandReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.intent.action.cmd")){ int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息 if(cmd == CMD_STOP_SERVICE){ stopService(); } if(cmd == CMD_SEND_DATA) { byte command = intent.getByteExtra("command", (byte) 0); int value = intent.getIntExtra("value", 0); sendCmd(command,value); } } } } }

 

主界面Activity只有一个按钮,就是通过Broadcast来想Service发送数据,Service收到数据进行命令等的解析,然后调用相应的函数,相当于直接调用了Service中的函数.因为Activity和Service是运行在不同的进程中的,两者不能进行直接的通讯,必须通过一个"桥梁"建立起联系才行.

[java] view plain copy print ?
  1. package com.lxx;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class BroadcastActivity extends Activity {  
  17.     /** Called when the activity is first created. */  
  18.   
  19.     TextView myTextView;  
  20.     Button sendButton;  
  21.     MyReceiver receiver;  
  22.     IBinder serviceBinder;  
  23.     MyService mService;  
  24.     Intent intent;  
  25.     int value = 0;  
  26.       
  27.     /**************service 命令*********/   
  28.     static final int CMD_STOP_SERVICE = 0x01;  
  29.     static final int CMD_SEND_DATA = 0x02;  
  30.     static final int CMD_SYSTEM_EXIT =0x03;  
  31.     static final int CMD_SHOW_TOAST =0x04;  
  32.       
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.           
  38.         myTextView = (TextView)findViewById(R.id.myTextView);  
  39.         myTextView.setText("Season");    
  40.         sendButton = (Button)findViewById(R.id.sendButton);  
  41.         sendButton.setOnClickListener(new SendButtonClickListener());  
  42.                     
  43.         intent = new Intent(BroadcastActivity.this,MyService.class);  
  44.         startService(intent);  
  45.     }  
  46.       
  47.     
  48.     public class SendButtonClickListener implements OnClickListener{  
  49.   
  50.         @Override  
  51.         public void onClick(View v) {  
  52.             // TODO Auto-generated method stub   
  53.             byte command = 45;  
  54.             int value = 0x12345;  
  55.             sendCmd(command,value);  
  56.         }     
  57.     }  
  58.       
  59.     @Override  
  60.     protected void onDestroy() {  
  61.         // TODO Auto-generated method stub   
  62.         super.onDestroy();  
  63.           
  64.         if(receiver!=null){  
  65.             BroadcastActivity.this.unregisterReceiver(receiver);  
  66.         }  
  67.     }  
  68.   
  69.      
  70.   
  71.   
  72.     @Override  
  73.     protected void onResume() {  
  74.         // TODO Auto-generated method stub   
  75.         super.onResume();  
  76.         receiver = new MyReceiver();  
  77.         IntentFilter filter=new IntentFilter();  
  78.         filter.addAction("android.intent.action.lxx");  
  79.         BroadcastActivity.this.registerReceiver(receiver,filter);  
  80.     }  
  81.   
  82.     public void showToast(String str){//显示提示信息   
  83.         Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();      
  84.     }  
  85.   
  86.   
  87.     public class MyReceiver extends BroadcastReceiver{  
  88.     @Override  
  89.     public void onReceive(Context context, Intent intent) {  
  90.         // TODO Auto-generated method stub   
  91.         if(intent.getAction().equals("android.intent.action.lxx")){  
  92.             Bundle bundle = intent.getExtras();  
  93.             int cmd = bundle.getInt("cmd");  
  94.               
  95.             if(cmd == CMD_SHOW_TOAST){  
  96.                 String str = bundle.getString("str");  
  97.                 showToast(str);  
  98.             }  
  99.               
  100.             else if(cmd == CMD_SYSTEM_EXIT){  
  101.                 System.exit(0);  
  102.             }  
  103.               
  104.         }  
  105.      }     
  106.    }  
  107.   
  108.     public void sendCmd(byte command, int value){  
  109.         Intent intent = new Intent();//创建Intent对象   
  110.         intent.setAction("android.intent.action.cmd");  
  111.         intent.putExtra("cmd", CMD_SEND_DATA);  
  112.         intent.putExtra("command", command);  
  113.         intent.putExtra("value", value);  
  114.         sendBroadcast(intent);//发送广播       
  115.     }  
  116.       
  117.    
  118. }  
package com.lxx; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class BroadcastActivity extends Activity { /** Called when the activity is first created. */ TextView myTextView; Button sendButton; MyReceiver receiver; IBinder serviceBinder; MyService mService; Intent intent; int value = 0; /**************service 命令*********/ static final int CMD_STOP_SERVICE = 0x01; static final int CMD_SEND_DATA = 0x02; static final int CMD_SYSTEM_EXIT =0x03; static final int CMD_SHOW_TOAST =0x04; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myTextView = (TextView)findViewById(R.id.myTextView); myTextView.setText("Season"); sendButton = (Button)findViewById(R.id.sendButton); sendButton.setOnClickListener(new SendButtonClickListener()); intent = new Intent(BroadcastActivity.this,MyService.class); startService(intent); } public class SendButtonClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub byte command = 45; int value = 0x12345; sendCmd(command,value); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(receiver!=null){ BroadcastActivity.this.unregisterReceiver(receiver); } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); receiver = new MyReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("android.intent.action.lxx"); BroadcastActivity.this.registerReceiver(receiver,filter); } public void showToast(String str){//显示提示信息 Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); } public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals("android.intent.action.lxx")){ Bundle bundle = intent.getExtras(); int cmd = bundle.getInt("cmd"); if(cmd == CMD_SHOW_TOAST){ String str = bundle.getString("str"); showToast(str); } else if(cmd == CMD_SYSTEM_EXIT){ System.exit(0); } } } } public void sendCmd(byte command, int value){ Intent intent = new Intent();//创建Intent对象 intent.setAction("android.intent.action.cmd"); intent.putExtra("cmd", CMD_SEND_DATA); intent.putExtra("command", command); intent.putExtra("value", value); sendBroadcast(intent);//发送广播 } }


以下主要对代码部分进行详细的说明:

1.为了方便Activity和Service简历起良好的通信关系,需要在各自发送的数据进行命令的解释,这些命令在两者之间是一致的,能够相互读懂对方发送过来的数据.

[java] view plain copy print ?
  1. /**************service 命令*********/   
  2.     static final int CMD_STOP_SERVICE = 0x01;//停止服务   
  3.     static final int CMD_SEND_DATA = 0x02;//发送数据   
  4.     static final int CMD_SYSTEM_EXIT =0x03;//退出程序   
  5.     static final int CMD_SHOW_TOAST =0x04;//界面上显示toast  
/**************service 命令*********/ static final int CMD_STOP_SERVICE = 0x01;//停止服务 static final int CMD_SEND_DATA = 0x02;//发送数据 static final int CMD_SYSTEM_EXIT =0x03;//退出程序 static final int CMD_SHOW_TOAST =0x04;//界面上显示toast


2.Service传送数据到Activity:

要接收Broadcast首先的有个Receiver:

[java] view plain copy print ?
  1. //接收Activity传送过来的命令   
  2.    private class CommandReceiver extends BroadcastReceiver{  
  3.        @Override  
  4.        public void onReceive(Context context, Intent intent) {  
  5.         if(intent.getAction().equals("android.intent.action.cmd")){  
  6.             int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息                               
  7.                  if(cmd == CMD_STOP_SERVICE){  
  8.                   stopService();  
  9.                  }    
  10.                    
  11.                  if(cmd == CMD_SEND_DATA)  
  12.                  {  
  13.                  byte command = intent.getByteExtra("command", (byte0);  
  14.                  int value =  intent.getIntExtra("value"0);  
  15.                   sendCmd(command,value);  
  16.                  }  
  17.                           
  18.         }     
  19.        }                          
  20.    }  
//接收Activity传送过来的命令 private class CommandReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.intent.action.cmd")){ int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息 if(cmd == CMD_STOP_SERVICE){ stopService(); } if(cmd == CMD_SEND_DATA) { byte command = intent.getByteExtra("command", (byte) 0); int value = intent.getIntExtra("value", 0); sendCmd(command,value); } } } }

为了能够接收到数据,首先得把这个Receiver注册:

[java] view plain copy print ?
  1. public class MyReceiver extends BroadcastReceiver{  
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.         // TODO Auto-generated method stub   
  5.         if(intent.getAction().equals("android.intent.action.lxx")){  
  6.             Bundle bundle = intent.getExtras();  
  7.             int cmd = bundle.getInt("cmd");  
  8.               
  9.             if(cmd == CMD_SHOW_TOAST){  
  10.                 String str = bundle.getString("str");  
  11.                 showToast(str);  
  12.             }  
  13.               
  14.             else if(cmd == CMD_SYSTEM_EXIT){  
  15.                 System.exit(0);  
  16.             }  
  17.               
  18.         }  
  19.      }     
  20.    }  
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals("android.intent.action.lxx")){ Bundle bundle = intent.getExtras(); int cmd = bundle.getInt("cmd"); if(cmd == CMD_SHOW_TOAST){ String str = bundle.getString("str"); showToast(str); } else if(cmd == CMD_SYSTEM_EXIT){ System.exit(0); } } } }



 

其中doJob是启动一个线程,定时的去读取蓝牙串口的数据,然后根据里面的数据解析就可以实现不同的操作了,这个反过来就是外设直接控制手机了.因为这个线程是一直在后台运行着的,只要不结束Service,它就一直保持这与外设串口蓝牙模块的通讯.

[java] view plain copy print ?
  1. public class MyThread extends Thread{          
  2.         @Override  
  3.         public void run() {  
  4.             // TODO Auto-generated method stub   
  5.             super.run();  
  6.             connectDevice();//连接蓝牙设备   
  7.          while(threadFlag){  
  8.              int value = readByte();//从蓝牙模块读取一个字节的数据,解释命令用             
  9.             if(value != -1){                  
  10.                 DisplayToast(value + "");         
  11.             }  
  12.               
  13.             try{  
  14.                 Thread.sleep(50);  
  15.             }catch(Exception e){  
  16.                 e.printStackTrace();  
  17.             }              
  18.          }  
  19.        }      
  20.     }  
public class MyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); connectDevice();//连接蓝牙设备 while(threadFlag){ int value = readByte();//从蓝牙模块读取一个字节的数据,解释命令用 if(value != -1){ DisplayToast(value + ""); } try{ Thread.sleep(50); }catch(Exception e){ e.printStackTrace(); } } } }

3.Activity传送数据到Service:

 Activity上有一个按钮,点击一下就可以发送数据到蓝牙串口模块,工作原理是这样的:界面上实现按钮的点击事件

[java] view plain copy print ?
  1. public class SendButtonClickListener implements OnClickListener{  
  2.   
  3.     @Override  
  4.     public void onClick(View v) {  
  5.         // TODO Auto-generated method stub   
  6.         byte command = 45;  
  7.         int value = 0x12345;  
  8.            sendCmd(command,value);  
  9.     }     
  10.    }  
public class SendButtonClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub byte command = 45; int value = 0x12345; sendCmd(command,value); } }

这里的sendCmd是关键,里面通过建立起一个广播消息,Service里会负责接收他

[java] view plain copy print ?
  1. public void sendCmd(byte command, int value){  
  2.         Intent intent = new Intent();//创建Intent对象   
  3.         intent.setAction("android.intent.action.cmd");  
  4.         intent.putExtra("cmd", CMD_SEND_DATA);  
  5.         intent.putExtra("command", command);  
  6.         intent.putExtra("value", value);  
  7.         sendBroadcast(intent);//发送广播       
  8.     }  
public void sendCmd(byte command, int value){ Intent intent = new Intent();//创建Intent对象 intent.setAction("android.intent.action.cmd"); intent.putExtra("cmd", CMD_SEND_DATA); intent.putExtra("command", command); intent.putExtra("value", value); sendBroadcast(intent);//发送广播 }

Service中

[java] view plain copy print ?
  1. //前台Activity调用startService时,该方法自动执行   
  2.     @Override  
  3.     public int onStartCommand(Intent intent, int flags, int startId) {  
  4.         // TODO Auto-generated method stub   
  5.         cmdReceiver = new CommandReceiver();  
  6.         IntentFilter filter = new IntentFilter();//创建IntentFilter对象   
  7.         //注册一个广播,用于接收Activity传送过来的命令,控制Service的行为,如:发送数据,停止服务等   
  8.         filter.addAction("android.intent.action.cmd");  
  9.         //注册Broadcast Receiver   
  10.         registerReceiver(cmdReceiver, filter);  
  11.         doJob();//调用方法启动线程   
  12.         return super.onStartCommand(intent, flags, startId);  
  13.   
  14.     }  
//前台Activity调用startService时,该方法自动执行 @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub cmdReceiver = new CommandReceiver(); IntentFilter filter = new IntentFilter();//创建IntentFilter对象 //注册一个广播,用于接收Activity传送过来的命令,控制Service的行为,如:发送数据,停止服务等 filter.addAction("android.intent.action.cmd"); //注册Broadcast Receiver registerReceiver(cmdReceiver, filter); doJob();//调用方法启动线程 return super.onStartCommand(intent, flags, startId); }

通过以上步骤就可以建立起蓝牙模块发送数据到Activity,Activity也可以发送数据到蓝牙模块了;

本例子的源码:

http://download.csdn.net/source/3557026 

博文转自:http://blog.csdn.net/cen616899547/article/category/1133607


你可能感兴趣的:(android,service,cmd,command,filter,byte)