Google I/O 2014 - What's new in Android
Jun 26, 2014
Official Moto 360 Demo at Google I/O
Meet Moto 360, a classic timepiece powered by Android Wear. Get a sneak peek at just some of the functionalities of Moto 360 in demo we are giving at this year's Google I/O.
Jun 22, 2014
Implement BroadcastReceiver to monitor Bluetooth state changed
Last example show how to Enable Bluetooth using Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE. In this example, we implement a new class BTStateChangedBroadcastReceiver, extends BroadcastReceiver, to monitor Bluetooth state changed.
BTStateChangedBroadcastReceiver.java
Call registerReceiver(...) to register new BTStateChangedBroadcastReceiver(), to the intent of BluetoothAdapter.ACTION_STATE_CHANGED.
activity_main.xml
Permission of "android.permission.BLUETOOTH" is needed in AndroidManifest.xml.
BTStateChangedBroadcastReceiver.java
package com.example.androidbluetooth; import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class BTStateChangedBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); switch(state){ case BluetoothAdapter.STATE_CONNECTED: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_CONNECTED", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_CONNECTING: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_CONNECTING", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_DISCONNECTED: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_DISCONNECTED", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_DISCONNECTING: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_DISCONNECTING", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_OFF: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_OFF", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_ON: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_ON", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_TURNING_OFF: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_TURNING_OFF", Toast.LENGTH_SHORT).show(); break; case BluetoothAdapter.STATE_TURNING_ON: Toast.makeText(context, "BTStateChangedBroadcastReceiver: STATE_TURNING_ON", Toast.LENGTH_SHORT).show(); break; } } }
Call registerReceiver(...) to register new BTStateChangedBroadcastReceiver(), to the intent of BluetoothAdapter.ACTION_STATE_CHANGED.
package com.example.androidbluetooth; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView textInfo; Button buttonEnableBT; BluetoothAdapter bluetoothAdapter; final static int REQUEST_ENABLE_BT = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textInfo = (TextView)findViewById(R.id.info); buttonEnableBT = (Button)findViewById(R.id.enablebt); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { textInfo.setText("BlueTooth not supported in this device"); buttonEnableBT.setEnabled(false); }else{ if (bluetoothAdapter.isEnabled()) { buttonEnableBT.setEnabled(false); textInfo.setText("BlueTooth enabled"); }else{ buttonEnableBT.setEnabled(true); textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth."); } //register BroadcastReceiver registerReceiver(new BTStateChangedBroadcastReceiver(), new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } buttonEnableBT.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_ENABLE_BT){ if(resultCode==RESULT_OK){ Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show(); } } if (bluetoothAdapter.isEnabled()) { buttonEnableBT.setEnabled(false); textInfo.setText("BlueTooth enabled"); }else{ buttonEnableBT.setEnabled(true); textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth."); } } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidbluetooth.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/enablebt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enable BlueTooth" /> </LinearLayout>
Permission of "android.permission.BLUETOOTH" is needed in AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH"/>
Jun 18, 2014
Check BlueTooth status, and enable it using Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE
This example check if Bluetooth supported in running device and its ON/OFF status. Then call startActivityForResult() with Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE, when user click button to enable it. Then re-check the status again in onActivityResult() when result returned.
MainActivity.java
activity_main.xml
Add <uses-permission android:name="android.permission.BLUETOOTH"/> in AndroidManifest.xml.
MainActivity.java
package com.example.androidbluetooth; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView textInfo; Button buttonEnableBT; BluetoothAdapter bluetoothAdapter; final static int REQUEST_ENABLE_BT = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textInfo = (TextView)findViewById(R.id.info); buttonEnableBT = (Button)findViewById(R.id.enablebt); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { textInfo.setText("BlueTooth not supported in this device"); buttonEnableBT.setEnabled(false); }else{ if (bluetoothAdapter.isEnabled()) { buttonEnableBT.setEnabled(false); textInfo.setText("BlueTooth enabled"); }else{ buttonEnableBT.setEnabled(true); textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth."); } } buttonEnableBT.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_ENABLE_BT){ if(resultCode==RESULT_OK){ Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show(); } } if (bluetoothAdapter.isEnabled()) { buttonEnableBT.setEnabled(false); textInfo.setText("BlueTooth enabled"); }else{ buttonEnableBT.setEnabled(true); textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth."); } } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidbluetooth.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/enablebt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Enable BlueTooth" /> </LinearLayout>
Add <uses-permission android:name="android.permission.BLUETOOTH"/> in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidbluetooth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.BLUETOOTH"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.androidbluetooth.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Jun 6, 2014
Subscribe to:
Posts (Atom)