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:edu.uwb.css.a545.project.socialplay.BluetoothChatFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);/*from  w w  w .  j av a  2s . com*/
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mServices = new ArrayList<BluetoothChatService>();
    deviceCount = 0;

    // 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:com.example.bikey.BluetoothActivity.java

/** 
 * Constructor. Prepares a new BluetoothChat session.
 * @param fragmentActivity  The UI Activity Context
 * @param handler  A Handler to send messages back to the UI Activity
 *//* w  ww  .  java 2s  .com*/
public BluetoothActivity(FragmentActivity fragmentActivity, Handler handler) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;
    mHandler = handler;
}

From source file:com.googlecode.android_scripting.facade.bluetooth.BluetoothSocketConnFacade.java

public BluetoothSocketConnFacade(FacadeManager manager) {
    super(manager);
    mEventFacade = manager.getReceiver(EventFacade.class);
    mService = manager.getService();//from  w  w  w.  j ava  2 s. co m
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:com.hwx.wheel.steeringwheel.bluetooth.ScaleActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)//from  w  w w .  j  a v a 2  s  .c om
        Log.e(TAG, "+++ ON CREATE +++");
    setContentView(R.layout.bluetooth_other);
    connect_state = (TextView) findViewById(R.id.connect_state);
    text_data = (TextView) findViewById(R.id.text_data);
    CONNECT_MAC = AppConfig.getInstance().getString("isEmpetConnectMac", "");
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    itemClickAdapter = new ItemClickAdapter(deviceList);
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    LocalBroadcastManager.getInstance(Application.getContext()).registerReceiver(mReceiver, filter);
    new CommandReceiver() {
        @Override
        public void onDataReceived(byte[] buffer, byte function, byte safeCod) {
        }

        @Override
        public void onFail() {
            connect_state.setText("fail connect");
            builder.setLength(0);
            connect_state.postDelayed(() -> startOpen(), 3000);
            startOpen();
        }

        @Override
        public void onLost() {
            connect_state.setText("lost connect");
            builder.setLength(0);
            connect_state.postDelayed(() -> startOpen(), 3000);
        }

        @Override
        public void onDeviceInfo(String name, String address) {
            connect_state.append("\t" + name);
        }

        @Override
        public void onStadeTag(int stade) {
            switch (stade) {
            case 0:
                connect_state.setText("we're doing nothing");
                break;
            case 1:
                connect_state.setText("now listening for incoming connections");
                break;
            case 2:
                connect_state.setText("now initiating an outgoing connection");
                break;
            case 3:
                connect_state.setText("now connected to a remote " + CONNECT_NAME);
                break;
            }
        }
    }.regiest();
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, ControlFragment.newInstance(1))
            .commit();
}

From source file:com.scytulip.nrfUARTbackrec.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from w  w  w .  ja  v a 2s.  c  o m
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    messageListView = (ListView) findViewById(R.id.listMessage);
    listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail);
    messageListView.setAdapter(listAdapter);
    messageListView.setDivider(null);
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    btnSend = (Button) findViewById(R.id.sendButton);
    btnDwnd = (Button) findViewById(R.id.dlButton);
    edtMessage = (EditText) findViewById(R.id.sendText);
    service_init();

    mBackData = new BackRecData(getApplicationContext());

    // 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 Send button  
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.sendText);
            String message = editText.getText().toString();
            byte[] value;
            try {
                //send data to service
                value = message.getBytes("UTF-8");
                mService.writeRXCharacteristic(value);
                //Update the log with time stamp
                String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
                listAdapter.add("[" + currentDateTimeString + "] TX: " + message);
                messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
                edtMessage.setText("");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    // Handler Download Button
    btnDwnd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                mService.writeRXCharacteristic("T".getBytes("UTF-8"));
            } catch (Exception e) {
                Log.i(TAG, e.toString());
            }

        }
    });

    // Set initial UI state

}

From source file:com.fastbootmobile.encore.app.fragments.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getPreferenceManager().setSharedPreferencesName(SettingsKeys.PREF_SETTINGS);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.settings);

    // Load Bluetooth paired devices
    final ListPreference btNameList = (ListPreference) findPreference(
            SettingsKeys.KEY_BLUETOOTH_AUTOCONNECT_NAME);
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    if (adapter == null) {
        // No Bluetooth adapter, remove preferences
        getPreferenceScreen().removePreference(findPreference(SettingsKeys.KEY_BLUETOOTH_AUTOCONNECT_ENABLE));
        getPreferenceScreen().removePreference(findPreference(SettingsKeys.KEY_BLUETOOTH_AUTOCONNECT_ACTION));
        getPreferenceScreen().removePreference(findPreference(SettingsKeys.KEY_BLUETOOTH_AUTOCONNECT_NAME));
        getPreferenceScreen().removePreference(findPreference("category_bluetooth"));
    } else {/* w  w w . j  av  a 2 s .com*/
        Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
        CharSequence[] entries = new CharSequence[pairedDevices.size()];

        String currentPreferred = getPrefs().getString(SettingsKeys.KEY_BLUETOOTH_AUTOCONNECT_NAME, null);

        int i = 0;
        for (BluetoothDevice device : pairedDevices) {
            entries[i] = device.getName();
            ++i;
        }
        btNameList.setEntries(entries);
        btNameList.setEntryValues(entries);

        if (currentPreferred != null) {
            btNameList.setDefaultValue(currentPreferred);
        }
    }
}

From source file:com.cardiograph.view.BLEActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from w  w w  . ja  v  a  2s. c  om
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    messageListView = (ListView) findViewById(R.id.listMessage);
    listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail);
    messageListView.setAdapter(listAdapter);
    messageListView.setDivider(null);
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    btnSend = (Button) findViewById(R.id.sendButton);
    edtMessage = (EditText) findViewById(R.id.sendText);
    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(BLEActivity.this, DeviceListActivity.class);
                    startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
                } else {
                    //Disconnect button pressed
                    if (mDevice != null) {
                        mService.disconnect();

                    }
                }
            }
        }
    });
    // Handler Send button  
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.sendText);
            String message = editText.getText().toString();
            byte[] value;
            try {
                //send data to service
                value = message.getBytes("UTF-8");
                mService.writeRXCharacteristic(value);
                //Update the log with time stamp
                String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
                listAdapter.add("[" + currentDateTimeString + "] TX: " + message);
                messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
                edtMessage.setText("");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    // Set initial UI state

}

From source file:net.openracer.remote.DeviceListActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i(LOGTAG, "on-create");

    setContentView(R.layout.activity_device_list);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    setResult(Activity.RESULT_CANCELED);

    Button scanButton = (Button) findViewById(R.id.button1);
    scanButton.setOnClickListener(new OnClickListener() {
        @Override//from w  ww  .j a  v a 2 s.  co m
        public void onClick(View v) {

            if (hasBtPerms()) {
                Log.e(LOGTAG, "pushed");
                v.setEnabled(false);
                discoveredDevices.clear();
                doDiscovery();
            }
        }
    });

    pairedDevices = new ArrayAdapter<String>(this, R.layout.btdevice_list_item);
    discoveredDevices = new ArrayAdapter<String>(this, R.layout.btdevice_list_item);

    ListView discoveredListView = (ListView) findViewById(R.id.listView2);
    discoveredListView.setAdapter(discoveredDevices);
    discoveredListView.setOnItemClickListener(deviceClickListener);

    ListView pairedListView = (ListView) findViewById(R.id.listView1);
    pairedListView.setAdapter(pairedDevices);
    pairedListView.setOnItemClickListener(deviceClickListener);

    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        Log.e(LOGTAG, "No bluetooth adapter");
    }

    if (!hasBtPerms()) {
        Button button = (Button) findViewById(R.id.button1);
        button.setText("No Bluetooth permissions");
        button.setEnabled(false);
    }

    if (btAdapter != null) {
        if (!btAdapter.isEnabled()) {
            Button button = (Button) findViewById(R.id.button1);
            button.setText("Bluetooth not enabled");
            button.setEnabled(false);
        }
        Set<BluetoothDevice> sysPairedDevices = btAdapter.getBondedDevices();
        if (sysPairedDevices.isEmpty()) {
            pairedDevices.add("No paired devices.");
        } else {
            for (BluetoothDevice pDev : sysPairedDevices) {
                pairedDevices.add(pDev.getName() + "\n" + pDev.getAddress());
            }
        }
    } else {
        Button button = (Button) findViewById(R.id.button1);
        button.setText("No Bluetooth device");
        button.setEnabled(false);
    }

}

From source file:com.nordicsemi.UART_UDP_PROXY.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*from  www .  j  a v  a 2  s.co m*/
    this.m_this = this;
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    messageListView = (ListView) findViewById(R.id.listMessage);
    listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail);
    messageListView.setAdapter(listAdapter);
    messageListView.setDivider(null);
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    exitButton = (Button) findViewById(R.id.exitButton);
    exitButton.setEnabled(true);
    btnConnectDisconnect.setEnabled(true);
    mRemoteRssiVal = (TextView) findViewById(R.id.rssival);
    mRemoteRssiVal.setText("");
    service_init();

    listAdapter.clear();
    listAdapter.add("Ready...");
    messageListView.smoothScrollToPosition(0);
    listAdapter.notifyDataSetChanged();
    messageListView.invalidateViews();

    // disable the screen saver
    getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

    // 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) {
                        // disconnect
                        mService.disconnect();

                        // clear the UI
                        listAdapter.clear();
                        listAdapter.add("Ready...");
                        messageListView.smoothScrollToPosition(0);
                        listAdapter.notifyDataSetChanged();
                        messageListView.invalidateViews();
                    }
                }
            }
        }
    });
    // Handler Send button  
    exitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // DA
            mService.disconnect();
            Log.d(TAG, "exiting proxy application...");
            finish();
            System.exit(0);
        }
    });

    // Set initial UI state

}

From source file:com.wordpress.bennthomsen.bleuart.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from ww w .j  av  a  2 s. c om
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    messageListView = (ListView) findViewById(R.id.listMessage);
    listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail);
    messageListView.setAdapter(listAdapter);
    messageListView.setDivider(null);
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    btnSend = (Button) findViewById(R.id.sendButton);
    edtMessage = (EditText) findViewById(R.id.sendText);
    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 Send button
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.sendText);
            String message = editText.getText().toString();
            byte[] value;
            try {
                //send data to service
                value = message.getBytes("UTF-8");
                mService.writeRXCharacteristic(value);
                //Update the log with time stamp
                String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
                listAdapter.add("[" + currentDateTimeString + "] TX: " + message);
                messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
                edtMessage.setText("");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    // Set initial UI state

}