来自http://blog.csdn.net/zhangnianxiang/article/details/6655431
扫描周围蓝牙设备的demo
老规矩。右键取得图片地址,利用下载工具下载这个显示不出来的图片。后缀改为rar即可得到源代码项目。
- package zhang.bluetooth;
- import java.util.Iterator;
- 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.widget.Button;
- public class BlueTooth extends Activity {
- private Button button;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button=(Button)findViewById(R.id.button);
- button.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- //得到bluetoothAdapter对象
- BluetoothAdapter adapter= BluetoothAdapter.getDefaultAdapter();
- //判断BluetoothAdapter师傅哦为空,如果为空,则表明没有蓝牙设备
- if(adapter!=null){
- System.out.println("本机拥有BlueTooth");
- //调用isEnable方法,判断当前蓝牙是否可用
- if(!adapter.isEnabled()){
- //创建一个intent对象,。该对象用于启动蓝牙
- Intent intent = new Intent (BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivity(intent);
- }
- //得到已经配对的蓝牙适配器对象
- Set<BluetoothDevice> devices = adapter.getBondedDevices();
- if(devices.size()>0){
- for(Iterator<BluetoothDevice> iterator =devices.iterator();iterator.hasNext();){
- BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();
- //打印远程蓝牙设备的地址
- System.out.println(bluetoothDevice.getAddress());}
- }
- }else{
- System.out.println("没有蓝牙设备");
- }
- }
- });
- }
- }
布局文件就不给了,就一个button
记得配置
- <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>