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>
No comments:
Post a Comment