• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
笨笨笨笨
博客园    首页    新随笔    联系   管理    订阅  订阅

Android -蓝牙

package com.xuben.bluetoothtest1;


import android.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import java.util.Iterator;
import java.util.Set;

import android.widget.Button;

public class Bluetooth_test1Activity extends Activity {
    
/** Called when the activity is first created. */
    
private Button button = null;
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button 
= (Button)findViewById(R.id.scanButtonId);
        button.setOnClickListener(
new ButtonListener());
    }
    
    
private class ButtonListener implements OnClickListener{
        
public void onclick(View v){
            BluetoothAdapter adapter 
= BluetoothAdapter.getDefaultAdapter();
            
if(adapter != null){
                System.out.println(
"Has bluetooth device");
                
if(adapter.isEnabled()){
                    Intent intent 
= new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    startActivity(intent);
                }
                Set
<BluetoothDevice> devices = adapter.getBondedDevices();
                
if(devices.size() > 0){
                    
for(Iterator iterator = devices.iterator(); iterator.hasNext();){
                        BluetoothDevice bluetoothDevice 
= (BluetoothDevice)iterator.next();
                        System.out.println(bluetoothDevice.getAddress());
                    }            
                }
            }
            
else{
                System.out.println(
"No bluetooth device");
            }
        }

        @Override
        
public void onClick(DialogInterface dialog, int which) {
            
// TODO Auto-generated method stub
            
        }
        
    }
}
posted @ 2011-07-13 17:29  笨笨笨笨  阅读(191)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3