-
-
Save anonymous/5ba4ec135b18f7967780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.modle000; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.UUID; | |
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothDevice; | |
import android.bluetooth.BluetoothSocket; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.ListView; | |
//import android.widget.ProgressBar; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import android.view.Menu; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import android.os.Environment; | |
public class bluetooth_act extends Activity{ | |
private static final String TAG = "actmainBTClient"; | |
private static final boolean D = true; | |
private ListView mCView; | |
private Button cButton; | |
private static int event_int = 0; | |
private static int msIndex = 0; | |
private static int msInC1,msInC2,msInC3,msInCER; | |
private String decideMessage = ""; | |
private BluetoothAdapter mBTAdapter = null; | |
private BluetoothSocket btSocket = null; | |
private OutputStream outStream = null; | |
private InputStream inStream = null; | |
private ArrayAdapter<String> mCArrayAdapter; | |
private ConnectedThread mConnectedThread; | |
// private ProgressBar vbar1,vbar2,vbar3; | |
// 設定UUID之值 | |
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); | |
//server的MAC位址 | |
private static String address = "00:1A:FF:09:02:D1"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.bluetooth_act); | |
if (D) | |
Log.e(TAG, "+++ ON CREATE +++"); | |
//尋找藍芽裝置 | |
mBTAdapter = BluetoothAdapter.getDefaultAdapter(); | |
//若mBluetoothAdapter為空值,表示不支援藍芽裝置 | |
if (mBTAdapter == null) { | |
Toast.makeText(this, | |
"Bluetooth is not available.", | |
Toast.LENGTH_LONG).show(); | |
finish(); | |
return; | |
} | |
//若找到藍芽裝置但未開啟,要求開啟藍芽並重啟程式 | |
if (!mBTAdapter.isEnabled()) { | |
Toast.makeText(this, | |
"請開啟藍芽後重新啟動程式", | |
Toast.LENGTH_LONG).show(); | |
finish(); | |
return; | |
} | |
if (D) | |
Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++"); | |
} | |
//開啟時動作 | |
@Override | |
public void onStart() { | |
super.onStart(); | |
if (D){ | |
Log.e(TAG, "++ ON START ++"); | |
} | |
setupChat(); | |
} | |
//重啟時動作 | |
@Override | |
public void onResume() { | |
super.onResume(); | |
if (D) { | |
Log.e(TAG, "+ ON RESUME +"); | |
Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); | |
} | |
//設定連線目標裝置的MAC位址 | |
BluetoothDevice btdevice = mBTAdapter.getRemoteDevice(address); | |
//設定連線目標裝置的UUID服務 | |
try { | |
btSocket = btdevice.createRfcommSocketToServiceRecord(MY_UUID); | |
} catch (IOException e) { | |
Log.e(TAG, "ON RESUME: Socket creation failed.", e); | |
} | |
// 取消搜詢周圍藍芽裝置 | |
mBTAdapter.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); | |
} | |
} | |
mConnectedThread = new ConnectedThread(btSocket); | |
mConnectedThread.start(); | |
} | |
private void setupChat() { | |
Log.d(TAG, "setupChat()"); | |
//設定ProgressBar | |
// vbar1 = (ProgressBar) findViewById(R.id.progressBar1); | |
// vbar2 = (ProgressBar) findViewById(R.id.progressBar2); | |
// vbar3 = (ProgressBar) findViewById(R.id.progressBar3); | |
//設定Button的click監聽 | |
cButton = (Button) findViewById(R.id.button1); | |
cButton.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
mConnectedThread.cancel(); | |
finish(); | |
} | |
}); | |
//設定ListView | |
mCArrayAdapter = new ArrayAdapter<String>(this, R.layout.message); | |
mCView = (ListView) findViewById(R.id.ListView01); | |
mCView.setAdapter(mCArrayAdapter); | |
} | |
//中斷時動作 | |
@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); | |
} | |
} | |
//停止時動作 | |
@Override | |
public void onStop() { | |
super.onStop(); | |
if (D) | |
Log.e(TAG, "-- ON STOP --"); | |
mConnectedThread.cancel(); | |
} | |
//移除時動作 | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (D) | |
Log.e(TAG, "--- ON DESTROY ---"); | |
} | |
private final Handler mHandler = new Handler(){ | |
public void handleMessage(Message msg) { | |
byte[] readBuf = (byte[])msg.obj; | |
String readMessage = new String(readBuf, 0, msg.arg1); | |
mCArrayAdapter.add("設備: " + readMessage); | |
switch (event_int){ | |
case 0: | |
String temp_val1 = new String(readMessage.substring(0, 1)); | |
if (temp_val1.equals("V") ){ | |
event_int = 1; | |
decideMessage = decideMessage.concat(readMessage); | |
} | |
break; | |
case 1: | |
decideMessage = decideMessage.concat(readMessage); | |
decideMessage = decideMessage.trim(); | |
msIndex = decideMessage.length(); | |
if (msIndex >= 20){ | |
msInC1 = decideMessage.indexOf("V1"); //V1 | |
if (msInC1 != -1){ | |
String temp_read1 = new String(decideMessage.substring(msInC1 + 3,msInC1 + 5)); | |
String outST_11 = temp_read1.substring(0, 1); | |
String outST_12 = temp_read1.substring(1, 2); | |
double v1_o1 = Double.parseDouble(hexStringToBase10(outST_11)); | |
double v1_o2 = Double.parseDouble(hexStringToBase10(outST_12)); | |
v1_o1 = v1_o1 * 16; | |
double v1 = (v1_o1 + v1_o2) / 255 * 12; | |
// double vbar_d1 = v1 * 10 ; | |
// int var_i1 = Integer.parseInt(Double.toString(vbar_d1)); | |
String V1_out = Double.toString(v1); | |
TextView mOutText01 = (TextView) findViewById(R.id.getdata01); | |
mOutText01.setText("太陽能電壓: " + V1_out); | |
// vbar1.setProgress(var_i1); | |
} | |
msInC2 = decideMessage.indexOf("V2"); //V2 | |
if (msInC2 != -1){ | |
String temp_read2 = new String(decideMessage.substring(msInC2 + 3,msInC2 + 5)); | |
String outST_21 = temp_read2.substring(0, 1); | |
String outST_22 = temp_read2.substring(1, 2); | |
double v2_o1 = Double.parseDouble(hexStringToBase10(outST_21)); | |
double v2_o2 = Double.parseDouble(hexStringToBase10(outST_22)); | |
v2_o1 = v2_o1 * 16; | |
double v2 = (v2_o1 + v2_o2) / 255 * 6; | |
// double vbar_d2 = v2 * 10 ; | |
// int var_i2 = Integer.parseInt(Double.toString(vbar_d2)); | |
String V2_out = Double.toString(v2); | |
TextView mOutText02 = (TextView) findViewById(R.id.getdata02); | |
mOutText02.setText("電池電壓: " + V2_out); | |
// vbar2.setProgress(var_i2); | |
} | |
msInC3 = decideMessage.lastIndexOf("V3"); //V3 | |
if (msInC3 != -1){ | |
String temp_read3 = new String(decideMessage.substring(msInC3 + 3,msInC3 + 5)); | |
String outST_31 = temp_read3.substring(0, 1); | |
String outST_32 = temp_read3.substring(1, 2); | |
double v3_o1 = Double.parseDouble(hexStringToBase10(outST_31)); | |
double v3_o2 = Double.parseDouble(hexStringToBase10(outST_32)); | |
v3_o1 = v3_o1 * 16; | |
double v3 = (v3_o1 + v3_o2)/ 255 * 12; | |
// double vbar_d3 = v3 * 10 ; | |
// int var_i3 = Integer.parseInt(Double.toString(vbar_d3)); | |
String V3_out = Double.toString(v3); | |
TextView mOutText03 = (TextView) findViewById(R.id.getdata03); | |
mOutText03.setText("太陽能超頻電壓: " + V3_out); | |
// vbar3.setProgress(var_i3); | |
} | |
msInCER = decideMessage.lastIndexOf("ER"); //ERROR CODE | |
if (msInCER != -1){ | |
String er_data = new String(decideMessage.substring(msInCER + 3,msInCER + 5)); | |
String er_out1 = ""; | |
String er_out2 = ""; | |
int ect = 1; | |
int t1,t2; | |
int outST_er1 = Integer.parseInt(hexStringToBase10(er_data.substring(0, 1))); | |
int outST_er2 = Integer.parseInt(hexStringToBase10(er_data.substring(1, 2))); | |
while (ect <= 4){ | |
t1 = outST_er1 % 2; | |
outST_er1 = outST_er1 / 2; | |
if(t1 == 1){ | |
er_out1 = "1" + er_out1; | |
}else{ | |
er_out1 = "0" + er_out1; | |
} | |
t2 = outST_er2 % 2; | |
outST_er2 = outST_er2 / 2; | |
if(t2 == 1){ | |
er_out2 = "1" + er_out2; | |
}else{ | |
er_out2 = "0" + er_out2; | |
} | |
ect = ect +1; | |
} | |
String er_out = er_out1 + er_out2; | |
TextView mERText = (TextView) findViewById(R.id.text_b); | |
mERText.setText("錯誤碼: " + er_out); | |
} | |
event_int = 0; | |
decideMessage = ""; | |
} | |
break; | |
} | |
} | |
}; | |
private InputStream mmSocket; | |
public static String hexStringToBase10(String value){ //16進制字串轉10進制 | |
int i = 0; | |
String result = null; | |
while(true){ | |
if( (value.toLowerCase()).equals(Integer.toHexString(i))){ | |
result = String.valueOf(i); | |
break; | |
} | |
i++; | |
if (i > 16){ | |
break; | |
} | |
} | |
return result; | |
} | |
private class ConnectedThread extends Thread { | |
private final BluetoothSocket mmSocket; | |
public ConnectedThread(BluetoothSocket csocket) { | |
mmSocket = csocket; | |
InputStream tmpIn = null; | |
OutputStream tmpOut = null; | |
// 取得Bluetooth的input跟output | |
try { | |
tmpIn = csocket.getInputStream(); | |
tmpOut = csocket.getOutputStream(); | |
} catch (IOException e) { | |
Log.e(TAG, "temp sockets not created", e); | |
} | |
inStream = tmpIn; | |
outStream = tmpOut; | |
} | |
public void cancel() { | |
// TODO 自動產生的方法 Stub | |
} | |
@SuppressWarnings("deprecation") | |
public void run() { | |
byte[] rbuffer = new byte[1024]; | |
int bytes; | |
// 監聽是否有資料傳輸進來 | |
while (true) { | |
try{ | |
bytes = inStream.read(rbuffer); | |
mHandler.obtainMessage(2, bytes, -1, rbuffer).sendToTarget(); | |
} catch (Exception e) { | |
Log.e(TAG, "disconnected", e); | |
boolean blBuild = false; | |
try{ | |
FileReader fr = new FileReader("/sdcard/greenoutput.txt"); | |
if(fr.read() == 0){ | |
blBuild = true; | |
}else{ | |
blBuild = false; | |
} | |
}catch(Exception e1){ | |
e1.printStackTrace(); | |
} | |
/* 建立新的文字檔 */ | |
if(blBuild){ | |
try{ | |
FileWriter fw = new FileWriter("/sdcard/greenoutput.txt", false); | |
Date data = new Date(); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
Date date = null; | |
String dateString = sdf.format(date); | |
System.out.println(dateString); | |
//建立FileOutputStream物件,路徑為SD卡中的output.txt | |
FileOutputStream output = new FileOutputStream("/sdcard/greenoutput.txt"); | |
output.write(dateString.getBytes()); | |
String data01 = " "; | |
String data02 = "\n"; | |
output.write(data01.getBytes()); | |
output.write(data02.getBytes()); | |
output.close(); | |
}catch(Exception e1){ | |
e1.printStackTrace(); | |
} | |
} | |
} | |
continue; | |
} | |
} | |
} | |
public void cancel() { | |
try { | |
mmSocket.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "close() of connect socket failed", e); | |
} | |
} | |
public Object getBytes() { | |
// TODO 自動產生的方法 Stub | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment