Created
April 9, 2014 14:52
-
-
Save Technicus/10279431 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 heck.pinball.ghostscanner; | |
| import android.os.Bundle; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| /* | |
| import android.os.Bundle; | |
| import android.app.Activity; | |
| import android.bluetooth.BluetoothAdapter; | |
| import android.bluetooth.BluetoothDevice; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import java.util.Set; | |
| import android.content.Intent; | |
| import android.content.IntentFilter; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.Button; | |
| import android.widget.ListView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| */ | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Set; | |
| import android.os.Bundle; | |
| import android.app.Activity; | |
| import android.bluetooth.BluetoothAdapter; | |
| import android.bluetooth.BluetoothDevice; | |
| import android.content.Intent; | |
| import android.view.Menu; | |
| import android.view.View; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.Button; | |
| import android.widget.ListAdapter; | |
| import android.widget.ListView; | |
| import android.widget.Toast; | |
| //package info.androidhive.tabsswipe; | |
| //import info.androidhive.tabsswipe.R; | |
| //import android.os.Bundle; | |
| import android.support.v4.app.Fragment; | |
| //import android.view.LayoutInflater; | |
| //import android.view.View; | |
| //import android.view.ViewGroup; | |
| public class BluetoothFragment extends Fragment { | |
| private Button On, Off, Visible, list; | |
| private BluetoothAdapter BA; | |
| private Set<BluetoothDevice> pairedDevices; | |
| private ListView lv; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| On = (Button) findViewById(R.id.button1); | |
| Off = (Button) findViewById(R.id.button2); | |
| Visible = (Button) findViewById(R.id.button3); | |
| list = (Button) findViewById(R.id.button4); | |
| lv = (ListView) findViewById(R.id.listView1); | |
| BA = BluetoothAdapter.getDefaultAdapter(); | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| View rootView = inflater.inflate(R.layout.fragment_bluetooth, container, false); | |
| pairedDevices = BA.getBondedDevices(); | |
| ArrayList list = new ArrayList(); | |
| for (BluetoothDevice bt : pairedDevices) | |
| list.add(bt.getName()); | |
| Toast.makeText(getApplicationContext(), "Showing Paired Devices", | |
| Toast.LENGTH_SHORT).show(); | |
| final ArrayAdapter adapter = new ArrayAdapter | |
| (this, android.R.layout.simple_list_item_1, list); | |
| lv.setAdapter(adapter); | |
| return rootView; | |
| } | |
| public void off(View view) { | |
| BA.disable(); | |
| Toast.makeText(getApplicationContext(), "Turned off", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| public void visible(View view) { | |
| Intent getVisible = new Intent(BluetoothAdapter. | |
| ACTION_REQUEST_DISCOVERABLE); | |
| startActivityForResult(getVisible, 0); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.main, menu); | |
| return true; | |
| } | |
| } | |
| /* private static final int REQUEST_ENABLE_BT = 1; | |
| private Button onBtn; | |
| private Button offBtn; | |
| private Button listBtn; | |
| private Button findBtn; | |
| private TextView text; | |
| private BluetoothAdapter myBluetoothAdapter; | |
| private Set<BluetoothDevice> pairedDevices; | |
| private ListView myListView; | |
| private ArrayAdapter<String> BTArrayAdapter; | |
| // take an instance of BluetoothAdapter - Bluetooth radio | |
| myBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); | |
| if(myBluetoothAdapter==null) | |
| { | |
| onBtn.setEnabled(false); | |
| offBtn.setEnabled(false); | |
| listBtn.setEnabled(false); | |
| findBtn.setEnabled(false); | |
| text.setText("Status: not supported"); | |
| Toast.makeText(getApplicationContext(), "Your device does not support Bluetooth", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| else | |
| { | |
| text = (TextView) findViewById(R.id.text); | |
| onBtn = (Button) findViewById(R.id.turnOn); | |
| onBtn.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| on(v); | |
| } | |
| }); | |
| offBtn = (Button) findViewById(R.id.turnOff); | |
| offBtn.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| off(v); | |
| } | |
| }); | |
| listBtn = (Button) findViewById(R.id.paired); | |
| listBtn.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| list(v); | |
| } | |
| }); | |
| findBtn = (Button) findViewById(R.id.search); | |
| findBtn.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| find(v); | |
| } | |
| }); | |
| myListView = (ListView) findViewById(R.id.listView1); | |
| // create the arrayAdapter that contains the BTDevices, and set it to the ListView | |
| BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); | |
| myListView.setAdapter(BTArrayAdapter); | |
| } | |
| public void on(View view){ | |
| if (!myBluetoothAdapter.isEnabled()) { | |
| Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); | |
| startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT); | |
| Toast.makeText(getApplicationContext(),"Bluetooth turned on" , | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| else{ | |
| Toast.makeText(getApplicationContext(),"Bluetooth is already on", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| if(requestCode == REQUEST_ENABLE_BT){ | |
| if(myBluetoothAdapter.isEnabled()) { | |
| text.setText("Status: Enabled"); | |
| } else { | |
| text.setText("Status: Disabled"); | |
| } | |
| } | |
| } | |
| public void list(View view){ | |
| // get paired devices | |
| pairedDevices = myBluetoothAdapter.getBondedDevices(); | |
| // put it's one to the adapter | |
| for(BluetoothDevice device : pairedDevices) | |
| BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress()); | |
| Toast.makeText(getApplicationContext(),"Show Paired Devices", | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| final BroadcastReceiver bReceiver = new BroadcastReceiver() { | |
| public void onReceive(Context context, Intent intent) { | |
| String action = intent.getAction(); | |
| // When discovery finds a device | |
| if (BluetoothDevice.ACTION_FOUND.equals(action)) { | |
| // Get the BluetoothDevice object from the Intent | |
| BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); | |
| // add the name and the MAC address of the object to the arrayAdapter | |
| BTArrayAdapter.add(device.getName() + "\n" + device.getAddress()); | |
| BTArrayAdapter.notifyDataSetChanged(); | |
| } | |
| } | |
| }; | |
| public void find(View view) { | |
| if (myBluetoothAdapter.isDiscovering()) { | |
| // the button is pressed when it discovers, so cancel the discovery | |
| myBluetoothAdapter.cancelDiscovery(); | |
| } | |
| else { | |
| BTArrayAdapter.clear(); | |
| myBluetoothAdapter.startDiscovery(); | |
| registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); | |
| } | |
| } | |
| public void off(View view){ | |
| myBluetoothAdapter.disable(); | |
| text.setText("Status: Disconnected"); | |
| Toast.makeText(getApplicationContext(),"Bluetooth turned off", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| unregisterReceiver(bReceiver); | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment