Example #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_scan);
    final BluetoothManager bluetooth_manager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

    bluetooth_adapter = bluetooth_manager.getAdapter();
    mHandler = new Handler();

    if (bluetooth_adapter == null) {
      Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_LONG).show();
      finish();
      return;
    }
    if (!bluetooth_manager.getAdapter().isEnabled()) {
      Toast.makeText(this, "Bluetooth is turned off on this device currently", Toast.LENGTH_LONG)
          .show();
    } else {
      if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        Toast.makeText(
                this,
                "The android version of this device is not compatible with Bluetooth Low Energy",
                Toast.LENGTH_LONG)
            .show();
      }
    }
    // Will request that GPS be enabled for devices running Marshmallow or newer.
    LocationHelper.requestLocationForBluetooth(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) initializeScannerCallback();

    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // 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, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
      finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
      Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    Log.i("SCAN", "" + mBluetoothAdapter.isMultipleAdvertisementSupported());
    Log.i("SCAN", "" + mBluetoothAdapter.isOffloadedFilteringSupported());
    Log.i("SCAN", "" + mBluetoothAdapter.isOffloadedScanBatchingSupported());
  }
  @SuppressLint("NewApi")
  public void setview() {
    setContentView(R.layout.ibeacon);
    lin_device = (LinearLayout) findViewById(R.id.lin_device);
    so = (Button) findViewById(R.id.bt_so);
    so.setOnClickListener(
        new OnClickListener() {

          public void onClick(View v) {
            ibeacon.clear();
            lin_device.removeAllViews();
          }
        });

    mHandler = new Handler();
    ibeacon = new ArrayList<Ibeacon_device>();

    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
      finish();
    }
    // Initializes a Bluetooth adapter. For API level 18 and above, get a
    // reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
      finish();
      return;
    }
  }
