Activity_blue.xml
<RelativeLayout 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"
tools:context="com.example.bluetooth.BlueActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BlueTooth Example" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="Scan" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="88dp"
android:layout_marginTop="56dp"
/>
</RelativeLayout>
BlueActivity.java
package com.example.bluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class BlueActivity extends Activity {
BluetoothAdapter ba;
BroadcastReceiver br;
IntentFilter ifs;
TextView tv;
BluetoothDevice bd;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue);
tv=(TextView) findViewById(R.id.textView2);
ba=BluetoothAdapter.getDefaultAdapter();
if(ba==null){
tv.setText("Bluetooth is not supported by this device");
}
br=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
tv.setText("Discovery was Started");
}
else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
tv.setText("Discovery hasfinished");
}
else if(action.equals(BluetoothDevice.ACTION_FOUND)){
msg=tv.getText().toString();
tv.setText("Bluetooth found");
bd=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
msg+="\n"+bd.getName()+"...."+bd.getAddress();
tv.setText(msg);
}
}
};
ifs=new IntentFilter();
ifs.addAction(ba.ACTION_DISCOVERY_STARTED);
ifs.addAction(ba.ACTION_DISCOVERY_FINISHED);
ifs.addAction(bd.ACTION_FOUND);
registerReceiver(br, ifs);
}
public void scanButton(View v){
Intent i=new Intent();
i.setAction(ba.ACTION_REQUEST_DISCOVERABLE);
startActivity(i);
ba.startDiscovery();
}
protected void onDestroy(){
super.onDestroy();
unregisterReceiver(br);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blue, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<RelativeLayout 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"
tools:context="com.example.bluetooth.BlueActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BlueTooth Example" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="Scan" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="88dp"
android:layout_marginTop="56dp"
/>
</RelativeLayout>
BlueActivity.java
package com.example.bluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class BlueActivity extends Activity {
BluetoothAdapter ba;
BroadcastReceiver br;
IntentFilter ifs;
TextView tv;
BluetoothDevice bd;
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue);
tv=(TextView) findViewById(R.id.textView2);
ba=BluetoothAdapter.getDefaultAdapter();
if(ba==null){
tv.setText("Bluetooth is not supported by this device");
}
br=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){
tv.setText("Discovery was Started");
}
else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
tv.setText("Discovery hasfinished");
}
else if(action.equals(BluetoothDevice.ACTION_FOUND)){
msg=tv.getText().toString();
tv.setText("Bluetooth found");
bd=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
msg+="\n"+bd.getName()+"...."+bd.getAddress();
tv.setText(msg);
}
}
};
ifs=new IntentFilter();
ifs.addAction(ba.ACTION_DISCOVERY_STARTED);
ifs.addAction(ba.ACTION_DISCOVERY_FINISHED);
ifs.addAction(bd.ACTION_FOUND);
registerReceiver(br, ifs);
}
public void scanButton(View v){
Intent i=new Intent();
i.setAction(ba.ACTION_REQUEST_DISCOVERABLE);
startActivity(i);
ba.startDiscovery();
}
protected void onDestroy(){
super.onDestroy();
unregisterReceiver(br);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blue, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".BlueActivity"
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