Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- 此程式碼單純用來測試速度用
- https://www.plurk.com/p/lhxlfk
- */
- package org.spptest;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- 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 MT cmt = new MT(); //主執行緒
- static private IT cit = new IT(); //InputStream 封包計數器
- static private ST cst = new ST(); //介面更新計時器
- static private class MT extends Thread{
- public void run(){
- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- adapter.enable();
- try {
- while(!adapter.isEnabled()){
- sleep(10);
- }
- } catch (InterruptedException e) {
- return;
- }
- BluetoothDevice device = null;
- //如果不知道要連線哪個裝置,可以用這段來看
- /*Set<BluetoothDevice> ls = adapter.getBondedDevices();
- for(BluetoothDevice di : ls){
- push(di.toString());
- }
- //if(true)return; //臨時中斷點
- */
- //在這裡選擇預計要連線的裝置
- device = adapter.getRemoteDevice("(MAC Address)"); //TODO 知道之後再來改
- try {
- BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
- if(socket==null){
- push("socket==null");
- return;
- }
- boolean cd = false;
- while (!cd){
- try {
- push("connecting...");
- socket.connect();
- cd = true;
- }catch (IOException e){
- sleep(5000);
- }
- }
- OutputStream outputStream = socket.getOutputStream();
- InputStream inputStream = socket.getInputStream();
- cit.a(inputStream);
- push("start!");
- byte d = 0;
- while (true) {
- outputStream.write(0xFF & d++);
- }
- } catch (IOException e) {
- push(e.getMessage());
- e.printStackTrace();
- } catch (InterruptedException e){
- push(e.getMessage());
- }
- }
- }
- 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);
- }
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement