List of usage examples for android.bluetooth BluetoothManager getAdapter
public BluetoothAdapter getAdapter()
From source file:io.puzzlebox.bloom.ui.BloomFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_bloom, container, false); // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // setContentView(R.layout.main); // getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); progressBarAttention = (ProgressBar) v.findViewById(R.id.progressBarAttention); final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }; ShapeDrawable progressBarAttentionDrawable = new ShapeDrawable( new RoundRectShape(roundedCorners, null, null)); String progressBarAttentionColor = "#FF0000"; progressBarAttentionDrawable.getPaint().setColor(Color.parseColor(progressBarAttentionColor)); ClipDrawable progressAttention = new ClipDrawable(progressBarAttentionDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);/*from w ww . j a va2 s . co m*/ progressBarAttention.setProgressDrawable(progressAttention); progressBarAttention .setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); progressBarMeditation = (ProgressBar) v.findViewById(R.id.progressBarMeditation); ShapeDrawable progressBarMeditationDrawable = new ShapeDrawable( new RoundRectShape(roundedCorners, null, null)); String progressBarMeditationColor = "#0000FF"; progressBarMeditationDrawable.getPaint().setColor(Color.parseColor(progressBarMeditationColor)); ClipDrawable progressMeditation = new ClipDrawable(progressBarMeditationDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); progressBarMeditation.setProgressDrawable(progressMeditation); progressBarMeditation .setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); progressBarSignal = (ProgressBar) v.findViewById(R.id.progressBarSignal); ShapeDrawable progressBarSignalDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null)); String progressBarSignalColor = "#00FF00"; progressBarSignalDrawable.getPaint().setColor(Color.parseColor(progressBarSignalColor)); ClipDrawable progressSignal = new ClipDrawable(progressBarSignalDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); progressBarSignal.setProgressDrawable(progressSignal); progressBarSignal.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); // progressBarSignal.setProgress(tgSignal); progressBarPower = (ProgressBar) v.findViewById(R.id.progressBarPower); ShapeDrawable progressBarPowerDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null)); String progressBarPowerColor = "#FFFF00"; progressBarPowerDrawable.getPaint().setColor(Color.parseColor(progressBarPowerColor)); ClipDrawable progressPower = new ClipDrawable(progressBarPowerDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); progressBarPower.setProgressDrawable(progressPower); progressBarPower.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); // progressBarRange = (ProgressBar) v.findViewById(R.id.progressBarRange); //// ShapeDrawable progressBarRangeDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); // ShapeDrawable progressBarRangeDrawable = new ShapeDrawable(); //// String progressBarRangeColor = "#FF00FF"; // String progressBarRangeColor = "#990099"; // progressBarRangeDrawable.getPaint().setColor(Color.parseColor(progressBarRangeColor)); // ClipDrawable progressRange = new ClipDrawable(progressBarRangeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); // progressBarRange.setProgressDrawable(progressRange); // progressBarRange.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); // //// progressBarRange.setMax(128 + 127); // progressBarRange.setMax(bloomRangeMax); // progressBarBloom = (ProgressBar) v.findViewById(R.id.progressBarBloom); // ShapeDrawable progressBarBloomDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null)); // String progressBarBloomColor = "#7F0000"; // progressBarBloomDrawable.getPaint().setColor(Color.parseColor(progressBarBloomColor)); // ClipDrawable progressBloom = new ClipDrawable(progressBarBloomDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); // progressBarBloom.setProgressDrawable(progressBloom); // progressBarBloom.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); seekBarAttention = (SeekBar) v.findViewById(R.id.seekBarAttention); seekBarAttention.setOnSeekBarChangeListener(this); seekBarMeditation = (SeekBar) v.findViewById(R.id.seekBarMeditation); seekBarMeditation.setOnSeekBarChangeListener(this); // imageViewStatus = (ImageView) v.findViewById(R.id.imageViewStatus); servoSeekBar = (SeekBar) v.findViewById(R.id.ServoSeekBar); servoSeekBar.setEnabled(false); // servoSeekBar.setMax(180); servoSeekBar.setMax(100); servoSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { byte[] buf = new byte[] { (byte) 0x03, (byte) 0x00, (byte) 0x00 }; buf[1] = (byte) servoSeekBar.getProgress(); BloomSingleton.getInstance().characteristicTx.setValue(buf); BloomSingleton.getInstance().mBluetoothLeService .writeCharacteristic(BloomSingleton.getInstance().characteristicTx); } }); // rssiValue = (TextView) v.findViewById(R.id.rssiValue); connectBloom = (Button) v.findViewById(R.id.connectBloom); connectBloom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if (!BloomSingleton.getInstance().scanFlag) { scanLeDevice(); Timer mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { if ((BloomSingleton.getInstance().mDevice != null) && (BloomSingleton.getInstance().mDevice.getAddress() != null) && (BloomSingleton.getInstance().mBluetoothLeService != null)) { BloomSingleton .getInstance().mDeviceAddress = BloomSingleton.getInstance().mDevice .getAddress(); if (BloomSingleton.getInstance().mDeviceAddress != null) BloomSingleton.getInstance().mBluetoothLeService .connect(BloomSingleton.getInstance().mDeviceAddress); else { Toast toast = Toast.makeText(getActivity(), "Error connecting to Puzzlebox Bloom", Toast.LENGTH_SHORT); toast.setGravity(0, 0, Gravity.CENTER | Gravity.BOTTOM); toast.show(); } BloomSingleton.getInstance().scanFlag = true; } else { getActivity().runOnUiThread(new Runnable() { public void run() { Toast toast = Toast.makeText(getActivity(), "Error connecting to Puzzlebox Bloom", Toast.LENGTH_SHORT); toast.setGravity(0, 0, Gravity.CENTER | Gravity.BOTTOM); toast.show(); } }); } } }, BloomSingleton.getInstance().SCAN_PERIOD); } } catch (Exception e) { Log.e(TAG, "Exception connecting to Bloom: " + e); Toast toast = Toast.makeText(getActivity(), "Exception connecting to Puzzlebox Bloom", Toast.LENGTH_SHORT); toast.setGravity(0, 0, Gravity.CENTER | Gravity.BOTTOM); toast.show(); } System.out.println(BloomSingleton.getInstance().connState); // Log.e(TAG, connState); // if (connState == false) { if (!BloomSingleton.getInstance().connState && BloomSingleton.getInstance().mDeviceAddress != null) { BloomSingleton.getInstance().mBluetoothLeService .connect(BloomSingleton.getInstance().mDeviceAddress); } else { if (BloomSingleton.getInstance().mBluetoothLeService != null) { setBloomRGBOff(); BloomSingleton.getInstance().mBluetoothLeService.disconnect(); BloomSingleton.getInstance().mBluetoothLeService.close(); setButtonDisable(); } } } }); // Button buttonOpen = (Button) v.findViewById(R.id.buttonOpen); // buttonOpen.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // byte[] buf = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 }; // BloomSingleton.getInstance().characteristicTx.setValue(buf); // BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // } // }); // buttonOpen.setVisibility(View.GONE); // // Button buttonClose = (Button) v.findViewById(R.id.buttonClose); // buttonClose.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // byte[] buf = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00 }; // BloomSingleton.getInstance().characteristicTx.setValue(buf); // BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // } // }); // buttonClose.setVisibility(View.GONE); // buttonDemo = (Button) v.findViewById(R.id.buttonDemo); // buttonDemo.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // byte[] buf; //// if (! BloomSingleton.getInstance().demoActive) { // BloomSingleton.getInstance().demoActive = true; // // // bloomOpen() //// buf = new byte[]{(byte) 0x01, (byte) 0x00, (byte) 0x00}; //// BloomSingleton.getInstance().characteristicTx.setValue(buf); //// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // // // loopRGB() // buf = new byte[]{(byte) 0x06, (byte) 0x00, (byte) 0x00}; // BloomSingleton.getInstance().characteristicTx.setValue(buf); // BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // // // Set Red to 0 // buf = new byte[]{(byte) 0x0A, (byte) 0x00, (byte) 0x00}; // R = 0 // BloomSingleton.getInstance().characteristicTx.setValue(buf); // BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // // // bloomClose() //// buf = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00}; //// BloomSingleton.getInstance().characteristicTx.setValue(buf); //// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); // // //// } else { //// BloomSingleton.getInstance().demoActive = false; ////// buf = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00}; ////// BloomSingleton.getInstance().characteristicTx.setValue(buf); ////// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); //// buf = new byte[]{(byte) 0x0A, (byte) 0x00, (byte) 0x00}; // R = 0 //// BloomSingleton.getInstance().characteristicTx.setValue(buf); //// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); ////// buf = new byte[]{(byte) 0x0A, (byte) 0x01, (byte) 0x00}; // G = 0 ////// BloomSingleton.getInstance().characteristicTx.setValue(buf); ////// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); ////// buf = new byte[]{(byte) 0x0A, (byte) 0x02, (byte) 0x00}; // B = 0 ////// BloomSingleton.getInstance().characteristicTx.setValue(buf); ////// BloomSingleton.getInstance().mBluetoothLeService.writeCharacteristic(BloomSingleton.getInstance().characteristicTx); //// } // } // }); if (!getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(getActivity(), "Bluetooth LE not supported", Toast.LENGTH_SHORT).show(); getActivity().finish(); } final BluetoothManager mBluetoothManager = (BluetoothManager) getActivity() .getSystemService(Context.BLUETOOTH_SERVICE); BloomSingleton.getInstance().mBluetoothAdapter = mBluetoothManager.getAdapter(); if (BloomSingleton.getInstance().mBluetoothAdapter == null) { Toast.makeText(getActivity(), "Bluetooth LE not supported", Toast.LENGTH_SHORT).show(); getActivity().finish(); return v; } Intent gattServiceIntent = new Intent(getActivity(), RBLService.class); // bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE); getActivity().bindService(gattServiceIntent, mServiceConnection, getActivity().BIND_AUTO_CREATE); /** * Update settings according to default UI */ updateScreenLayout(); // updatePowerThresholds(); // updatePower(); if (BloomSingleton.getInstance().connState) setButtonEnable(); return v; }