Bluetooth:
The Android platform includes support for the Bluetooth network stack,which allows a device to wirelessly excahnge data with other Bluetooth devices.The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. these APIs let applications wirelessly connect to other Bluetooth devices,APIs let applications wirelessly connect to other Bluetooth devices,enabling point-to-point and multpoint wireless features.
Using Bluetooth APIs, an Android application can perform the following:
scan for the Bluetooth devices.
The Android platform includes support for the Bluetooth network stack,which allows a device to wirelessly excahnge data with other Bluetooth devices.The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. these APIs let applications wirelessly connect to other Bluetooth devices,APIs let applications wirelessly connect to other Bluetooth devices,enabling point-to-point and multpoint wireless features.
Using Bluetooth APIs, an Android application can perform the following:
scan for the Bluetooth devices.
- Query the local Bluetooth adapter for paired Bluetooth devices.
- Establish RFCOMM channels.
- connect other devices through service discovery.
- Transfer data to and from other devices.
- Manage multiple connections.
package com.codingonandroid;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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;
public class BluetoothDemoActivity extends Activity {
Button b1,b2;
TextView tv;
String s1,s2;
BluetoothAdapter badapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
tv=(TextView)findViewById(R.id.textView1);
badapter=BluetoothAdapter.getDefaultAdapter();
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(badapter==null){
tv.append("no device");
}
tv.append("searching for device.....");
badapter.startDiscovery();
Set<BluetoothDevice> bsd=badapter.getBondedDevices();
for(BluetoothDevice device:bsd){
/*s1=badapter.getAddress();
s2=badapter.getName();*/
String add=device.getAddress();
String name=device.getName();
int i=device.getBondState();
tv.append(add+" "+name+"state"+i);
}
}
});
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(badapter.isEnabled()){
s1=badapter.getAddress();
s2=badapter.getName();
}
else{
String bad=BluetoothAdapter.ACTION_REQUEST_ENABLE;
startActivityForResult(new Intent(bad), 0);
}
tv.append(s1+" "+s2);
}
});
}
}
No comments:
Post a Comment