安卓蓝牙入门二-------整合版本
2016-01-24 14:22
627 查看
本人目前刚接触蓝牙不久,由于自己摸索走了很多弯路,现在摸索出来了一点东西,希望写下来对大家,特别是新手有点帮助
废话不多说直接上代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/on"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="on"
android:text="Turn On" />
<Button
android:id="@+id/visible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="visible"
android:text="Get Visible" />
<Button
android:id="@+id/getband"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="getBand"
android:text="List getBand" />
<Button
android:id="@+id/off"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="off"
android:text="Turn Off" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9999FF">
</ListView>
<TextView
android:id="@+id/Tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
package com.example.blus;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener {
private TextView TV;
private ListView LV;
private BluetoothDevice BD;
private Set<BluetoothDevice>pairedDevices;
private BluetoothAdapter adapter;
private BluetoothDevice device;
private ArrayList<String> list=new ArrayList<String>();
private ArrayAdapter<String> ad=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xx);
inift();
adapter=BluetoothAdapter.getDefaultAdapter();//获得本地蓝牙适配器
}
public void inift() {
TV=(TextView)findViewById(R.id.Tv);
LV=(ListView)findViewById(R.id.listView1);
}
////////////////////开启蓝牙设备//////////////////////////////////////////////////////////
public void on(View view){
setTitle("本机蓝牙地址:" + adapter.getAddress());
if (!adapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(MainActivity.this,"Turned on"
,Toast.LENGTH_LONG).show();
}else{Toast.makeText(MainActivity.this,"Already on",
Toast.LENGTH_LONG).show();}
}
//////////////////////得到绑定的蓝牙设备////////////////////////////////////////////////
public void getBand(View view){
bd();
if(!adapter.isEnabled()){Toast.makeText(getApplicationContext(), "蓝牙设备没有开启", 6).show();}
else{
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("已经绑定的蓝牙设备")
.setIcon(R.drawable.ic_launcher)
.setMessage(TV.getText())
.setPositiveButton("确定", this)
.setNeutralButton("退出",this)
.create();
dialog.show();
TV.setText("");
}
}
public void bd(){
pairedDevices = adapter.getBondedDevices();
for(BluetoothDevice bt : pairedDevices)
TV.setText(bt.getName()+" : "+bt.getAddress()+"\n");
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////蓝牙收索//////////////////////////////////////////
public class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String str=null;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// TV.setText(TV.getText()+device.getName()+" : "+device.getAddress()+"\n");
// list.add(device.getName()+" : "+device.getAddress());
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
str ="已配对|"+ device.getName() + "|"
+ device.getAddress();
}
if(device.getBondState() == BluetoothDevice.BOND_NONE){
str ="未配对|"+ device.getName() + "|"
+ device.getAddress();
}
if(list.indexOf(str)==-1){//防止出现相同的
list.add(str);
}
ad=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
LV.setAdapter(ad);
}
}
public void visible(View view){
//1.注册一个广播,用于接收“发现设备”的广播
//.list.removeAll(list);
list.clear();
if(adapter.isEnabled()){
IntentFilter intentFilter =new IntentFilter(device.ACTION_FOUND);
BluetoothReceiver receiver = new BluetoothReceiver();
registerReceiver(receiver, intentFilter);
adapter.startDiscovery();
Toast.makeText(getApplicationContext(), "开始收索蓝牙设备", 6).show();
}else{Toast.makeText(getApplicationContext(), "蓝牙设备没有打开", 6).show();}
//BluetoothReceiver receiver = new BluetoothReceiver();
// if(adapter.isEnabled()){
// }
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////蓝牙关闭/////////////////////////////////////////////
public void off(View view){
LV.setAdapter(null);
list.clear();
if(adapter.isEnabled()){
adapter.disable();
Toast.makeText(getApplicationContext(), "蓝牙关闭中。。", 6).show();
TV.setText("");
}
}
////////////////////////////////////////////////////////////////////////////////
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
}
废话不多说直接上代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/on"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="on"
android:text="Turn On" />
<Button
android:id="@+id/visible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="visible"
android:text="Get Visible" />
<Button
android:id="@+id/getband"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="getBand"
android:text="List getBand" />
<Button
android:id="@+id/off"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="off"
android:text="Turn Off" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9999FF">
</ListView>
<TextView
android:id="@+id/Tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
package com.example.blus;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener {
private TextView TV;
private ListView LV;
private BluetoothDevice BD;
private Set<BluetoothDevice>pairedDevices;
private BluetoothAdapter adapter;
private BluetoothDevice device;
private ArrayList<String> list=new ArrayList<String>();
private ArrayAdapter<String> ad=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xx);
inift();
adapter=BluetoothAdapter.getDefaultAdapter();//获得本地蓝牙适配器
}
public void inift() {
TV=(TextView)findViewById(R.id.Tv);
LV=(ListView)findViewById(R.id.listView1);
}
////////////////////开启蓝牙设备//////////////////////////////////////////////////////////
public void on(View view){
setTitle("本机蓝牙地址:" + adapter.getAddress());
if (!adapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(MainActivity.this,"Turned on"
,Toast.LENGTH_LONG).show();
}else{Toast.makeText(MainActivity.this,"Already on",
Toast.LENGTH_LONG).show();}
}
//////////////////////得到绑定的蓝牙设备////////////////////////////////////////////////
public void getBand(View view){
bd();
if(!adapter.isEnabled()){Toast.makeText(getApplicationContext(), "蓝牙设备没有开启", 6).show();}
else{
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("已经绑定的蓝牙设备")
.setIcon(R.drawable.ic_launcher)
.setMessage(TV.getText())
.setPositiveButton("确定", this)
.setNeutralButton("退出",this)
.create();
dialog.show();
TV.setText("");
}
}
public void bd(){
pairedDevices = adapter.getBondedDevices();
for(BluetoothDevice bt : pairedDevices)
TV.setText(bt.getName()+" : "+bt.getAddress()+"\n");
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////蓝牙收索//////////////////////////////////////////
public class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String str=null;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// TV.setText(TV.getText()+device.getName()+" : "+device.getAddress()+"\n");
// list.add(device.getName()+" : "+device.getAddress());
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
str ="已配对|"+ device.getName() + "|"
+ device.getAddress();
}
if(device.getBondState() == BluetoothDevice.BOND_NONE){
str ="未配对|"+ device.getName() + "|"
+ device.getAddress();
}
if(list.indexOf(str)==-1){//防止出现相同的
list.add(str);
}
ad=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
LV.setAdapter(ad);
}
}
public void visible(View view){
//1.注册一个广播,用于接收“发现设备”的广播
//.list.removeAll(list);
list.clear();
if(adapter.isEnabled()){
IntentFilter intentFilter =new IntentFilter(device.ACTION_FOUND);
BluetoothReceiver receiver = new BluetoothReceiver();
registerReceiver(receiver, intentFilter);
adapter.startDiscovery();
Toast.makeText(getApplicationContext(), "开始收索蓝牙设备", 6).show();
}else{Toast.makeText(getApplicationContext(), "蓝牙设备没有打开", 6).show();}
//BluetoothReceiver receiver = new BluetoothReceiver();
// if(adapter.isEnabled()){
// }
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////蓝牙关闭/////////////////////////////////////////////
public void off(View view){
LV.setAdapter(null);
list.clear();
if(adapter.isEnabled()){
adapter.disable();
Toast.makeText(getApplicationContext(), "蓝牙关闭中。。", 6).show();
TV.setText("");
}
}
////////////////////////////////////////////////////////////////////////////////
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories