Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.58 KB | None | 0 0
  1. package com.android.backend.data.bluetooth;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import java.io.OutputStream;
  8.  
  9. import java.lang.reflect.Method;
  10. import java.util.ArrayList;
  11. import android.bluetooth.BluetoothDevice;
  12. import android.bluetooth.BluetoothSocket;
  13.  
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.os.Message;
  17.  
  18. import android.util.Log;
  19.  
  20.  
  21. import com.android.AutoActivity;
  22. import com.android.CalibrationActivity;
  23.  
  24. import com.android.ui.UIManager;
  25.  
  26. public class Connection extends Thread {
  27.  
  28. final Connection tt = this;
  29. private final String TAG = "Connection"; // Class name for logging
  30. public boolean connected = false;
  31. public BluetoothSocket bSocket = null;
  32. public InputStream mmInStream = null;
  33. public OutputStream mmOutStream = null;
  34. public ArrayList<String> message = new ArrayList<String>();
  35. public BluetoothDevice currentDevice = null;
  36. public UIManager mgr;
  37. public boolean connectFailure = false;
  38. public boolean stopAutoConnect = false;
  39. public String type = null;
  40. public AutoActivity ff;
  41.  
  42. public void bsocketcancel() {
  43. try {
  44. bSocket.close();
  45. } catch (IOException e) { }
  46. }
  47.  
  48. public Connection(UIManager mgr, BluetoothDevice device, String message,
  49. String type) {
  50. this.mgr = mgr;
  51. currentDevice = device;
  52. this.message.add(message);
  53. this.type = type;
  54. }
  55.  
  56. public Connection(UIManager mgr, BluetoothDevice device, ArrayList<String> message,
  57. String type) {
  58. this.mgr = mgr;
  59. currentDevice = device;
  60. this.message = message;
  61. this.type = type;
  62. }
  63.  
  64. @Override
  65. public void run() {
  66. if (type.compareToIgnoreCase("Manual") == 0) {
  67.  
  68. Log.d("thread", "manual");
  69. if (!connected)
  70. connect(currentDevice);
  71.  
  72. if (bSocket != null){
  73. for(String msg : message)
  74. write(msg.getBytes());
  75. }
  76.  
  77. mgr.hideConnectingProgressBox();
  78.  
  79. if(connected){
  80. mgr.connectedDialog();
  81. System.out.println("Maunal connected");
  82.  
  83. }
  84.  
  85. Log.d("thread", "hideprogressBox");
  86. if (!connected)
  87. mgr.showConnectionErrBox();
  88.  
  89. }
  90.  
  91. if (type.compareToIgnoreCase("auto") == 0){
  92. Log.d("thread", "auto");
  93. stopConnection();
  94. int i = 0;
  95. while (true) { //Set to 5 second
  96.  
  97. if (!connected)
  98. connect(currentDevice);
  99.  
  100. if (bSocket != null){
  101. for(String msg : message)
  102. write(msg.getBytes());
  103. }
  104.  
  105. mgr.hideConnectingProgressBox();
  106.  
  107. Log.d("thread", "hideprogressBox");
  108.  
  109.  
  110.  
  111. if (!connected) {
  112. mgr.connecting();
  113. }
  114.  
  115. if (connected) {
  116. mgr.connectedDialog();
  117. bsocketcancel();
  118. System.out.println("Auto connected");
  119. //BluetoothManager.getInstance().disconnectAll(); // cut all connection after transmit
  120. break;
  121. }
  122.  
  123.  
  124. if(stopAutoConnect){
  125. mgr.cancelconnecting();
  126. break;
  127. }
  128. }
  129.  
  130. }
  131.  
  132. if (type.compareToIgnoreCase("calibrate") == 0) {
  133.  
  134. Log.d("thread", "calibrate");
  135. if (!connected)
  136. connect(currentDevice);
  137.  
  138. if (bSocket != null){
  139. for(String msg : message)
  140. write(msg.getBytes());
  141. }
  142.  
  143. mgr.hideConnectingProgressBox();
  144.  
  145. if(connected){
  146. mgr.connectedDialog();
  147. mgr.msgsent();
  148. System.out.println("Calibration connected");
  149.  
  150. int bufferSize = 1024;
  151. byte[] buffer = new byte[bufferSize];
  152. int bytes;
  153. String apple = "";
  154.  
  155. // Keep listening to the InputStream while connected
  156. while (true) {
  157. try {
  158. // Read from the InputStream
  159. bytes = mmInStream.read(buffer);
  160.  
  161. if ( bytes != -1){
  162. while ((bytes==bufferSize)&&(buffer[bufferSize-1] != 0)){
  163. apple = apple + "_" + new String(buffer, 0, bytes);
  164. bytes = mmInStream.read(buffer);
  165.  
  166. }
  167. apple = apple + new String(buffer, 0, bytes -1);
  168. System.out.println("message " + apple);
  169.  
  170.  
  171. //pass data to UI CalibrationActivity
  172. System.out.println("message out " + apple);
  173. Message msga = mhandler.obtainMessage();
  174. Bundle bundle = new Bundle();
  175. bundle.putString("key", apple);
  176. msga.setData(bundle);
  177. mhandler.sendMessage(msga);
  178. }
  179.  
  180. } catch (IOException e) {
  181. Log.e(TAG, "disconnected", e);
  182.  
  183. break;
  184. }
  185. }
  186.  
  187. }
  188.  
  189. Log.d("thread", "hideprogressBox");
  190. if (!connected){
  191. mgr.showConnectionErrBox();
  192. }
  193. }
  194.  
  195. }
  196.  
  197. public void connect(BluetoothDevice device) {
  198. currentDevice = device;
  199. BluetoothSocket tmp = null;
  200. BluetoothManager.getInstance().getBluetoothAdapter().cancelDiscovery();
  201. // Get a BluetoothSocket for a connection with the
  202. // given BluetoothDevice
  203. try {
  204. // tmp = device
  205. // .createRfcommSocketToServiceRecord(BluetoothManager.SerialPortServiceClass_UUID);
  206. Method m = device.getClass().getMethod("createRfcommSocket",
  207. new Class[] { int.class });
  208. tmp = (BluetoothSocket) m.invoke(device, 1);
  209.  
  210. // } catch (IOException e) {
  211. // Log.e(TAG, "create() failed", e);
  212. // return;
  213. } catch (Exception e) {
  214. Log.e(TAG, "Socket connection failed", e);
  215. return;
  216. }
  217.  
  218. bSocket = tmp;
  219.  
  220.  
  221. try {
  222. bSocket.connect();
  223. } catch (IOException e1) {
  224. Log.e(TAG, "Connection err", e1);
  225. connectFailure = true;
  226. return;
  227. }
  228.  
  229. if (tmp != null) {
  230. // Get Input/output stream for Socket
  231. try {
  232. mmInStream = bSocket.getInputStream();
  233. mmOutStream = bSocket.getOutputStream();
  234.  
  235. } catch (IOException e) {
  236. Log.e(TAG, "temp sockets not created", e);
  237. return;
  238. }
  239. }
  240.  
  241.  
  242. connected = true;
  243. connectFailure = false;
  244. }
  245.  
  246.  
  247.  
  248. private Handler mhandler;
  249.  
  250.  
  251. public void read(){
  252. Log.i(TAG, "Read is running");
  253.  
  254. int bufferSize = 1024;
  255. byte[] buffer = new byte[bufferSize];
  256. int bytes;
  257. String a = "";
  258.  
  259. // Keep listening to the InputStream while connected
  260. while (true) {
  261. try {
  262. // Read from the InputStream
  263. bytes = mmInStream.read(buffer);
  264.  
  265. if ( bytes != -1){
  266. while ((bytes==bufferSize)&&(buffer[bufferSize-1] != 0)){
  267. a = a + new String(buffer, 0, bytes);
  268. bytes = mmInStream.read(buffer);
  269.  
  270. }
  271. a = a + new String(buffer, 0, bytes -1);
  272. System.out.println("message " + a);
  273.  
  274. }
  275.  
  276. } catch (IOException e) {
  277. Log.e(TAG, "disconnected", e);
  278.  
  279. break;
  280. }
  281.  
  282. }
  283.  
  284. }
  285.  
  286.  
  287.  
  288.  
  289. public void write(byte[] buffer) {
  290. if (mmOutStream != null) {
  291. try {
  292. mmOutStream.write(buffer);
  293. Log.d(TAG, "Message Sent");
  294. } catch (IOException e) {
  295. Log.e(TAG, "Exception during write", e);
  296. }
  297. } else {
  298. //mgr.showConnectionErrBox();
  299. }
  300. }
  301.  
  302. public void stopConnection() {
  303. try {
  304. if(bSocket != null)
  305. bSocket.close();
  306. Log.e(TAG, "Connection closed");
  307. } catch (IOException e) {
  308. Log.e(TAG, "close() of connect socket failed", e);
  309. }
  310. }
  311.  
  312. public BluetoothDevice getCurrentDevice() {
  313. return currentDevice;
  314. }
  315.  
  316. public void setCurrentDevice(BluetoothDevice currentDevice) {
  317. this.currentDevice = currentDevice;
  318. }
  319.  
  320. public ArrayList<String> getMessage() {
  321. return message;
  322. }
  323.  
  324. public void setMessage(ArrayList<String> message) {
  325. this.message = message;
  326. }
  327.  
  328.  
  329. }
  330.  
  331. if(connected){
  332. mgr.connectedDialog();
  333. mgr.msgsent();
  334. System.out.println("Calibration connected");
  335.  
  336. int bufferSize = 1024;
  337. byte[] buffer = new byte[bufferSize];
  338. int bytes;
  339. String apple = "";
  340.  
  341. // Keep listening to the InputStream while connected
  342. while (true) {
  343. try {
  344. // Read from the InputStream
  345. bytes = mmInStream.read(buffer);
  346.  
  347. if ( bytes != -1){
  348. while ((bytes==bufferSize)&&(buffer[bufferSize-1] != 0)){
  349. apple = apple + "_" + new String(buffer, 0, bytes);
  350. bytes = mmInStream.read(buffer);
  351.  
  352. }
  353. apple = apple + new String(buffer, 0, bytes -1);
  354. System.out.println("message " + apple);
  355.  
  356.  
  357. //pass data to UI CalibrationActivity
  358. System.out.println("message out " + apple);
  359. Message msga = mhandler.obtainMessage();
  360. Bundle bundle = new Bundle();
  361. bundle.putString("key", apple);
  362. msga.setData(bundle);
  363. mhandler.sendMessage(msga);
  364. }
  365.  
  366. } catch (IOException e) {
  367. Log.e(TAG, "disconnected", e);
  368.  
  369. break;
  370. }
  371. }
  372.  
  373. }
  374.  
  375. package com.android;
  376.  
  377. import java.io.BufferedReader;
  378. import java.io.FileInputStream;
  379. import java.io.InputStreamReader;
  380. import java.util.ArrayList;
  381.  
  382. import com.android.SimpleGestureFilter.SimpleGestureListener;
  383. import com.android.backend.data.Playlist;
  384. import com.android.backend.data.PlaylistManager;
  385. import com.android.backend.data.SwitchBar;
  386. import com.android.backend.data.bluetooth.BluetoothManager;
  387. import com.android.backend.data.bluetooth.Connection;
  388. import com.android.ui.UIManager;
  389. import com.android.ui.playlist.PlaylistEntryList;
  390. import android.annotation.SuppressLint;
  391. import android.app.Activity;
  392. import android.app.AlertDialog;
  393. import android.app.Dialog;
  394. import android.bluetooth.BluetoothDevice;
  395. import android.content.BroadcastReceiver;
  396. import android.content.Context;
  397. import android.content.DialogInterface;
  398. import android.content.Intent;
  399. import android.content.IntentFilter;
  400. import android.os.Bundle;
  401. import android.os.Handler;
  402. import android.os.Message;
  403. import android.text.Editable;
  404. import android.util.Log;
  405. import android.view.MotionEvent;
  406. import android.view.View;
  407. import android.view.View.OnClickListener;
  408. import android.view.View.OnTouchListener;
  409. import android.view.Window;
  410. import android.view.WindowManager.LayoutParams;
  411. import android.widget.AdapterView;
  412. import android.widget.Button;
  413. import android.widget.CompoundButton;
  414. import android.widget.CompoundButton.OnCheckedChangeListener;
  415. import android.widget.EditText;
  416. import android.widget.ImageView;
  417. import android.widget.ListView;
  418. import android.widget.TextView;
  419. import android.widget.Toast;
  420. import android.widget.ToggleButton;
  421. import android.widget.AdapterView.OnItemClickListener;
  422. import android.widget.AdapterView.OnItemLongClickListener;
  423.  
  424. //public class CalibrationActivity extends Activity{
  425. public class CalibrationActivity extends Activity implements SimpleGestureListener{
  426.  
  427.  
  428. String gotBread;
  429.  
  430.  
  431. public void handleMessage(Message msg)
  432. { switch(msg.what){
  433.  
  434. case 2:
  435. Bundle got = getIntent().getExtras();
  436. gotBread = got.getString("key");
  437.  
  438. TextView aa = (TextView) this.findViewById(R.id.stringinput);
  439. aa.setText(gotBread);
  440.  
  441.  
  442.  
  443.  
  444. break;
  445. }
  446. }
  447.  
  448.  
  449.  
  450.  
  451.  
  452. private SimpleGestureFilter detector;
  453.  
  454. @Override
  455. public boolean dispatchTouchEvent(MotionEvent me){
  456. this.detector.onTouchEvent(me);
  457. return super.dispatchTouchEvent(me);
  458. }
  459.  
  460. @Override
  461. public void onSwipe(int direction) {
  462. // TODO Auto-generated method stub
  463. //final MainActivity tt = this;
  464.  
  465. String str = "";
  466.  
  467. switch (direction) {
  468.  
  469. //case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";
  470. case SimpleGestureFilter.SWIPE_RIGHT : this.finish();
  471. break;
  472.  
  473. case SimpleGestureFilter.SWIPE_LEFT : str = "Swipe Left";
  474. break;
  475.  
  476. case SimpleGestureFilter.SWIPE_DOWN : str = "Swipe Down";
  477. break;
  478.  
  479.  
  480. case SimpleGestureFilter.SWIPE_UP : str = "Swipe Up";
  481. break;
  482.  
  483. }
  484. //Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
  485.  
  486. }
  487.  
  488. @Override
  489. public void onDoubleTap() {
  490. // TODO Auto-generated method stub
  491. //Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
  492. //Do Nothing for now future development
  493.  
  494. }
  495.  
  496.  
  497.  
  498. private PlaylistManager playlistMgr;
  499.  
  500. private PlaylistEntryList playlistView; // Data for listview, stores current
  501. // playlist in memory
  502.  
  503. public String createdNameList; // Indicated the name of currently Name of
  504. // playlist that is being created
  505.  
  506. public Dialog dialog_customizePL, dialog_createPL, dialog_customizePLms; // Dialog boxes for
  507. // Playlist creation
  508.  
  509. public UIManager uiMgr = new UIManager();
  510.  
  511. public String CONNECTED_DEVICE_MAC;
  512.  
  513. public String dialogboxtext;
  514.  
  515. public Connection Conn;
  516.  
  517. int i = 0;
  518.  
  519. protected void onPause() {
  520. super.onPause();
  521. //BluetoothManager.getInstance().disconnectAll();
  522. unregisterReceiver(mRvcau); //Sequence is important
  523. System.exit(0); // Kill off all thread to prevent connectiong lost popup
  524. // I use system because is a calibration so I want user not to leave the activity else cannot calibrate
  525.  
  526. }
  527.  
  528.  
  529. protected void onResume() { //restart bluetooth is accidently off it
  530. super.onResume();
  531.  
  532. BluetoothManager.getInstance().init();
  533. BluetoothManager.getInstance().getBluetoothAdapter().enable();
  534.  
  535. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); // IntentFilter class is important to declare for below filter
  536. registerReceiver(mRvcau,filter); // mRvcau is the location of logic, filter is message identify by android
  537.  
  538. filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); // Future deveploment
  539. registerReceiver(mRvcau,filter);
  540.  
  541. filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED); // Future deveploment
  542. registerReceiver(mRvcau,filter);
  543.  
  544. filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); // Future deveploment
  545. registerReceiver(mRvcau,filter);
  546. }
  547.  
  548. /** Called when the activity is first created. */
  549. @Override
  550. public void onCreate(Bundle savedInstanceState) {
  551. super.onCreate(savedInstanceState);
  552. setContentView(R.layout.remotedevicecalibration);
  553.  
  554.  
  555.  
  556. detector = new SimpleGestureFilter(this,this); ////////////////// For Gesture
  557.  
  558. playlistMgr = PlaylistManager.getInstance();
  559.  
  560. initUI();
  561. uiMgr.initPlayListUI(this);
  562.  
  563. }
  564.  
  565.  
  566. int connect = 0; // Check connection
  567. int disconnect = 0; // Check disconnection
  568. int requestdisconnect = 0;
  569. int connectstatuschanges = 0;
  570.  
  571. public BroadcastReceiver mRvcau = new BroadcastReceiver(){ // The new is instantiate the class **always read from the back
  572. // The line from the back is creating an instantiate of this class
  573. @Override
  574. public void onReceive(Context context, Intent intent) {
  575. String action = intent.getAction();
  576.  
  577. if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
  578. //AutoActivity.this.finish();
  579. connect = 1; // prevent activity from exiting
  580. System.out.println("ACTION_ACL_CONNECTED " + connect);
  581.  
  582. }
  583. else
  584. {
  585. connect = 0;
  586. System.out.println("ACTION_ACL_CONNECTED " + connect);
  587. }
  588.  
  589.  
  590. if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { //future development
  591. disconnect = 1;
  592. System.out.println("ACTION_ACL_DISCONNECTED " + disconnect);
  593. }
  594. else
  595. {
  596. disconnect = 0;
  597. System.out.println("ACTION_ACL_DISCONNECTED " + disconnect);
  598. }
  599.  
  600. if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) { //future development
  601. requestdisconnect = 1;
  602. System.out.println("ACTION_ACL_DISCONNECT_REQUESTED " + requestdisconnect);
  603. }
  604. else
  605. {
  606. requestdisconnect = 0;
  607. System.out.println("ACTION_ACL_DISCONNECT_REQUESTED " + requestdisconnect);
  608. }
  609.  
  610. if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { //future development
  611. connectstatuschanges = 1;
  612. System.out.println("ACTION_BOND_STATE_CHANGED " + connectstatuschanges);
  613. }
  614. else
  615. {
  616. connectstatuschanges = 0;
  617. System.out.println("ACTION_BOND_STATE_CHANGED " + connectstatuschanges);
  618. }
  619.  
  620. }
  621. };
  622.  
  623. /**
  624. * Init layout component. Define Listener for Buttons and Dataset for
  625. * ListViews
  626. */
  627. @SuppressLint("ParserError")
  628. public void initUI() {
  629.  
  630.  
  631. Button Orange = (Button) findViewById(R.id.CalibrationOrange); // new method on test using image
  632. Orange.setOnClickListener(new OnClickListener() {
  633.  
  634. @Override
  635. public void onClick(View v){
  636. // TODO Auto-generated method stub
  637. activiteSingleConnection(20);
  638.  
  639. }
  640. });
  641.  
  642. Button Blue = (Button) findViewById(R.id.CalibrationBlue); // new method on test using image icon to on the switch
  643. Blue.setOnClickListener(new OnClickListener() {
  644.  
  645. @Override
  646. public void onClick(View v){
  647. // TODO Auto-generated method stub
  648. activiteSingleConnection(21);
  649.  
  650. }
  651. });
  652.  
  653. Button Green = (Button) findViewById(R.id.CalibrationGreen); // new method on test using image icon to on the switch
  654. Green.setOnClickListener(new OnClickListener() {
  655.  
  656. @Override
  657. public void onClick(View v){
  658. // TODO Auto-generated method stub
  659. activiteSingleConnection(22);
  660.  
  661. }
  662. });
  663.  
  664.  
  665. loadDataToMem();
  666. }
  667.  
  668.  
  669. public void activiteSingleConnection(int i) {
  670.  
  671. BluetoothManager btMgr = BluetoothManager.getInstance();
  672. BluetoothDevice device = btMgr.getBluetoothAdapter().getRemoteDevice(
  673. CONNECTED_DEVICE_MAC);
  674. ArrayList<String> msg = new ArrayList<String>();
  675. if(i == 20){
  676. msg.add("N");
  677. }
  678.  
  679. if(i == 21){
  680. msg.add("J");
  681. }
  682.  
  683. if(i == 22){
  684. msg.add("I");
  685. }
  686.  
  687. CharSequence fruit = null;
  688. TextView aa = (TextView) this.findViewById(R.id.stringinput);
  689. aa.setText(fruit);
  690.  
  691. btMgr.calibratesentMessage(uiMgr, device, msg);
  692. }
  693.  
  694. public void loadDataToMem() {
  695. try {
  696. String str = "";
  697. //StringBuffer buf = new StringBuffer();
  698. FileInputStream input = this.openFileInput("bone.txt");
  699. BufferedReader reader = new BufferedReader(new InputStreamReader(
  700. input));
  701. if (input != null) {
  702. while ((str = reader.readLine()) != null) {
  703. String[] buff = str.split(",");
  704. TextView topView = (TextView) this
  705. .findViewById(R.id.remotedevicecalibrationtextview1);
  706. TextView bottomView = (TextView) this
  707. .findViewById(R.id.remotedevicecalibrationtextView2);
  708. topView.setText("Name : " + buff[0]);
  709. bottomView.setText("Mac : " + buff[2]);
  710. this.CONNECTED_DEVICE_MAC = buff[2];
  711. Log.d("loadDataToMem", str);
  712. }
  713. }
  714. input.close();
  715. } catch (Exception e) {
  716. e.printStackTrace();
  717. }
  718. }
  719.  
  720.  
  721. }
  722.  
  723. 09-13 10:50:33.640: W/dalvikvm(2086): threadid=9: thread exiting with uncaught exception (group=0x40015560)
  724. 09-13 10:50:33.667: E/AndroidRuntime(2086): FATAL EXCEPTION: Thread-10
  725. 09-13 10:50:33.667: E/AndroidRuntime(2086): java.lang.NullPointerException
  726. 09-13 10:50:33.667: E/AndroidRuntime(2086): at com.android.backend.data.bluetooth.Connection.run(Connection.java:196)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement