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:com.edutech.eBalanbot.eBalanbotActivity.java

@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    context = getApplicationContext();/*from w  ww  . j av a 2  s.c o m*/

    if (!getResources().getBoolean(R.bool.isTablet))
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Set portrait mode only - for small screens like phones
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER); // Full screen rotation
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); // Full screen rotation
        new Handler().postDelayed(new Runnable() { // Hack to hide keyboard when the layout it rotated
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // Hide the keyboard - this is needed when the device is rotated
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getApplicationWindowToken(), 0);
            }
        }, 1000);
    }

    setContentView(R.layout.activity_main);

    // Get local Bluetooth adapter
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
    else
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        showToast("Bluetooth is not available", Toast.LENGTH_LONG);
        finish();
        return;
    }

    // get sensorManager and initialize sensor listeners
    SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorFusion = new SensorFusion(getApplicationContext(), mSensorManager);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayHomeAsUpEnabled(true);
    // Create the adapter that will return a fragment for each of the primary sections of the app.
    ViewPagerAdapter mViewPagerAdapter = new ViewPagerAdapter(getApplicationContext(),
            getSupportFragmentManager());

    // Set up the ViewPager with the adapter.
    CustomViewPager mViewPager = (CustomViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mViewPagerAdapter);

    if (getResources().getBoolean(R.bool.isTablet))
        mViewPager.setOffscreenPageLimit(2); // Since two fragments is selected in landscape mode, this is used to smooth things out

    // Bind the underline indicator to the adapter
    mUnderlinePageIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
    mUnderlinePageIndicator.setViewPager(mViewPager);
    mUnderlinePageIndicator.setFades(false);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mUnderlinePageIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (D)
                Log.d(TAG, "ViewPager position: " + position);
            if (position < actionBar.getTabCount()) // Needed for when in landscape mode
                actionBar.setSelectedNavigationItem(position);
            else
                mUnderlinePageIndicator.setCurrentItem(position - 1);
        }
    });

    int count = mViewPagerAdapter.getCount();
    Resources mResources = getResources();
    boolean landscape = false;
    if (mResources.getBoolean(R.bool.isTablet)
            && mResources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        landscape = true;
        count -= 1; // There is one less tab when in landscape mode
    }

    for (int i = 0; i < count; i++) { // For each of the sections in the app, add a tab to the action bar
        String text;
        if (landscape && i == count - 1)
            text = mViewPagerAdapter.getPageTitle(i) + " & " + mViewPagerAdapter.getPageTitle(i + 1); // Last tab in landscape mode have two titles in one tab
        else
            text = mViewPagerAdapter.getPageTitle(i).toString();

        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab().setText(text).setTabListener(this));
    }
    try {
        PackageManager mPackageManager = getPackageManager();
        if (mPackageManager != null)
            eBalanbotActivity.appVersion = mPackageManager.getPackageInfo(getPackageName(), 0).versionName; // Read the app version name
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.duinopeak.balanbot.BalanbotActivity.java

@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    context = getApplicationContext();//from   www.j ava2 s.c  o  m

    if (!getResources().getBoolean(R.bool.isTablet))
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Set portrait mode only - for small screens like phones
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER); // Full screen rotation
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); // Full screen rotation
        new Handler().postDelayed(new Runnable() { // Hack to hide keyboard when the layout it rotated
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // Hide the keyboard - this is needed when the device is rotated
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getApplicationWindowToken(), 0);
            }
        }, 1000);
    }

    setContentView(R.layout.activity_main);

    // Get local Bluetooth adapter
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
        mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
    else
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        showToast("Bluetooth is not available", Toast.LENGTH_LONG);
        finish();
        return;
    }

    // get sensorManager and initialize sensor listeners
    SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorFusion = new SensorFusion(getApplicationContext(), mSensorManager);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayHomeAsUpEnabled(true);

    // Create the adapter that will return a fragment for each of the primary sections of the app.
    ViewPagerAdapter mViewPagerAdapter = new ViewPagerAdapter(getApplicationContext(),
            getSupportFragmentManager());

    // Set up the ViewPager with the adapter.
    CustomViewPager mViewPager = (CustomViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mViewPagerAdapter);

    if (getResources().getBoolean(R.bool.isTablet))
        mViewPager.setOffscreenPageLimit(2); // Since two fragments is selected in landscape mode, this is used to smooth things out

    // Bind the underline indicator to the adapter
    mUnderlinePageIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
    mUnderlinePageIndicator.setViewPager(mViewPager);
    mUnderlinePageIndicator.setFades(false);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mUnderlinePageIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (D)
                Log.d(TAG, "ViewPager position: " + position);
            if (position < actionBar.getTabCount()) // Needed for when in landscape mode
                actionBar.setSelectedNavigationItem(position);
            else
                mUnderlinePageIndicator.setCurrentItem(position - 1);
        }
    });

    int count = mViewPagerAdapter.getCount();
    Resources mResources = getResources();
    boolean landscape = false;
    if (mResources.getBoolean(R.bool.isTablet)
            && mResources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        landscape = true;
        count -= 1; // There is one less tab when in landscape mode
    }

    for (int i = 0; i < count; i++) { // For each of the sections in the app, add a tab to the action bar
        String text;
        if (landscape && i == count - 1)
            text = mViewPagerAdapter.getPageTitle(i) + " & " + mViewPagerAdapter.getPageTitle(i + 1); // Last tab in landscape mode have two titles in one tab
        else
            text = mViewPagerAdapter.getPageTitle(i).toString();

        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab().setText(text).setTabListener(this));
    }
    try {
        PackageManager mPackageManager = getPackageManager();
        if (mPackageManager != null)
            BalanbotActivity.appVersion = mPackageManager.getPackageInfo(getPackageName(), 0).versionName; // Read the app version name
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.MyHealthHubGlassMainActivity.java

private void openSelectDeviceDialog(String sensorType) {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
        BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices()
                .toArray(new BluetoothDevice[0]);

        int mDeviceIndex = 0;
        if (mAllBondedDevices.length > 0) {
            int deviceCount = mAllBondedDevices.length;
            String[] deviceNames = new String[deviceCount];
            int i = 0;
            for (BluetoothDevice device : mAllBondedDevices) {
                deviceNames[i++] = device.getName() + "|" + device.getAddress();
            }//from w w w .ja  va  2 s. c  om
            DialogFragment deviceDialog = new SelectDeviceDialogFragment();
            Bundle args = new Bundle();
            args.putString("sensorType", sensorType);
            args.putStringArray("names", deviceNames);
            args.putInt("position", mDeviceIndex);
            args.putBoolean("device", true);
            deviceDialog.setArguments(args);
            getFragmentManager().beginTransaction();
            deviceDialog.show(getSupportFragmentManager().beginTransaction(), "deviceDialog");
        }
    }
}

