Advertisement
SpyMomiji

Android - Bluetooth SPP client (speed test)

Feb 25th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. /**
  2.     此程式碼單純用來測試速度用
  3.     https://www.plurk.com/p/lhxlfk
  4. */
  5.  
  6. package org.spptest;
  7.  
  8. import android.bluetooth.BluetoothAdapter;
  9. import android.bluetooth.BluetoothDevice;
  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.     final static private String SERVICE_UUID = "00001101-0000-1000-8000-00805F9B34FB";
  25.  
  26.     static private MT cmt = new MT(); //主執行緒
  27.     static private IT cit = new IT(); //InputStream 封包計數器
  28.     static private ST cst = new ST(); //介面更新計時器
  29.  
  30.     static private class MT extends Thread{
  31.         public void run(){
  32.             BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  33.             adapter.enable();
  34.  
  35.             try {
  36.                 while(!adapter.isEnabled()){
  37.                     sleep(10);
  38.                 }
  39.             } catch (InterruptedException e) {
  40.                 return;
  41.             }
  42.  
  43.             BluetoothDevice device = null;
  44.            
  45.             //如果不知道要連線哪個裝置,可以用這段來看
  46.             /*Set<BluetoothDevice> ls = adapter.getBondedDevices();
  47.             for(BluetoothDevice di : ls){
  48.                 push(di.toString());
  49.             }
  50.             //if(true)return; //臨時中斷點
  51.             */
  52.             //在這裡選擇預計要連線的裝置
  53.             device = adapter.getRemoteDevice("(MAC Address)"); //TODO 知道之後再來改
  54.  
  55.             try {
  56.                 BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
  57.                 if(socket==null){
  58.                     push("socket==null");
  59.                     return;
  60.                 }
  61.                 boolean cd = false;
  62.                 while (!cd){
  63.                     try {
  64.                         push("connecting...");
  65.                         socket.connect();
  66.                         cd = true;
  67.                     }catch (IOException e){
  68.                         sleep(5000);
  69.                     }
  70.                 }
  71.                 OutputStream outputStream = socket.getOutputStream();
  72.                 InputStream inputStream = socket.getInputStream();
  73.  
  74.                 cit.a(inputStream);
  75.  
  76.                 push("start!");
  77.  
  78.                 byte d = 0;
  79.                 while (true) {
  80.                     outputStream.write(0xFF & d++);
  81.                 }
  82.             } catch (IOException e) {
  83.                 push(e.getMessage());
  84.                 e.printStackTrace();
  85.             } catch (InterruptedException e){
  86.                 push(e.getMessage());
  87.             }
  88.  
  89.         }
  90.     }
  91.  
  92.     static private class IT extends Thread{
  93.         private InputStream is = null;
  94.         private Object s = new Object();
  95.  
  96.         public void a(InputStream stream){
  97.             is = stream;
  98.             synchronized (s){
  99.                 s.notifyAll();
  100.             }
  101.         }
  102.  
  103.         public void run(){
  104.             while (true){
  105.                 if(is==null){
  106.                     try {
  107.                         synchronized (s) {
  108.                             s.wait();
  109.                         }
  110.                     } catch (InterruptedException e) {
  111.                         return;
  112.                     }
  113.                 }
  114.                 try {
  115.                     is.read();
  116.                     bps++;
  117.                 } catch (IOException e) {
  118.                     is=null;
  119.                     push(e.getMessage());
  120.                     e.printStackTrace();
  121.                 }
  122.             }
  123.         }
  124.     }
  125.  
  126.     static private class ST extends Thread{
  127.         public void run(){
  128.             Object s = new Object();
  129.             try {
  130.                 while (true){
  131.                     synchronized (s){
  132.                         s.wait(1000);
  133.                     }
  134.                     handler.sendEmptyMessage(-1);
  135.                 }
  136.             } catch (InterruptedException e) {
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.     }
  141.  
  142.     final static private String EMPTY = new String("");
  143.  
  144.     static private void push(String m){
  145.         for (int i=message.length-1;i>0;){
  146.             if(message[i]==null){
  147.                 message[i] = EMPTY;
  148.             }else {
  149.                 message[i] = message[--i];
  150.             }
  151.         }
  152.         message[0]=m;
  153.     }
  154.  
  155.     static private int bps = 0;
  156.     static private String[] message = new String[20];
  157.     static private boolean inited = false;
  158.  
  159.     static private TextView tv = null;
  160.  
  161.     @Override
  162.     protected void onCreate(Bundle savedInstanceState) {
  163.         super.onCreate(savedInstanceState);
  164.         setContentView(R.layout.activity_main);
  165.  
  166.         tv = (TextView)findViewById(R.id.message);
  167.  
  168.         if(!inited) {
  169.             inited = true;
  170.             cmt.start();
  171.             cit.start();
  172.             cst.start();
  173.         }
  174.  
  175.     }
  176.  
  177.     static private Handler handler = new Handler(){
  178.         @Override
  179.         public void handleMessage(Message msg) {
  180.             if(msg.what==-1){
  181.                 StringBuilder sb = new StringBuilder();
  182.                 sb.append(bps*8);
  183.                 bps = 0;
  184.                 sb.append(" bps\n");
  185.                 for(String i : message){
  186.                     sb.append(i);
  187.                     sb.append("\n");
  188.                 }
  189.                 if(tv!=null){
  190.                     tv.setText(sb);
  191.                 }
  192.             }
  193.         }
  194.     };
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement