Example usage for android.bluetooth BluetoothAdapter getDefaultAdapter

List of usage examples for android.bluetooth BluetoothAdapter getDefaultAdapter

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter getDefaultAdapter.

Prototype

public static synchronized BluetoothAdapter getDefaultAdapter() 

Source Link

Document

Get a handle to the default local Bluetooth adapter.

Usage

From source file:activeng.pt.activenglab.BluetoothChatService.java

/**
 * Constructor. Prepares a new BluetoothChat session.
 *
 * @param context The UI Activity Context
 *///from  w  w  w .j av a2 s .  c  o  m
//public BluetoothChatService(Context context, Handler handler) {
public BluetoothChatService(Context context) {
    mContext = context;
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = Constants.STATE_NONE;
    //mHandler = handler;

    connectionUpdates = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            Log.d("ActivEng", "BluetoothChatService --> onReceive");
            if (extras != null) {
                String message = extras.getString(Intent.EXTRA_TEXT);
                Log.d("ActivEng", message);
                write(message + "\n");
            }
        }
    };

}

From source file:com.github.vgoliveira.panificadora.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from   ww  w. ja va  2s  . c o m

    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    time_more = (Button) findViewById(R.id.button_time_more);
    time_less = (Button) findViewById(R.id.button_time_less);
    dough_qnt = (Button) findViewById(R.id.button_dough_qnt);
    options = (Button) findViewById(R.id.button_options);
    color = (Button) findViewById(R.id.button_color);
    init_stop = (Button) findViewById(R.id.button_init_stop);
    startRecipe = (Button) findViewById(R.id.startRecipesBookSelectionActivity);

    // dont allow the user click on these buttons if not connected and GATT service discovered.
    time_more.setEnabled(false);
    time_less.setEnabled(false);
    dough_qnt.setEnabled(false);
    options.setEnabled(false);
    color.setEnabled(false);
    init_stop.setEnabled(false);

    isConnected = false;

    service_init();

    // Handler Disconnect & Connect button
    btnConnectDisconnect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mBtAdapter.isEnabled()) {
                Log.i(TAG, "onClick - BT not enabled yet");
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            } else {
                if (btnConnectDisconnect.getText().equals("Connect")) {

                    //Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices
                    Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class);
                    startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
                } else {
                    //Disconnect button pressed
                    if (mDevice != null) {
                        mService.disconnect();

                    }
                }
            }
        }
    });

    //Handler startRecipe
    startRecipe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent booksIntent = new Intent(MainActivity.this, SelectionListActivity.class);
            booksIntent.putExtra("file", "recipes_books");
            booksIntent.putExtra("level", "books");
            booksIntent.putExtra("list_title", "Livros de receitas");

            booksIntent.putExtra("isConnected", isConnected);
            startActivityForResult(booksIntent, REQUEST_SELECT_RECIPE);
        }
    });

    time_more.setOnTouchListener(this);
    time_less.setOnTouchListener(this);
    options.setOnTouchListener(this);
    dough_qnt.setOnTouchListener(this);
    color.setOnTouchListener(this);
    init_stop.setOnTouchListener(this);

    checkLogin();

}

From source file:com.example.robert.bluetoothnew.BluetoothChatFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);/*w w w .jav a 2s  .  c  om*/
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    initSingleTonTemp();
    // If the adapter is null, then Bluetooth is not supported
    intiCallWeb();
    toWebPosition();
    if (mBluetoothAdapter == null) {
        FragmentActivity activity = getActivity();
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        activity.finish();
    } // No bluetooth service

    tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onInit(int status) {
            Log.v("abasdasd", "status 123");

            if (status == TextToSpeech.SUCCESS) {

                int result = tts.setLanguage(Locale.TAIWAN);

                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                } else {

                }

            } else {
                Log.e("TTS", "Initilization Failed!");
            }
        }
    });
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.fragments.SensorConfigFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);//from ww  w. j av a 2  s .  co m

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // no bluetooth support on this device, app should be stop
        Toast.makeText(getActivity().getApplicationContext(), "No Bluetooth Support!", Toast.LENGTH_LONG)
                .show();
        getActivity().finish();
    }
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        return;
    }

    // start create Sensors when bluetooth is enabled;
    startSensorModule();
}

From source file:com.pipirssolutions.aeseztargetcontroller.ProgramFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);/*from  w  w  w  . j a va2 s  . com*/

    handler = new Handler();

    blocked = true;

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        FragmentActivity activity = getActivity();
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        activity.finish();
    }
}

From source file:uk.ac.gcu.bluedroid.BluetoothChatService.java

/**
 * Constructor. Prepares a new MainActivity session.
 * /*ww w. j  a va 2  s.c  o  m*/
 * @param context
 *            The UI Activity Context
 * @param handler
 *            A Handler to send messages back to the UI Activity
 */
public BluetoothChatService(Context context, Handler handler) {
    GsonBuilder builder = new GsonBuilder();
    gson = builder.create();
    ackHandler = new Handler();

    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;
    mHandler = handler;
    tags = new ArrayList<String>();
}

From source file:com.cognizant.glass.bluetoothconnect.DemoAppRecieverService.java

@Override
public void onCreate() {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mDeviceAddresses = new ArrayList<String>();
    mConnThreads = new ArrayList<ConnectedThread>();
    mSockets = new ArrayList<BluetoothSocket>();
    mUuids = new ArrayList<UUID>();
    mUuids.add(UUID.fromString(clientId));
    beaconList = new ArrayList<String>();
    start();/*from www .  j ava2 s.  co m*/
}

From source file:com.example.deer.boochat.chat_room.BluetoothChatFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);/*from   w  ww .  j  ava  2 s .  c o m*/
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        FragmentActivity activity = getActivity();
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        activity.finish();
    }

    Bundle bundle = getArguments();
    if (bundle != null) {
        mDeviceName = bundle.getString(EXTRAS_DEVICE_NAME);
        mDeviceAddress = bundle.getString(EXTRAS_DEVICE_ADDRESS);
    }

}

From source file:fr.bmartel.android.dotti.DottiActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dotti);

    // Use this check to determine whether BLE is supported on the device. Then
    // you can selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "Bluetooth Smart is not supported on your device", Toast.LENGTH_SHORT).show();
        finish();/* ww w  .  j a va 2  s  .  c  o m*/
    }

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    final ProgressBar progress_bar = (ProgressBar) findViewById(R.id.scanningProgress);

    if (progress_bar != null)
        progress_bar.setEnabled(false);

    final Button button_stop_scanning = (Button) findViewById(R.id.stop_scanning_button);

    if (button_stop_scanning != null)
        button_stop_scanning.setEnabled(false);

    final TextView scanText = (TextView) findViewById(R.id.scanText);

    if (scanText != null)
        scanText.setText("");

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

    button_stop_scanning.setEnabled(false);

    final Button button_find_accessory = (Button) findViewById(R.id.scanning_button);

    button_stop_scanning.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (currentService != null && currentService.isScanning()) {

                currentService.stopScan();

                if (progress_bar != null) {
                    progress_bar.setEnabled(false);
                    progress_bar.setVisibility(View.GONE);
                }

                if (scanText != null)
                    scanText.setText("");

                if (button_stop_scanning != null)
                    button_stop_scanning.setEnabled(false);
            }
        }
    });

    button_find_accessory.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            triggerNewScan();
        }
    });

    if (mBluetoothAdapter.isEnabled()) {

        Intent intent = new Intent(this, DottiBtService.class);

        // bind the service to current activity and create it if it didnt exist before
        startService(intent);
        bound = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }
}

From source file:com.phonegap.plugin.BluetoothPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    Log.d(LOG_TAG, "Plugin Called");
    this.callbackContext = callbackContext;
    PluginResult result = null;/*w  w w .  j a  v  a  2s  .  c  o  m*/

    //Looper.prepare();
    btadapter = BluetoothAdapter.getDefaultAdapter();
    found_devices = new ArrayList<BluetoothDevice>();

    if (ACTION_DISCOVER_DEVICES.equals(action)) {
        try {

            Log.d(LOG_TAG, "We're in " + ACTION_DISCOVER_DEVICES);

            found_devices.clear();
            discovering = true;

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            Log.i(LOG_TAG, "Discovering devices...");
            btadapter.startDiscovery();

            result = new PluginResult(PluginResult.Status.NO_RESULT);
            result.setKeepCallback(true);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_IS_BT_ENABLED);

            boolean isEnabled = btadapter.isEnabled();

            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED,
                    "Returning " + "is Bluetooth Enabled? " + isEnabled);
            result = new PluginResult(PluginResult.Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_ENABLE_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_ENABLE_BT);

            boolean enabled = false;

            Log.d(LOG_TAG, "Enabling Bluetooth...");

            if (btadapter.isEnabled()) {
                enabled = true;
            } else {
                enabled = btadapter.enable();
            }

            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled);
            result = new PluginResult(PluginResult.Status.OK, enabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_DISABLE_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_DISABLE_BT);

            boolean disabled = false;

            Log.d(LOG_TAG, "Disabling Bluetooth...");

            if (btadapter.isEnabled()) {
                disabled = btadapter.disable();
            } else {
                disabled = true;
            }

            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled);
            result = new PluginResult(PluginResult.Status.OK, disabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_PAIR_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_PAIR_BT);

            String addressDevice = args.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean paired = false;

            Log.d(LOG_TAG, "Pairing with Bluetooth device with name " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("createBond");
                paired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired);
            result = new PluginResult(PluginResult.Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_UNPAIR_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_UNPAIR_BT);

            String addressDevice = args.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean unpaired = false;

            Log.d(LOG_TAG, "Unpairing Bluetooth device with " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("removeBond");
                unpaired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired);
            result = new PluginResult(PluginResult.Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_LIST_BOUND_DEVICES);

            Log.d(LOG_TAG, "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i(LOG_TAG, device.getName() + " " + device.getAddress() + " " + device.getBondState());

                    if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                        resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ,"
                                + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                                + device.getBluetoothClass().getDeviceClass() + "\" }";
                        if (count < pairedDevices.size() - 1)
                            resultBoundDevices = resultBoundDevices + ",";
                    } else
                        Log.i(LOG_TAG, device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices);
            result = new PluginResult(PluginResult.Status.OK, resultBoundDevices);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_STOP_DISCOVERING_BT);

            boolean stopped = true;

            Log.d(LOG_TAG, "Stop Discovering Bluetooth Devices...");

            if (btadapter.isDiscovering()) {
                Log.i(LOG_TAG, "Stop discovery...");
                stopped = btadapter.cancelDiscovery();
                discovering = false;
            }

            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped);
            result = new PluginResult(PluginResult.Status.OK, stopped);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

    } else if (ACTION_IS_BOUND_BT.equals(action)) {
        try {
            Log.d(LOG_TAG, "We're in " + ACTION_IS_BOUND_BT);
            String addressDevice = args.getString(0);
            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            Log.i(LOG_TAG, "BT Device in state " + device.getBondState());

            boolean state = false;

            if (device != null && device.getBondState() == 12)
                state = true;
            else
                state = false;

            Log.d(LOG_TAG, "Is Bound with " + device.getName() + " - address " + device.getAddress());

            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state);
            result = new PluginResult(PluginResult.Status.OK, state);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

        /*
              } else if ( ACTION_READ.equals(action) )  {
                 final int socketId = args.getInt(0);
                 final int bufferSize = args.getInt(1);
                 this.callback_read = callbackContext;
                 ReadThread readThread = new ReadThread(
                       m_sockets.get(socketId),socketId,bufferSize);
                 readThread.start();
                 m_readThreads.add(readThread);
                 PluginResult pluginResult = new PluginResult(
                       PluginResult.Status.NO_RESULT);
                 pluginResult.setKeepCallback(true);
                 callbackContext.sendPluginResult(pluginResult);
                 return true;
        */

    } else {
        result = new PluginResult(PluginResult.Status.INVALID_ACTION);
        Log.d(LOG_TAG, "Invalid action : " + action + " passed");
    }
    this.callbackContext.sendPluginResult(result);
    return true;
}