Advertisement
SpyMomiji

Android - Bluetooth SPP server (speed test)

Feb 25th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. /**
  2.     此程式碼單純用來測試速度用 (伺服端)
  3.     https://www.plurk.com/p/lhxlfk
  4. */
  5.  
  6. package org.spptest2;
  7.  
  8. import android.bluetooth.BluetoothAdapter;
  9. import android.bluetooth.BluetoothServerSocket;
  10. import android.bluetooth.BluetoothSocket;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.widget.TextView;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.UUID;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24.  
  25.  
  26.     final static private String SERVICE_UUID = "00001101-0000-1000-8000-00805F9B34FB";
  27.     static private String NAME = "bluetoothComm";
  28.  
  29.     //請參照我貼的 client 版本
  30.     static private MT cmt = new MT();
  31.     static private IT cit = new IT();
  32.     static private ST cst = new ST();
  33.  
  34.     static class MT extends Thread{
  35.         public void run(){
  36.             BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  37.  
  38.             adapter.enable();
  39.  
  40.             try {
  41.                 while (!adapter.isEnabled()){
  42.                     sleep(10);
  43.                 }
  44.             } catch (InterruptedException e) {
  45.                 push(e.getMessage());
  46.                 e.printStackTrace();
  47.                 return;
  48.             }
  49.             push("self address: "+adapter.getAddress());
  50.  
  51.             BluetoothServerSocket serverSocket = null;
  52.             try {
  53.                 while (true){
  54.                     //也許是我忘記 close() 的關係,導致下一次新連線無法正常使用
  55.                     serverSocket = adapter.listenUsingRfcommWithServiceRecord(NAME, UUID.fromString(SERVICE_UUID));
  56.                     if(serverSocket==null){
  57.                         push("serverSocket==null");
  58.                         return;
  59.                     }
  60.                     try{
  61.                         BluetoothSocket socket = serverSocket.accept();
  62.                         push("here comes: " + socket.getRemoteDevice().getAddress());
  63.                         OutputStream outputStream = socket.getOutputStream();
  64.                         InputStream inputStream = socket.getInputStream();
  65.                         cit.a(inputStream);
  66.  
  67.                         push("start!");
  68.  
  69.                         byte d = 0;
  70.                         while (true) {
  71.                             outputStream.write(0xFF&d++);
  72.                             //實測結果,兩邊同時發送資料是不會影響傳輸速率的
  73.                             //sleep(1);
  74.                             //if(!socket.isConnected())break;
  75.                         }
  76.                     }catch (IOException e){
  77.                         push(e.getMessage());
  78.                         e.printStackTrace();
  79.                     }
  80.                 }
  81.             } catch (IOException e) {
  82.                 push(e.getMessage());
  83.                 e.printStackTrace();
  84.             }
  85.             /*catch (InterruptedException e) {
  86.                 e.printStackTrace();
  87.             }*/
  88.  
  89.  
  90.         }
  91.     }
  92.  
  93.     static private class IT extends Thread{
  94.         private InputStream is = null;
  95.         private Object s = new Object();
  96.  
  97.         public void a(InputStream stream){
  98.             is = stream;
  99.             synchronized (s){
  100.                 s.notifyAll();
  101.             }
  102.         }
  103.  
  104.         public void run(){
  105.             while (true){
  106.                 if(is==null){
  107.                     try {
  108.                         synchronized (s) {
  109.                             s.wait();
  110.                         }
  111.                     } catch (InterruptedException e) {
  112.                         return;
  113.                     }
  114.                 }
  115.                 try {
  116.                     is.read();
  117.                     bps++;
  118.                 } catch (IOException e) {
  119.                     is=null;
  120.                     push(e.getMessage());
  121.                     e.printStackTrace();
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.     static private class ST extends Thread{
  128.         public void run(){
  129.             Object s = new Object();
  130.             try {
  131.                 while (true){
  132.                     synchronized (s){
  133.                         s.wait(1000);
  134.                     }
  135.                     handler.sendEmptyMessage(-1);
  136.                 }
  137.             } catch (InterruptedException e) {
  138.                 e.printStackTrace();
  139.             }
  140.         }
  141.     }
  142.  
  143.     final static private String EMPTY = new String();
  144.  
  145.     static private void push(String m){
  146.         for (int i=message.length-1;i>0;){
  147.             if(message[i]==null){
  148.                 message[i] = EMPTY;
  149.             }else {
  150.                 message[i] = message[--i];
  151.             }
  152.         }
  153.         message[0]=m;
  154.     }
  155.  
  156.     static private int bps = 0;
  157.     static private String[] message = new String[20];
  158.     static private boolean inited = false;
  159.  
  160.     static private TextView tv = null;
  161.  
  162.     @Override
  163.     protected void onCreate(Bundle savedInstanceState) {
  164.         super.onCreate(savedInstanceState);
  165.         setContentView(R.layout.activity_main);
  166.  
  167.         tv = (TextView)findViewById(R.id.message);
  168.  
  169.         if(!inited) {
  170.             inited = true;
  171.             cmt.start();
  172.             cit.start();
  173.             cst.start();
  174.         }
  175.  
  176.     }
  177.  
  178.     static private Handler handler = new Handler(){
  179.         @Override
  180.         public void handleMessage(Message msg) {
  181.             if(msg.what==-1){
  182.                 StringBuilder sb = new StringBuilder();
  183.                 sb.append(bps*8);
  184.                 bps = 0;
  185.                 sb.append(" bps\n");
  186.                 for(String i : message){
  187.                     sb.append(i);
  188.                     sb.append("\n");
  189.                 }
  190.                 if(tv!=null){
  191.                     tv.setText(sb);
  192.                 }
  193.                 super.handleMessage(msg);
  194.             }
  195.         }
  196.     };
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement