Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nicho.myapplication;
- import android.bluetooth.BluetoothAdapter;
- import android.os.Bundle;
- import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
- import org.osmdroid.views.MapView;
- import org.osmdroid.config.Configuration;
- import android.app.Activity;
- import android.content.Context;
- import android.preference.PreferenceManager;
- import android.bluetooth.BluetoothSocket;
- import android.bluetooth.BluetoothDevice;
- import java.util.UUID;
- import android.widget.Toast;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.IOException;
- import java.util.Set;
- import android.content.Intent;
- import org.osmdroid.api.IMapController;
- import org.osmdroid.util.GeoPoint;
- import org.osmdroid.views.overlay.Marker;
- import org.osmdroid.views.overlay.OverlayItem;
- import org.osmdroid.views.overlay.ItemizedIconOverlay;
- public class MainActivity extends Activity {
- private BluetoothAdapter mBluetoothAdapter = null;
- private BluetoothSocket btSocket = null;
- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private InputStream inStream = null;
- private OutputStream outStream = null;
- BluetoothDevice iterator;
- Marker.OnMarkerClickListener l;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Context ctx = getApplicationContext();
- //important! set your user agent to prevent getting banned from the osm servers
- Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
- setContentView(R.layout.activity_main);
- MapView map = (MapView) findViewById(R.id.map);
- map.setTileSource(TileSourceFactory.MAPNIK);
- map.setBuiltInZoomControls(true);
- map.setMultiTouchControls(true);
- IMapController mapController = map.getController();
- mapController.setZoom(12);
- GeoPoint startPoint = new GeoPoint(46.573353, -87.41443);
- GeoPoint loc_TC02 = new GeoPoint(46.57355, -87.41553);
- mapController.setCenter(startPoint);
- // build a new marker pin
- Marker TC01 = new Marker(map);
- TC01.setPosition(startPoint);
- TC01.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
- TC01.setTitle("TC01");
- Marker TC02 = new Marker(map);
- TC02.setPosition(loc_TC02);
- TC01.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
- TC02.setTitle("TC02");
- map.getOverlays().add(TC01);
- map.getOverlays().add(TC02);
- map.invalidate();
- BlueTooth();
- }
- public void onResume() {
- super.onResume();
- //this will refresh the osmdroid configuration on resuming.a
- //if you make changes to the configuration, use
- //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- //Configuration.getInstance().save(this, prefs);
- Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
- }
- private void BlueTooth() {
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableAdapter, 0);
- }
- if (mBluetoothAdapter == null) {
- Toast.makeText(getApplicationContext(), "Bluetooth null !", Toast.LENGTH_SHORT).show();
- }
- if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
- Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
- if (bondedDevices.isEmpty()) {
- Toast.makeText(getApplicationContext(), "Please Pair the Device first", Toast.LENGTH_SHORT).show();
- } else {
- for (BluetoothDevice iterator : bondedDevices) {
- if (iterator.getName().equals("HC-05")) {
- BluetoothDevice device = iterator;
- try {
- btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- btSocket.connect();
- } catch (IOException ex) {
- Toast.makeText(getApplicationContext(), "Could not connect to Arduino.", Toast.LENGTH_SHORT).show();
- }
- if (btSocket.isConnected()) {
- Toast.makeText(getApplicationContext(), "Connected to HC05", Toast.LENGTH_SHORT).show();
- //Write 'c' to arduino to tell it to send data.
- try {
- outStream = btSocket.getOutputStream();
- outStream.write('r');
- }
- catch (IOException ex) {
- Toast.makeText(getApplicationContext(), "Error writing to Arduino.", Toast.LENGTH_SHORT).show();
- }
- //Read data from Arduino
- try {
- int byteCount = inStream.available();
- if (byteCount > 0) {
- byte[] rawData = new byte[byteCount];
- try {
- inStream.read(rawData);
- final String data = new String(rawData, "UTF-8");
- //Display collected data on screen.
- Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
- } catch (IOException ex) {
- Toast.makeText(getApplicationContext(), "Error reading from Arduino.", Toast.LENGTH_SHORT).show();
- }
- }
- } catch (IOException e) {
- Toast.makeText(getApplicationContext(), "HC05 not found/Connection Failed.", Toast.LENGTH_SHORT).show();
- }
- }
- else {
- Toast.makeText(getApplicationContext(), "Could not read from Arduino.", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement