Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.blootooth;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- int REQUEST_ENABLE_BT = 1;
- TextView text;
- BluetoothAdapter mBluetoothAdapter;
- private boolean mScanning;
- private Handler mHandler;
- private static final long SCAN_PERIOD = 10000;
- private String name;
- private int id;
- FrameLayout container;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text = (TextView) findViewById(R.id.textView1);
- container = (FrameLayout) findViewById(R.id.container);
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (mBluetoothAdapter == null) {
- }
- if (mBluetoothAdapter != null) {
- Toast.makeText(getApplicationContext(), "You Nigga go on", 2000)
- .show();
- }
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(
- BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- if (!getPackageManager().hasSystemFeature(
- PackageManager.FEATURE_BLUETOOTH_LE)) {
- Toast.makeText(this, "no BLE nigga", Toast.LENGTH_SHORT).show();
- finish();
- }
- mBluetoothAdapter.startDiscovery();
- RecieveDevices();
- }
- public void RecieveDevices() {
- // Create a BroadcastReceiver for ACTION_FOUND
- final BroadcastReceiver mReceiver = 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 address to an array adapter to show in a
- // ListView
- text.setText(device.getName() + "\n" + device.getAddress());
- name = device.getName();
- }
- }
- };
- // Register the BroadcastReceiver
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(mReceiver, filter); // Don't forget to unregister
- // during onDestroy
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement