Presentation is loading. Please wait.

Presentation is loading. Please wait.

專題進度報告 組員:吳家齊、江弘喻.

Similar presentations


Presentation on theme: "專題進度報告 組員:吳家齊、江弘喻."— Presentation transcript:

1 專題進度報告 組員:吳家齊、江弘喻

2 package com.example.thinbtclient; import java.io.*;
import java.io.*; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.InputStream; import java.util.UUID; import com.android.ViewEvent.R; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.os.Bundle; import android.util.Log; import android.widget.Toast; 把這些都import進去,因為沒有這些有下面程式碼也不能跑

3 public class ThinBTClient extends Activity {
public static void main(String[] args) throws IOException { BufferedReader mText = new BufferedReader(newInputStreamReader(System.in)); } 這邊是輸入 double x = 0; double y = 0; 這兩個都是宣告 /** Called when the activity is first created. */ private TextView mtext; private LinearLayout myLayout; private static final String TAG = "THINBTCLIENT"; private static final boolean D = true; private BluetoothAdapter mBluetoothAdapter = null; private BluetoothSocket btSocket = null; private InputStream inStream = null; private OutputStream outStream = null; private PrintStream out = null; private static final UUID MY_UUID = UUID.fromString(" A0-B0C0D0E0F099"); private static String address = "00:15:83:49:42:47"; 這些是藍芽的設定

4 public void onCreate(Bundle savedInstanceState) {
@Override 配合繼承架構, 衍生類別的虛擬函式可以取代基礎類別的同名虛擬函式, 運用多型指標時虛擬函式呼叫可以動態地繫結 (bind) 到該物件的虛擬函式上 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mtext = (TextView) this.findViewById(R.id.myText); myLayout = (LinearLayout) this.findViewById(R.id.myLinear); myLayout.setOnTouchListener(mTouchListener); 內容瀏覽 if (D) Log.e(TAG, "+++ ON CREATE +++"); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show(); finish(); return; } 藍芽沒開

5 if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this, "Please enable your BT and re-run this program.", Toast.LENGTH_LONG).show(); finish(); return; } 藍芽有開 if (D) Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++"); public OnTouchListener mTouchListener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) PrintStream p = new PrintStream( outStream ); p.print("__X: " + event.getX() + ",__Y: " + event.getY()); mtext.setText("__X: " + event.getX() + ",__Y: " + event.getY()); return true; }; 觸控面板控制滑鼠座標

6 @Override public void onResume() { super.onResume(); if (D) { Log.e(TAG, "+ ON RESUME +"); Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); } BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { Log.e(TAG, "ON RESUME: Socket creation failed.", e); 讀取藍芽裝置的編碼

7 mBluetoothAdapter.cancelDiscovery();
try { btSocket.connect(); Log.e(TAG, "ON RESUME: BT connection established, data transfer link open."); } catch (IOException e) { btSocket.close(); } catch (IOException e2) { Log.e(TAG,"ON RESUME: Unable to close socket during connection failure", e2); } 尋找藍芽發送端的通道

8 Log.e(TAG, “+ ABOUT TO SAY SOMETHING TO SERVER +”);
if (D) Log.e(TAG, “+ ABOUT TO SAY SOMETHING TO SERVER +”); try { outStream = btSocket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "ON RESUME: Output stream creation failed.", e); } String message = "Hello message from client to server."; byte[] msgBuffer = message.getBytes(); outStream.write(msgBuffer); Log.e(TAG, "ON RESUME: Exception during write.", e); 印出Stream的訊息

9 public void onPause() { super.onPause(); if (D)
@Override public void onPause() { super.onPause(); if (D) Log.e(TAG, "- ON PAUSE -"); if (outStream != null) { try { outStream.flush(); } catch (IOException e) { Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e); } try { btSocket.close(); } catch (IOException e2) { Log.e(TAG, "ON PAUSE: Unable to close socket.", e2); 關閉藍芽


Download ppt "專題進度報告 組員:吳家齊、江弘喻."

Similar presentations


Ads by Google