MainActivity.java
activity_main.xml
AndroidManifest.xml
package com.blueTooth;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
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.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.blueTooth.R.id;
public class MainActivity extends Activity {
List<BluetoothDevice> devices=new ArrayList<BluetoothDevice>();
BluetoothDevice device;
BluetoothAdapter adapter;
private CheckBox checkBox;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox=(CheckBox) findViewById(R.id.checkBox1);
textView=(TextView) findViewById(id.textView1);
adapter=BluetoothAdapter.getDefaultAdapter();
if(adapter==null){
checkBox.setEnabled(false);
checkBox.setChecked(false);
textView.setText("本地无蓝牙设备");
}else{
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==false){
adapter.disable();
textView.setText("未打开蓝牙");
}else{
adapter.enable();
IntentFilter findintent=new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(finreciver,findintent);
adapter.startDiscovery();
if(devices.size()>0){
textView.setText("列表有数据");
}else{
textView.setText("未发现远程蓝牙设备(列表为空)");
}
}
}
});
}
}
private BroadcastReceiver finreciver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
Toast.makeText(MainActivity.this, "搜索完成",Toast.LENGTH_SHORT).show();
System.out.println("搜索完成");
}
}
};
}
activity_main.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" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:text="CheckBox" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text="TextView" />
</RelativeLayout>
AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>