From source file:com.tribalyte.plugin.myo.MyoApi.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext cbc) throws JSONException {
    boolean res = true;
    logd("Execute: " + action + ", args len: " + args.length() + ", args: " + args.toString());
    if (ACTION_INIT.equals(action)) {
        initHub(cbc);//w  w  w.ja v a  2 s .  com
    } else if (ACTION_SHUTDOWN.equals(action)) {
        mHub.shutdown();
        cbc.success();
    } else if (ACTION_OPEN_SCAN_DLG.equals(action)) {
        startScanActivity(cbc);
    } else if (ACTION_ATTACH_ADJ.equals(action)) {
        attachToAdjacentMyo(null, cbc);
    } else if (ACTION_ATTACH_ADJS.equals(action)) {
        attachToAdjacentMyo(args.getInt(0), cbc);
    } else if (ACTION_ATTACH_MAC.equals(action)) {
        attachToAdjacentMyo(args.getString(0), cbc);
    } else if (ACTION_DETACH.equals(action)) {
        mHub.detach(args.getString(0));
        cbc.success();
    } else if (ACTION_SET_LOCK_POLICY.equals(action)) {
        mHub.setLockingPolicy(Hub.LockingPolicy.valueOf(args.getString(0)));
        cbc.success();
    } else if (ACTION_GET_LOCK_POLICY.equals(action)) {
        String policy = mHub.getLockingPolicy().name();
        cbc.success(policy);
    } else if (ACTION_SET_SENDUSAGE.equals(action)) {
        mHub.setSendUsageData(args.getBoolean(0));
        cbc.success();
    } else if (ACTION_IS_SENDUSAGE.equals(action)) {
        cbc.success(mHub.isSendingUsageData() ? 1 : 0);
    } else if (ACTION_GET_ATTACH_ALLOWANCE.equals(action)) {
        cbc.success(mHub.getMyoAttachAllowance());
    } else if (ACTION_SET_ATTACH_ALLOWANCE.equals(action)) {
        mHub.setMyoAttachAllowance(args.getInt(0));
        cbc.success();
    } else if (ACTION_GET_DEVICES.equals(action)) {
        JSONArray myoList = new JSONArray();
        for (Myo myo : mHub.getConnectedDevices()) {
            myoList.put(JsonMapper.toJson(myo));
        }
        cbc.success(myoList);
    } else if (ACTION_NOW.equals(action)) {
        cbc.success(Long.toString(mHub.now()));
    } else if (ACTION_ON.equals(action)) {
        mController.setEventHandler(args.getString(0), cbc);
    } else if (ACTION_OFF.equals(action)) {
        mController.removeEventHandler(args.getString(0));
    } else if (ACTION_MYO_IS_UNLOCKED.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            cbc.success(myo.isUnlocked() ? 1 : 0);
        }
    } else if (ACTION_MYO_LOCK.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            myo.lock();
            cbc.success();
        }
    } else if (ACTION_MYO_UNLOCK.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            myo.unlock(Myo.UnlockType.valueOf(args.getString(1)));
            cbc.success();
        }
    } else if (ACTION_MYO_REQUEST_RSSI.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            myo.requestRssi();
            cbc.success();
        }
    } else if (ACTION_MYO_VIBRATE.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            myo.vibrate(Myo.VibrationType.valueOf(args.getString(1)));
            cbc.success();
        }
    } else if (ACTION_MYO_NOTIFY_USER.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            myo.notifyUserAction();
            cbc.success();
        }
    } else if (ACTION_MYO_GET_CONNECT_STATE.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            cbc.success(myo.getConnectionState().name());
        }
    } else if (ACTION_MYO_IS_CONNECTED.equals(action)) {
        Myo myo = mController.getMyoOrErr(args.getString(0), cbc);
        if (myo != null) {
            cbc.success(myo.isConnected() ? 1 : 0);
        }
    } else if (ACTION_IS_BLUETOOTH_ENABLED.equals(action)) {
        try {
            cbc.success(BluetoothAdapter.getDefaultAdapter().isEnabled() ? 1 : 0);
        } catch (Exception e) {
            loge("Error accessing Bluetooth adapter", e);
            cbc.error("Error accessing Bluetooth adapter");
        }
    } else if (ACTION_OPEN_BLUETOOTH_CONFIG.equals(action)) {
        startBluetoothConfigActivity();
        mOpenBtConfigCbc = cbc;
    } else {
        logw("Action not supported: " + action);
        res = false; //Will result in a "MethodNotFound" error
    }

    //TODO: add Hub.Scanner functionality?

    return res;
}

From source file:com.netlinks.healthbracelet.service.HeartRateService.java

/**
 * Constructor.
 */
public HeartRateService() {
    mAdapter = BluetoothAdapter.getDefaultAdapter();

}

From source file:edu.stanford.mobisocial.dungbeetle.ui.fragments.FeedActionsFragment.java

public void requestBluetooth() {
    if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        Intent bt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(bt, REQUEST_BT_ENABLE);
        return;//  ww w .  j av a2 s . c  o  m
    }

    final int DISCO_LENGTH = 300;
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCO_LENGTH);
    startActivityForResult(discoverableIntent, REQUEST_BT_BROADCAST);
}

From source file:com.shenqu.jlplayer.nRFUARTv2.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_SELECT_DEVICE:
        //When the DeviceList-Activity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address == " + mDevice + " mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);
        }//from  ww  w. j av  a 2  s.c o  m
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();
        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.example.android.lightcontrol.MainActivity.java

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getApplicationContext();//from w ww . j  a  va  2 s .c  om

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    if (mBluetoothAdapter.isEnabled()) {
        mChatService = new BluetoothChatService(this, mHandler);
    }

    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    final ActionBar actionBar = getActionBar();

    assert actionBar != null;
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (B) {
                B = false;
                Toast.makeText(MainActivity.this, "Round theme cancelled", Toast.LENGTH_SHORT).show();
            }

            if (position == 0) {
            }
            if (position == 1) {
                updatespinner();
                setupChat();
            }
            if (position == 2) {

            }
            if (position == 3) {
                setupGroupMode();
            }
            actionBar.setSelectedNavigationItem(position);
        }
    });

    /*for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            
    actionBar.addTab(actionBar.newTab()
            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
            .setTabListener(this));
    }*/
    actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(0)).setTabListener(this)
            .setIcon(R.drawable.btn_group_control_icon));
    actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(1)).setTabListener(this)
            .setIcon(R.drawable.btn_light_control_icon));
    actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(2)).setTabListener(this)
            .setIcon(R.drawable.btn_group_configure_icon));
    actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(3)).setTabListener(this)
            .setIcon(R.drawable.btn_sceario_control_icon));
}

From source file:uk.ac.horizon.ubihelper.service.Service.java

@Override
public void onCreate() {
    // One-time set-up...
    Log.d(TAG, "onCreate()");
    // TODO/*  w  w w .ja  va2s.  c om*/
    super.onCreate();
    // handler for requests
    mHandler = new Handler();
    // create taskbar notification
    int icon = R.drawable.service_notification_icon;
    CharSequence tickerText = getText(R.string.notification_start_message);
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = this;
    CharSequence contentTitle = getText(R.string.notification_title);
    CharSequence contentText = getText(R.string.notification_description);
    Intent notificationIntent = new Intent(this, MainPreferences.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    startForeground(RUNNING_ID, notification);

    channelManager = new ChannelManager(peerChannelFactory);
    // sensors
    if (!isEmulator()) {
        Log.d(TAG, "Create sensor channels...");

        SensorChannel magnetic = new SensorChannel("magnetic", this, Sensor.TYPE_MAGNETIC_FIELD);
        channelManager.addChannel(magnetic);
        SensorChannel accelerometer = new SensorChannel("accelerometer", this, Sensor.TYPE_ACCELEROMETER);
        channelManager.addChannel(accelerometer);
        SensorChannel gyro = new SensorChannel("gyro", this, Sensor.TYPE_GYROSCOPE);
        channelManager.addChannel(gyro);
        SensorChannel light = new SensorChannel("light", this, Sensor.TYPE_LIGHT);
        channelManager.addChannel(light);
        SensorChannel pressure = new SensorChannel("pressure", this, Sensor.TYPE_PRESSURE);
        channelManager.addChannel(pressure);
        SensorChannel proximity = new SensorChannel("proximity", this, Sensor.TYPE_PROXIMITY);
        channelManager.addChannel(proximity);
        SensorChannel temperature = new SensorChannel("temperature", this, Sensor.TYPE_TEMPERATURE);
        channelManager.addChannel(temperature);

        try {
            Field f = Sensor.class.getField("TYPE_AMBIENT_TEMPERATURE");
            SensorChannel sc = new SensorChannel("ambientTemperature", this, f.getInt(null));
            channelManager.addChannel(sc);
        } catch (Exception e) {
            Log.d(TAG, "Could not get field Sensor.TYPE_AMBIENT_TEMPERATURE");
        }
        try {
            Field f = Sensor.class.getField("TYPE_RELATIVE_HUMIDITY");
            SensorChannel sc = new SensorChannel("relativeHumidity", this, f.getInt(null));
            channelManager.addChannel(sc);
        } catch (Exception e) {
            Log.d(TAG, "Could not get field Sensor.TYPE_AMBIENT_TEMPERATURE");
        }

        // all sensors by full name
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (sensorManager != null) {
            List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
            for (Sensor sensor : sensors) {
                SensorChannel sc = new SensorChannel("sensor." + sensor.getName(), this, sensor);
                channelManager.addChannel(sc);
            }
        }
        BluetoothDiscoveryChannel btchannel = new BluetoothDiscoveryChannel(this, mHandler, "bluetooth");
        channelManager.addChannel(btchannel);
        WifiScannerChannel wifichannel = new WifiScannerChannel(this, mHandler, "wifi");
        channelManager.addChannel(wifichannel);
        GpsStatusChannel gpsstatus = new GpsStatusChannel("gpsstatus", this);
        channelManager.addChannel(gpsstatus);
        CellLocationChannel cellchannel = new CellLocationChannel(mHandler, this, "cell.location", false);
        channelManager.addChannel(cellchannel);
        CellLocationChannel cellchannel2 = new CellLocationChannel(mHandler, this, "cell.neighbors", true);
        channelManager.addChannel(cellchannel2);
        CellStrengthChannel cellchannel3 = new CellStrengthChannel(this, "cell.strength");
        channelManager.addChannel(cellchannel3);
    }
    channelManager.addChannel(new TimeChannel(mHandler, "time"));
    List<String> locationProviders = LocationChannel.getAllProviders(this);
    for (String provider : locationProviders) {
        LocationChannel locchannel = new LocationChannel("location." + provider, this, provider);
        channelManager.addChannel(locchannel);
    }

    channelManager.addChannel(new MicChannel(mHandler, "mic"));

    Log.d(TAG, "Create http server...");

    // http server
    httpPort = getPort();

    httpListener = new HttpListener(this, httpPort);
    httpListener.start();

    // peer communication
    peerManager = new PeerManager(this);
    int serverPort = peerManager.getServerPort();

    channelManager.addChannel(new EnabledPeersChannel(this, peerManager, "peers"));

    // wifi discovery
    wifiDiscoveryManager = new WifiDiscoveryManager(this);
    wifiDiscoverable = getWifiDiscoverable();
    wifiDiscoveryManager.setServerPort(serverPort);
    wifiDiscoveryManager.setEnabled(wifiDiscoverable);

    bluetooth = BluetoothAdapter.getDefaultAdapter();
    if (bluetooth != null)
        btmac = bluetooth.getAddress();

    telephony = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (telephony != null)
        imei = telephony.getDeviceId();

    logManager = new LogManager(this, channelManager);
    logManager.checkPreferences();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(onRunChangeListener);

    Log.d(TAG, "onCreate() finished");
}

From source file:com.evothings.BLE.java

private void stopScan(final CordovaArgs args, final CallbackContext callbackContext) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    adapter.stopLeScan(this);
    mScanCallbackContext = null;// w  w w . j  a  v a  2 s  .co m
}