Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9150983
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: LINUX

2012-07-19 18:57:23

//开启蓝牙功能
            myButton = (Button)findViewById(R.id.Button_1);
        myButton.setOnClickListener(new OnClickListener() {
                        
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                //得到BluetoothAdapter对象
                                BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
                                //判断BluetoothAdapter对象是否为空,为空,则表明本机没有蓝牙设备
                                if(null != adapter){
                                        System.out.println("本机拥有蓝牙设备");
                                        //判断蓝牙是否可用
                                        if(!adapter.isEnabled()){
                                                //创建一个intent对象,感对象用于启动一个Activity,提示用户开启蓝牙设备
                                                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                startActivity(intent);
                                        }
                                        //迭代,得到所有已经配对的蓝牙适配器对象, 本机已保存的配对信息。
                                        Set devices = adapter.getBondedDevices();
                                        if(devices.size() > 0){
                                                for(Iterator iterator = devices.iterator(); iterator.hasNext(); ){
                                                        BluetoothDevice bluetoothDevice = (BluetoothDevice)iterator.next();
                                                        System.out.println(bluetoothDevice.getAddress()+ "----" +bluetoothDevice.getName());
                                                }
                                        }
                                }else{
                                        System.out.println("本机没有蓝牙设备");
                                }
                        }
                });
        
        
        //设置蓝牙可见
        myButton2 = (Button)findViewById(R.id.Button_2);
        myButton2.setOnClickListener(new OnClickListener() {
                        
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                //创建Intent对象,并将其Action的值设置为
                                Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                            //讲一个键值对对方到Intent对象当中,用于指定可见状态的持续时间
                                discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
                            startActivity(discoverableIntent);
                            
                        }
                });
        
        

        
        myButton3 = (Button)findViewById(R.id.Button_3);
        myButton3.setOnClickListener(new OnClickListener() {        
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                
                                str = "";
                                //广播监听
                        IntentFilter intentfilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        bluetoothReceiver = new BluetoothReceiver();
                        registerReceiver(bluetoothReceiver, intentfilter);

                                BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
                                adapter.startDiscovery();
                                
                        }
                });
        
        myButton4 = (Button)this.findViewById(R.id.Button_4);
        myButton4.setOnClickListener(new OnClickListener() {
                        
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                
                                String address = "00:10:10:22:02:72";   //电脑MAC 
                                UUID uuid = UUID.fromString(SPP_UUID);  

                                //创建adapter对象
                                BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
                                //通过MAC地址创建BluetoothDevice对象
                                BluetoothDevice btDev = adapter.getRemoteDevice(address);
                                
                                try {
                                        btSocket = btDev.createRfcommSocketToServiceRecord(uuid);
                                        btSocket.connect();
                                        System.out.println("----"+btSocket.getRemoteDevice().toString());
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                        System.out.println("创建RFCOM出错");
                                }
                                
                                InputStream is =null;
                                OutputStream os = null;
                                
                                try {
                        is = btSocket.getInputStream();
                        os = btSocket.getOutputStream();
                    } catch (IOException e) {
                            e.printStackTrace();
                            System.out.println("·······");
                    }
                    
                    String message = "01123456";
                    byte[] send = message.getBytes();
                    
                    try{
                            os.write(send);
                    } catch(IOException e) {
                            e.printStackTrace();
                            System.out.println("输出时出现异常");
                    }
                        }
                });
        
    }
    
    //广播监听类
    private class BluetoothReceiver extends BroadcastReceiver{

                @Override
                public void onReceive(Context context, Intent intent) {
                        // TODO Auto-generated method stub
                        String action = intent.getAction();
                        if(BluetoothDevice.ACTION_FOUND.equals(action)){
                                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                            System.out.println(device.getName()+"----"+device.getAddress());
                            
                            str += device.getName()+"----"+device.getAddress() + "\n";
                            myTextView.setText(str);
                            
                        }
                }
            
    }
}


/*
 * 未开启蓝牙功能的时候仍然能够得到已经配对的蓝牙的信息
 * 已配对的蓝牙设备的信息已经存储在了手机里,但是不能进行连接操作
 * 
 */
阅读(5539) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~