阅读更多 MainActivity.java
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 devices=new ArrayList();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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(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() {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString 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
        

AndroidManifest.xml
        
  • com.blueTooth.rar (688.3 KB)
  • 下载次数: 0

更多相关文章

  1. 开篇
  2. Android(安卓)获取设备各种信息以及其它
  3. qt部署到android设备,很久没响应解决办法
  4. Notes on the implementation of encryption in Android(安卓)3.
  5. Android(安卓)获取设备各种信息以及其它
  6. 优雅的处理Android数据库升级的问题
  7. Android(安卓)Usb源码分析
  8. JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器
  9. android adb常用命令整理

随机推荐

  1. java7 switch语句使用字符串
  2. 如何在Spring中读取具有相同键的多个属性
  3. java中的异常处理
  4. java中两个事物,怎么设置,能让一个事物出现
  5. 基于内容估计文本宽度的算法
  6. JavaScript中的map()函数
  7. Java面试题--基础知识部分
  8. Hbase1.1.x Java版之批量查删操作
  9. 【Servlet】使用org.eclipse.jetty实现小
  10. java中的成员变量和局部变量的区别