1 / 9

專題進度報告

專題進度報告. 組員 : 吳家齊、江弘喻. package com.example.thinbtclient ; 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 ;

rodney
Télécharger la présentation

專題進度報告

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. package com.example.thinbtclient; • 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 { • BufferedReadermText = new BufferedReader(newInputStreamReader(System.in)); • } • 這邊是輸入 • double x = 0; • double y = 0; 這兩個都是宣告 • /** Called when the activity is first created. */ • private TextViewmtext; • private LinearLayoutmyLayout; • private static final String TAG = "THINBTCLIENT"; • private static final boolean D = true; • private BluetoothAdaptermBluetoothAdapter = null; • private BluetoothSocketbtSocket = null; • private InputStreaminStream = null; • private OutputStreamoutStream = null; • private PrintStream out = null; • private static final UUID MY_UUID = UUID.fromString("10203040-5060-7080-90A0-B0C0D0E0F099"); • private static String address = "00:15:83:49:42:47"; • 這些是藍芽的設定

  4. @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 OnTouchListenermTouchListener = new OnTouchListener() • { • public booleanonTouch(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) { • try { • btSocket.close(); • } catch (IOException e2) { • Log.e(TAG,"ON RESUME: Unable to close socket during connection failure", e2); • } • } • 尋找藍芽發送端的通道

  8. 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(); • try { • outStream.write(msgBuffer); • } catch (IOException e) { • Log.e(TAG, "ON RESUME: Exception during write.", e); • } • } • 印出Stream的訊息

  9. @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); • } • } • 關閉藍芽

More Related