Example #4
0
 private String getLocalBluetoothMAC(Context context) {
   BluetoothManager manager =
       (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
   BluetoothAdapter adapter = manager.getAdapter();
   String mac = adapter.getAddress();
   return mac;
 }
 public boolean isSupportBle() {
   // is support 4.0 ?
   final BluetoothManager bluetoothManager =
       (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
   mBluetoothAdapter = bluetoothManager.getAdapter();
   if (mBluetoothAdapter == null) return false;
   else return true;
 }
 @Override
 public void onCreate() {
   Log.i(TAG, "onCreate of IBeaconService called");
   // Initializes Bluetooth adapter.
   final BluetoothManager bluetoothManager =
       (BluetoothManager) this.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
   bluetoothAdapter = bluetoothManager.getAdapter();
 }
  private void startLeScan() {
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

    this.bluetoothAdapter = bluetoothManager.getAdapter();

    Log.d(TAG, "startLeScan() ... ");
    this.bluetoothAdapter.startLeScan(this);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calibeate_location);

    Button okButton = (Button) findViewById(R.id.ok);
    okButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent returnIntent = new Intent();
            if (distance1 < distance2 && distance1 < distance3) {
              returnIntent.putExtra("beaconAmaj", BEACON_MAJOR_1);
              returnIntent.putExtra("beaconAmin", BEACON_MINOR_1);
              returnIntent.putExtra("beaconBmaj", BEACON_MAJOR_2);
              returnIntent.putExtra("beaconBmin", BEACON_MAJOR_2);
              returnIntent.putExtra("beaconBdist", distance2);
              returnIntent.putExtra("beaconCmaj", BEACON_MAJOR_3);
              returnIntent.putExtra("beaconCmin", BEACON_MAJOR_3);
              returnIntent.putExtra("beaconCdist", distance3);
            } else if (distance2 < distance1 && distance2 < distance3) {
              returnIntent.putExtra("beaconAmaj", BEACON_MAJOR_2);
              returnIntent.putExtra("beaconAmin", BEACON_MINOR_2);
              returnIntent.putExtra("beaconBmaj", BEACON_MAJOR_1);
              returnIntent.putExtra("beaconBmin", BEACON_MAJOR_1);
              returnIntent.putExtra("beaconBdist", distance1);
              returnIntent.putExtra("beaconCmaj", BEACON_MAJOR_3);
              returnIntent.putExtra("beaconCmin", BEACON_MAJOR_3);
              returnIntent.putExtra("beaconCdist", distance3);
            } else {
              returnIntent.putExtra("beaconAmaj", BEACON_MAJOR_3);
              returnIntent.putExtra("beaconAmin", BEACON_MINOR_3);
              returnIntent.putExtra("beaconBmaj", BEACON_MAJOR_2);
              returnIntent.putExtra("beaconBmin", BEACON_MAJOR_2);
              returnIntent.putExtra("beaconBdist", distance2);
              returnIntent.putExtra("beaconCmaj", BEACON_MAJOR_1);
              returnIntent.putExtra("beaconCmin", BEACON_MAJOR_1);
              returnIntent.putExtra("beaconCdist", distance1);
            }
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
          }
        });

    BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter btAdapter = btManager.getAdapter();
    if (btAdapter != null && !btAdapter.isEnabled()) {
      Intent enableIntent = new Intent(ACTION_REQUEST_ENABLE);
      startActivityForResult(enableIntent, 0);
    }

    // I made a thread so i can Thread.sleep
    // It sleeps 3 seconds before terminating the search
    Runnable find = new FindDevices(btAdapter);
    Thread t = new Thread(find);
    t.start();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    appState = (GlobalVar) getApplicationContext(); // 获得全局变量
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 设置成竖屏

    super.onCreate(savedInstanceState);
    // getActionBar().setTitle(R.string.title_devices);
    requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉title
    // mHandler = new Handler();

    // 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, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
      finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    appState.BluetoothAdapter = mBluetoothAdapter;

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
      Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    // 开启蓝牙
    mBluetoothAdapter.enable();

    mBLE_reciv = new BluetoothLeClassDianzichen(this);
    appState.mBLE_reciv_dianzichen = mBLE_reciv;
    // appState.init_BluetoothLeClassDianzichen();
    if (!mBLE_reciv.initialize()) {
      Log.e(TAG, "Unable to initialize Bluetooth");
      finish();
    }
    // 发现BLE终端的Service时回调
    mBLE_reciv.setOnServiceDiscoverListener(mOnServiceDiscover);
    // 收到BLE终端数据交互的事件
    mBLE_reciv.setOnDataAvailableListener(mOnDataAvailable);

    mBLE_send = new BluetoothLeClassDianzichen(this);
    appState.mBLE_send_dianzichen = mBLE_send;
    if (!appState.mBLE_send_dianzichen.initialize()) {
      Log.e(TAG, "Unable to initialize Bluetooth");
      finish();
    }
    // 发现BLE终端的Service时回调
    mBLE_send.setOnServiceDiscoverListener(mOnServiceDiscover);
    // 收到BLE终端数据交互的事件
    mBLE_send.setOnDataAvailableListener(mOnDataAvailable);
  }
 /**
  * Configures the Bluetooth adapter and stores a reference to it
  *
  * @param c the current context
  * @return <code>true</code> if initialization was successful. <code>false</code> otherwise.
  */
 public static boolean configureBluetoothAdapter(Context c) {
   // Initializes Bluetooth adapter.
   final BluetoothManager bluetoothManager =
       (BluetoothManager) c.getSystemService(Context.BLUETOOTH_SERVICE);
   _ibp._bluetoothAdapter = bluetoothManager.getAdapter();
   if (_ibp._bluetoothAdapter == null || !_ibp._bluetoothAdapter.isEnabled()) {
     return false;
   }
   return true;
 }
 private BluetoothAdapter getBluetoothAdapter() {
   if (bluetoothAdapter == null) {
     // Initializes Bluetooth adapter.
     final BluetoothManager bluetoothManager =
         (BluetoothManager)
             this.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
     bluetoothAdapter = bluetoothManager.getAdapter();
   }
   return bluetoothAdapter;
 }
Example #12
0
  public ScanHelper(Context context, ScanCallback callback) {
    this.context = context;

    this.callback = callback;

    BluetoothManager bluetoothManager =
        (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

    bluetoothAdapter = bluetoothManager.getAdapter();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /**
     * Check if the current mobile phone supports ble Bluetooth, if you do not support the exit
     * program
     */
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
      Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
      finish();
    }

    /**
     * Adapter Bluetooth, get a reference to the Bluetooth adapter (API), which must be above
     * android4.3 or above.
     */
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    /** Check whether the device supports Bluetooth */
    if (mBluetoothAdapter == null) {
      Toast.makeText(this, "Blue not supported", Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    // initialise list adapter
    mLeDeviceListAdapter = new LeDeviceListAdapter();

    mDevicesListView = (ListView) findViewById(R.id.devices_list_view);
    mDevicesListView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            onListItemClick(i);
          }
        });

    // pass the adapter to the list view
    mDevicesListView.setAdapter(mLeDeviceListAdapter);

    // create service intent, later used to start/stop the service
    intent = new Intent(this, BluetoothLeScanService.class);

    // devices with marshmallow and above needs location service on to perform BLE scanning
    if (Build.VERSION.SDK_INT < 23) {
      // start scanning service
      startService(intent);
    } else {
      // check and ask for enabling location sevice
      checkLocationPermission();
    }
  }
Example #14
0
  private void checkBluetooth(Context context) {
    final BluetoothManager bluetoothManager =
        (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
      Toast.makeText(context, R.string.bluetooth_is_supported, Toast.LENGTH_SHORT).show();
    }
  }
Example #15
0
 private void startScan() {
   Log.wtf(TAG, "Starting Scan");
   BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
   mBluetoothAdapter = manager.getAdapter();
   if (mConnectedGatt != null) {
     mConnectedGatt.close();
   }
   mDevices.clear();
   mBluetoothAdapter.startLeScan(this);
   setProgressBarIndeterminateVisibility(true);
   mHand.postDelayed(mStopRunnable, 5000);
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   mHandler = new Handler();
   if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
     Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();
     finish();
   }
   final BluetoothManager bluetoothManager =
       (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
   mBluetoothAdapter = bluetoothManager.getAdapter();
 }
 private void firstRunSetup() {
   BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
   bluetoothAdapter = manager.getAdapter();
   bluetoothAdapter.setName("Alexandra-f1e1ddd0");
   if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
     startActivity(discoverableIntent);
   }
   BluetoothChatService bluetoothChatService =
       new BluetoothChatService(getApplicationContext(), mHandler);
   bluetoothChatService.start();
 }
Example #18
0
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View InputFragmentView = inflater.inflate(R.layout.sensor, container, false);

    // Bluetooth in Android 4.3 is accessed via the BluetoothManager, rather than
    // the old static bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothManager manager =
        (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = manager.getAdapter();
    // TODO (jos | tariq) if bluetoothAdapter is null here, there's no need to continue with the
    // fragment.
    // There's a method enable_bluetooth later in the file. This should be consistent.

    // 3d Cube
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    if (savedInstanceState == null) {
      newFragment = new _3D_object();
      ft.replace(R.id.container_3D, newFragment);
      ft.commit();
    }

    // TextViews
    deviceInfoText = (TextView) InputFragmentView.findViewById(R.id.d_Info);
    timestamp = (TextView) InputFragmentView.findViewById(R.id.timestamp);
    packetnumber = (TextView) InputFragmentView.findViewById(R.id.packetnumber);
    accelData = (TextView) InputFragmentView.findViewById(R.id.accelData);
    batteryVoltage = (TextView) InputFragmentView.findViewById(R.id.batteryVoltage);
    gyroData = (TextView) InputFragmentView.findViewById(R.id.gyroData);

    arrow_nw = (ImageView) InputFragmentView.findViewById(R.id.arrow_nw);
    arrow_n = (ImageView) InputFragmentView.findViewById(R.id.arrow_n);
    arrow_ne = (ImageView) InputFragmentView.findViewById(R.id.arrow_ne);
    arrow_w = (ImageView) InputFragmentView.findViewById(R.id.arrow_w);
    arrow_center = (ImageView) InputFragmentView.findViewById(R.id.arrow_center);
    arrow_e = (ImageView) InputFragmentView.findViewById(R.id.arrow_e);
    arrow_sw = (ImageView) InputFragmentView.findViewById(R.id.arrow_sw);
    arrow_s = (ImageView) InputFragmentView.findViewById(R.id.arrow_s);
    arrow_se = (ImageView) InputFragmentView.findViewById(R.id.arrow_se);

    arrow_nw.setRotation(-45);
    arrow_ne.setRotation(45);
    arrow_w.setRotation(-90);
    arrow_e.setRotation(90);
    arrow_sw.setRotation(-135);
    arrow_s.setRotation(180);
    arrow_se.setRotation(135);

    return InputFragmentView;
  }
 private void connectToBLE() {
   BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
   bluetoothAdapter = manager.getAdapter();
   if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
     startActivity(discoverableIntent);
   }
   Log.i("BLE", "setting up");
   logEvent(CONFIGURATION_TAG, "setting up BLE");
   bluetoothAdapter = manager.getAdapter();
   stopScan();
   startScan();
 }
 @TargetApi(18)
 private BluetoothAdapter getBluetoothAdapter() {
   if (android.os.Build.VERSION.SDK_INT < 18) {
     Log.w(TAG, "Not supported prior to API 18.");
     return null;
   }
   if (bluetoothAdapter == null) {
     // Initializes Bluetooth adapter.
     final BluetoothManager bluetoothManager =
         (BluetoothManager)
             this.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
     bluetoothAdapter = bluetoothManager.getAdapter();
   }
   return bluetoothAdapter;
 }
Example #21
0
  /**
   * Initializes a reference to the local Bluetooth adapter.
   *
   * @return Return true if the initialization is successful.
   */
  public boolean initialize() {
    // For API level 18 and above, get a reference to BluetoothAdapter through
    // BluetoothManager.

    log27_a = new StringBuilder();
    log27_g = new StringBuilder();
    log27_q = new StringBuilder();
    log27_e = new StringBuilder();
    log27_h = new StringBuilder();

    log26 = new StringBuilder();
    log28 = new StringBuilder();
    log27 = new StringBuilder();

    if (mBluetoothManager == null) {
      mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
      if (mBluetoothManager == null) {
        Log.e(TAG, "Unable to initialize BluetoothManager.");
        return false;
      }
    }

    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
      Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
      return false;
    }

    return true;
  }
  @Override
  public void onCreate() {

    // 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, R.string.ble_not_supported, Toast.LENGTH_LONG).show();
      mBleSupported = false;
    }

    // Initializes a Bluetooth adapter. For API level 18 and above, get a
    // reference to BluetoothAdapter through BluetoothManager.
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBtAdapter = mBluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBtAdapter == null) {
      Toast.makeText(this, R.string.bt_not_supported, Toast.LENGTH_LONG).show();
      mBleSupported = false;
    }

    mFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, mFilter);

    if (!mBtAdapter.isEnabled()) {
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(enableIntent);
    }

    startBluetoothLeService();

    super.onCreate();
  }
  private void onServiceAdded() {
    final BluetoothLeAdvertiser bluetoothLeAdvertiser =
        mBluetoothManager.getAdapter().getBluetoothLeAdvertiser();
    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.setIncludeTxPowerLevel(false); // necessity to fit in 31 byte advertisement
    // dataBuilder.setManufacturerData(0, advertisingBytes);
    dataBuilder.addServiceUuid(new ParcelUuid(UUID.fromString(Constants.CHAT_SERVICE_UUID)));
    // dataBuilder.setServiceData(pUUID, new byte[]{});

    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    settingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
    settingsBuilder.setConnectable(true);

    bluetoothLeAdvertiser.startAdvertising(
        settingsBuilder.build(), dataBuilder.build(), mAdvertiseCallback);
    final Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(
        new Runnable() {
          @Override
          public void run() {
            bluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
          }
        },
        ADVERTISE_TIMEOUT);
  }
 /**
  * Turns bluetooth on if bluetooth is supported
  *
  * @param activity application activity
  */
 public void turnBluetoothOn(Activity activity) {
   checkActivity(activity);
   if (hasBluetooth(activity)) {
     BluetoothManager mBluetoothManager =
         (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
     BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
     if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
       Log.i(DEBUG_BLUETOOTH, "Bluetooth is turned off");
       Log.i(DEBUG_BLUETOOTH, "Launch request to enable bluetooth");
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
     }
     if (mBluetoothAdapter.isEnabled()) {
       Log.i(DEBUG_BLUETOOTH, "Bluetooth is enabled");
     }
   }
 }
  public void start(Context context) {

    mContext = context;

    mHandler = new Handler();

    final BluetoothManager bluetoothManager =
        (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    mBluetoothAdapter.cancelDiscovery();

    final Intent gattServiceIntent = new Intent(context, BluetoothLeService.class);
    context.bindService(gattServiceIntent, mServiceConnection, context.BIND_AUTO_CREATE);

    if (BluetoothLEUtils.hasBluetoothLE(context)) {
      registerReceivers();
    }
  }
Example #26
0
  public void createServer() {
    server = bluetoothManager.openGattServer(activity, new Callback());

    service =
        new BluetoothGattService(
            UUID.fromString(B4UUID), BluetoothGattService.SERVICE_TYPE_SECONDARY);
    server.addService(service);
    System.out.println("server: " + server);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // setTheme(R.style.AppThemeRed);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_part_one);

    initToolbar();
    mFabButton = (ImageButton) findViewById(R.id.fabButton);
    mFabButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (!isRunning) {
              scanLeDevice(true);
              mFabButton.setImageResource(R.drawable.ic_not_interested_white_24dp);
              isRunning = true;
            } else {
              scanLeDevice(false);
              mFabButton.setImageResource(R.drawable.ic_autorenew_white_24dp);
              isRunning = false;
            }
          }
        });

    mHandler = new Handler();
    // 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, "不支持BLE", Toast.LENGTH_SHORT).show();
      finish();
    }
    final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
      Toast.makeText(this, "不支持BLE", Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    // initRecyclerView();
  }
Example #28
0
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_scan:
       BluetoothManager bluetooth_manager =
           (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
       Toast.makeText(this, "Scanning", Toast.LENGTH_LONG).show();
       if (bluetooth_manager == null) {
         Toast.makeText(this, "This device does not seem to support bluetooth", Toast.LENGTH_LONG)
             .show();
         return true;
       } else {
         if (!bluetooth_manager.getAdapter().isEnabled()) {
           Toast.makeText(
                   this, "Bluetooth is turned off on this device currently", Toast.LENGTH_LONG)
               .show();
           return true;
         } else {
           if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
             Toast.makeText(
                     this,
                     "The android version of this device is not compatible with Bluetooth Low Energy",
                     Toast.LENGTH_LONG)
                 .show();
             return true;
           }
         }
       }
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         scanLeDeviceLollipop(true);
       } else {
         scanLeDevice(true);
       }
       return true;
       //            case R.id.menu_stop:
       //                Intent tableIntent = new Intent(this, RawDataTable.class);
       //                startActivity(tableIntent);
       //                return true;
     default:
       return super.onOptionsItemSelected(item);
   }
 }
  private void InitBLE() {
    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) == false) {
      Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
      finish();
    }

    final BluetoothManager mBluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();

    if (mBluetoothAdapter == null) {
      Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
      finish();
    }

    if (mBluetoothAdapter.isEnabled() == false) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
  }
  boolean initiate() {
    // Use this check to determine whether BLE is supported on the device.
    // Then you can
    // selectively disable BLE-related features.
    if (!mainContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
      return false;
    }

    // Initializes a Bluetooth adapter. For API level 18 and above, get a
    // reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
        (BluetoothManager) mainContext.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
      return false;
    }
    return true;
  }