Skip to content

Instantly share code, notes, and snippets.

@matiri132
Last active June 26, 2017 21:43
Show Gist options
  • Save matiri132/4180ce0c351c91f6b177e94bbcc5d111 to your computer and use it in GitHub Desktop.
Save matiri132/4180ce0c351c91f6b177e94bbcc5d111 to your computer and use it in GitHub Desktop.
TrajeLedBT-src
package com.trajeledbt;
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by root on 12/06/17.
*/
public class ConnectThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
}
/* Call this from the main activity to send data to the remote device */
public void send(String send) {
byte[] buffer = send.getBytes();
try {
mmOutStream.write(buffer);
} catch (IOException e) { }
}
}
package com.trajeledbt;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;
public class DevicesList extends ListActivity {
static String MAC_ADRESS = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> ArrayBluetooth = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
if (BluetoothAdapter.getDefaultAdapter().getBondedDevices().size() > 0) {
for (BluetoothDevice device : BluetoothAdapter.getDefaultAdapter().getBondedDevices()) {
ArrayBluetooth.add(device.getName());
}
}
setListAdapter(ArrayBluetooth);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String generalinfo = ((TextView) v).getText().toString();
for (BluetoothDevice device : BluetoothAdapter.getDefaultAdapter().getBondedDevices()) {
if (device.getName().equals(generalinfo)) {
Intent macreturn = new Intent();
macreturn.putExtra(MAC_ADRESS, device.getAddress());
setResult(RESULT_OK, macreturn);
finish();
}
}
}
}
package com.trajeledbt;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
public class MainMenu extends AppCompatActivity {
//Elementos del menu
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_CONNECT_BT = 2;
public boolean connection = false;
private static String MAC = null;
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
BluetoothAdapter btadapter = null;
BluetoothDevice btdevice = null;
BluetoothSocket btsocket = null;
public ConnectThread connectedThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
/////////////////BLUETOOTH///////////////////////
btadapter = BluetoothAdapter.getDefaultAdapter();
if (btadapter == null) {
Toast.makeText(getApplicationContext(), R.string.btavailable, Toast.LENGTH_LONG).show();
} else if (!btadapter.isEnabled()) {
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt, REQUEST_ENABLE_BT);
}
////////////////////////////////////////////////////
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar (toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
TabConectBt tab4 = new TabConectBt();
return tab4;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Modos Basicos";
case 1:
return "Mis Modos";
case 2:
return "Estado";
case 3:
return "Conexion";
}
return null;
}
}
public boolean connectBluetooth() {
if (connection) {
//desconectar
try {
btsocket.close();
connection = false;
//butConectBt.setText(R.string.butConnect);
Toast.makeText(getApplicationContext(), R.string.btdisconnected, Toast.LENGTH_LONG).show();
return false;
} catch (IOException error) {
Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
}
} else {
//Conectar
Intent openlist = new Intent(MainMenu.this, DevicesList.class);
startActivityForResult(openlist, REQUEST_CONNECT_BT);
return true;
}
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(getApplicationContext(), R.string.btactivated, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), R.string.btnotactivated, Toast.LENGTH_LONG).show();
finish();
}
break;
case REQUEST_CONNECT_BT:
if (resultCode == Activity.RESULT_OK) {
MAC = data.getExtras().getString(DevicesList.MAC_ADRESS);
btdevice = btadapter.getRemoteDevice(MAC);
try {
btsocket = btdevice.createRfcommSocketToServiceRecord(MY_UUID);
btsocket.connect();
connection = true;
connectedThread = new ConnectThread(btsocket);
connectedThread.start();
// butConectBt.setText(R.string.butConnectPush);
Toast.makeText(getApplicationContext(), R.string.btconnected, Toast.LENGTH_LONG).show();
} catch (IOException error) {
connection = false;
Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.failmac, Toast.LENGTH_LONG).show();
}
}
}
}
package com.trajeledbt;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.trajeledbt.R;
public class Tab1 extends Fragment {
Button mode1;
Button mode2;
Button modeoff;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1 , container , false);
mode1 = (Button)view.findViewById(R.id.butMode1);
mode1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((MainMenu)getActivity()).connection){
((MainMenu)getActivity()).connectedThread.send("led1");
}
else{
Toast.makeText(((MainMenu)getActivity()).getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
}
}
});
mode2 = (Button)view.findViewById(R.id.butMode2);
mode2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((MainMenu)getActivity()).connection){
((MainMenu)getActivity()).connectedThread.send("led2");
}
else{
Toast.makeText(((MainMenu)getActivity()).getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
}
}
});
modeoff = (Button)view.findViewById(R.id.butOff);
modeoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((MainMenu)getActivity()).connection){
((MainMenu)getActivity()).connectedThread.send("off");
}
else{
Toast.makeText(((MainMenu)getActivity()).getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
package com.trajeledbt;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.trajeledbt.R;
/**
* Created by root on 12/06/17.
*/
public class Tab2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2 , container , false);
}
}
package com.trajeledbt;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.trajeledbt.R;
/**
* Created by root on 12/06/17.
*/
public class Tab3 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab3 , container , false);
}
}
package com.trajeledbt;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class TabConectBt extends Fragment {
Button butconnectBt;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.conect_bt , container , false);
butconnectBt = (Button)view.findViewById(R.id.butconnectBt);
butconnectBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainMenu)getActivity()).connectBluetooth();
}
});
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment