Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- 此程式碼單純用來測試速度用 (伺服端)
- https://www.plurk.com/p/lhxlfk
- */
- package org.spptest2;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothServerSocket;
- import android.bluetooth.BluetoothSocket;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.UUID;
- public class MainActivity extends AppCompatActivity {
- final static private String SERVICE_UUID = "00001101-0000-1000-8000-00805F9B34FB";
- static private String NAME = "bluetoothComm";
- //請參照我貼的 client 版本
- static private MT cmt = new MT();
- static private IT cit = new IT();
- static private ST cst = new ST();
- static class MT extends Thread{
- public void run(){
- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- adapter.enable();
- try {
- while (!adapter.isEnabled()){
- sleep(10);
- }
- } catch (InterruptedException e) {
- push(e.getMessage());
- e.printStackTrace();
- return;
- }
- push("self address: "+adapter.getAddress());
- BluetoothServerSocket serverSocket = null;
- try {
- while (true){
- //也許是我忘記 close() 的關係,導致下一次新連線無法正常使用
- serverSocket = adapter.listenUsingRfcommWithServiceRecord(NAME, UUID.fromString(SERVICE_UUID));
- if(serverSocket==null){
- push("serverSocket==null");
- return;
- }
- try{
- BluetoothSocket socket = serverSocket.accept();
- push("here comes: " + socket.getRemoteDevice().getAddress());
- OutputStream outputStream = socket.getOutputStream();
- InputStream inputStream = socket.getInputStream();
- cit.a(inputStream);
- push("start!");
- byte d = 0;
- while (true) {
- outputStream.write(0xFF&d++);
- //實測結果,兩邊同時發送資料是不會影響傳輸速率的
- //sleep(1);
- //if(!socket.isConnected())break;
- }
- }catch (IOException e){
- push(e.getMessage());
- e.printStackTrace();
- }
- }
- } catch (IOException e) {
- push(e.getMessage());
- e.printStackTrace();
- }
- /*catch (InterruptedException e) {
- e.printStackTrace();
- }*/
- }
- }
- static private class IT extends Thread{
- private InputStream is = null;
- private Object s = new Object();
- public void a(InputStream stream){
- is = stream;
- synchronized (s){
- s.notifyAll();
- }
- }
- public void run(){
- while (true){
- if(is==null){
- try {
- synchronized (s) {
- s.wait();
- }
- } catch (InterruptedException e) {
- return;
- }
- }
- try {
- is.read();
- bps++;
- } catch (IOException e) {
- is=null;
- push(e.getMessage());
- e.printStackTrace();
- }
- }
- }
- }
- static private class ST extends Thread{
- public void run(){
- Object s = new Object();
- try {
- while (true){
- synchronized (s){
- s.wait(1000);
- }
- handler.sendEmptyMessage(-1);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- final static private String EMPTY = new String();
- static private void push(String m){
- for (int i=message.length-1;i>0;){
- if(message[i]==null){
- message[i] = EMPTY;
- }else {
- message[i] = message[--i];
- }
- }
- message[0]=m;
- }
- static private int bps = 0;
- static private String[] message = new String[20];
- static private boolean inited = false;
- static private TextView tv = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView)findViewById(R.id.message);
- if(!inited) {
- inited = true;
- cmt.start();
- cit.start();
- cst.start();
- }
- }
- static private Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- if(msg.what==-1){
- StringBuilder sb = new StringBuilder();
- sb.append(bps*8);
- bps = 0;
- sb.append(" bps\n");
- for(String i : message){
- sb.append(i);
- sb.append("\n");
- }
- if(tv!=null){
- tv.setText(sb);
- }
- super.handleMessage(msg);
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement