Advertisement
Guest User

Untitled

a guest
Jul 13th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package com.example.blootooth;
  2.  
  3. import android.app.Activity;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.pm.PackageManager;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.widget.FrameLayout;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. public class MainActivity extends Activity {
  18.  
  19. int REQUEST_ENABLE_BT = 1;
  20. TextView text;
  21. BluetoothAdapter mBluetoothAdapter;
  22. private boolean mScanning;
  23. private Handler mHandler;
  24. private static final long SCAN_PERIOD = 10000;
  25. private String name;
  26. private int id;
  27.  
  28. FrameLayout container;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. text = (TextView) findViewById(R.id.textView1);
  35. container = (FrameLayout) findViewById(R.id.container);
  36.  
  37. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  38. if (mBluetoothAdapter == null) {
  39.  
  40. }
  41. if (mBluetoothAdapter != null) {
  42. Toast.makeText(getApplicationContext(), "You Nigga go on", 2000)
  43. .show();
  44.  
  45. }
  46. if (!mBluetoothAdapter.isEnabled()) {
  47. Intent enableBtIntent = new Intent(
  48. BluetoothAdapter.ACTION_REQUEST_ENABLE);
  49. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  50. }
  51. if (!getPackageManager().hasSystemFeature(
  52. PackageManager.FEATURE_BLUETOOTH_LE)) {
  53. Toast.makeText(this, "no BLE nigga", Toast.LENGTH_SHORT).show();
  54. finish();
  55. }
  56.  
  57. mBluetoothAdapter.startDiscovery();
  58.  
  59. RecieveDevices();
  60. }
  61.  
  62. public void RecieveDevices() {
  63.  
  64. // Create a BroadcastReceiver for ACTION_FOUND
  65. final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  66. public void onReceive(Context context, Intent intent) {
  67. String action = intent.getAction();
  68. // When discovery finds a device
  69. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  70. // Get the BluetoothDevice object from the Intent
  71. BluetoothDevice device = intent
  72. .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  73. // Add the name and address to an array adapter to show in a
  74. // ListView
  75. text.setText(device.getName() + "\n" + device.getAddress());
  76. name = device.getName();
  77.  
  78. }
  79. }
  80. };
  81. // Register the BroadcastReceiver
  82. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  83. registerReceiver(mReceiver, filter); // Don't forget to unregister
  84. // during onDestroy
  85.  
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement