Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. package com.example.nicho.myapplication;
  2.  
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.os.Bundle;
  5. import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
  6. import org.osmdroid.views.MapView;
  7. import org.osmdroid.config.Configuration;
  8. import android.app.Activity;
  9. import android.content.Context;
  10. import android.preference.PreferenceManager;
  11. import android.bluetooth.BluetoothSocket;
  12. import android.bluetooth.BluetoothDevice;
  13. import java.util.UUID;
  14. import android.widget.Toast;
  15. import java.io.InputStream;
  16. import java.io.OutputStream;
  17. import java.io.IOException;
  18. import java.util.Set;
  19. import android.content.Intent;
  20. import org.osmdroid.api.IMapController;
  21. import org.osmdroid.util.GeoPoint;
  22. import org.osmdroid.views.overlay.Marker;
  23. import org.osmdroid.views.overlay.OverlayItem;
  24. import org.osmdroid.views.overlay.ItemizedIconOverlay;
  25.  
  26. public class MainActivity extends Activity {
  27.  
  28. private BluetoothAdapter mBluetoothAdapter = null;
  29. private BluetoothSocket btSocket = null;
  30. private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  31. private InputStream inStream = null;
  32. private OutputStream outStream = null;
  33. BluetoothDevice iterator;
  34. Marker.OnMarkerClickListener l;
  35.  
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. Context ctx = getApplicationContext();
  40. //important! set your user agent to prevent getting banned from the osm servers
  41. Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
  42. setContentView(R.layout.activity_main);
  43.  
  44. MapView map = (MapView) findViewById(R.id.map);
  45. map.setTileSource(TileSourceFactory.MAPNIK);
  46.  
  47. map.setBuiltInZoomControls(true);
  48. map.setMultiTouchControls(true);
  49.  
  50. IMapController mapController = map.getController();
  51. mapController.setZoom(12);
  52.  
  53. GeoPoint startPoint = new GeoPoint(46.573353, -87.41443);
  54. GeoPoint loc_TC02 = new GeoPoint(46.57355, -87.41553);
  55.  
  56. mapController.setCenter(startPoint);
  57.  
  58. // build a new marker pin
  59. Marker TC01 = new Marker(map);
  60. TC01.setPosition(startPoint);
  61. TC01.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
  62. TC01.setTitle("TC01");
  63.  
  64. Marker TC02 = new Marker(map);
  65. TC02.setPosition(loc_TC02);
  66. TC01.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
  67. TC02.setTitle("TC02");
  68.  
  69.  
  70. map.getOverlays().add(TC01);
  71. map.getOverlays().add(TC02);
  72.  
  73. map.invalidate();
  74.  
  75. BlueTooth();
  76. }
  77.  
  78. public void onResume() {
  79. super.onResume();
  80. //this will refresh the osmdroid configuration on resuming.a
  81. //if you make changes to the configuration, use
  82. //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  83. //Configuration.getInstance().save(this, prefs);
  84. Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
  85. }
  86.  
  87. private void BlueTooth() {
  88. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  89. if (!mBluetoothAdapter.isEnabled()) {
  90. Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  91. startActivityForResult(enableAdapter, 0);
  92. }
  93. if (mBluetoothAdapter == null) {
  94. Toast.makeText(getApplicationContext(), "Bluetooth null !", Toast.LENGTH_SHORT).show();
  95. }
  96. if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
  97.  
  98. Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
  99.  
  100. if (bondedDevices.isEmpty()) {
  101. Toast.makeText(getApplicationContext(), "Please Pair the Device first", Toast.LENGTH_SHORT).show();
  102. } else {
  103. for (BluetoothDevice iterator : bondedDevices) {
  104. if (iterator.getName().equals("HC-05")) {
  105. BluetoothDevice device = iterator;
  106. try {
  107. btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
  108. btSocket.connect();
  109. } catch (IOException ex) {
  110. Toast.makeText(getApplicationContext(), "Could not connect to Arduino.", Toast.LENGTH_SHORT).show();
  111. }
  112. if (btSocket.isConnected()) {
  113. Toast.makeText(getApplicationContext(), "Connected to HC05", Toast.LENGTH_SHORT).show();
  114. //Write 'c' to arduino to tell it to send data.
  115. try {
  116. outStream = btSocket.getOutputStream();
  117. outStream.write('r');
  118. }
  119.  
  120. catch (IOException ex) {
  121. Toast.makeText(getApplicationContext(), "Error writing to Arduino.", Toast.LENGTH_SHORT).show();
  122. }
  123.  
  124. //Read data from Arduino
  125. try {
  126. int byteCount = inStream.available();
  127. if (byteCount > 0) {
  128. byte[] rawData = new byte[byteCount];
  129. try {
  130. inStream.read(rawData);
  131. final String data = new String(rawData, "UTF-8");
  132. //Display collected data on screen.
  133. Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
  134. } catch (IOException ex) {
  135. Toast.makeText(getApplicationContext(), "Error reading from Arduino.", Toast.LENGTH_SHORT).show();
  136. }
  137. }
  138. } catch (IOException e) {
  139. Toast.makeText(getApplicationContext(), "HC05 not found/Connection Failed.", Toast.LENGTH_SHORT).show();
  140. }
  141. }
  142.  
  143. else {
  144. Toast.makeText(getApplicationContext(), "Could not read from Arduino.", Toast.LENGTH_SHORT).show();
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement