zoukankan      html  css  js  c++  java
  • Android 蓝牙扫描

    一,获得BluetoothAdapter对象

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    二,判断当前设备中是否有蓝牙设备

    if(adapter!=null){
          //有蓝牙设备
         }else{
          //没有蓝牙设备
         }

    三,判断蓝牙是否打开和打开蓝牙

     

    if(adapter.isEnabled()){  
        //BluetoothAdapter.ACTION_REQUEST_ENABLE为启动蓝牙的action  
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
        startActivity(intent);  
       }  

    四,得到所有已经配对蓝牙设备地址

    Set<BluetoothDevice> devices = adapter.getBondedDevices();  
                    if(devices.size()>0){  
                        for(Iterator iterator = devices.iterator();iterator.hasNext();){  
                            BluetoothDevice device = (BluetoothDevice) iterator.next();  
                            System.out.println("已配对的设备:"+device.getAddress());  
                        }  
                    }  

    五,设置蓝牙的可见性

    //启动修改蓝牙可见性的Intent  
                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
                        //设置蓝牙可见性的时间,方法本身规定最多可见300秒  
                        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);  
                        startActivity(intent);  

    六,扫描周围的蓝牙设备

    adapter.startDiscovery();  

    android把扫描到的蓝牙设备通过广播的形式发出去,所以想接收扫描结果就必须写个广播接收器类。

     

    七,注意事项:权限

    注意:模拟器上不能模拟蓝牙设备,只能在真机上才能看到结果。

    <!-- 使用蓝牙设备的权限 -->  
       <uses-permission android:name="android.permission.BLURTOOTH"/>  
        <!-- 管理蓝牙设备的权限 -->  
       <uses-permission android:name="android.permission.BLURTOOTH_ADMIN"/>  
  • 相关阅读:
    ACdream 1224 Robbers (贪心)
    HDU 4320 Arcane Numbers 1 (质因子分解)
    在脚本中重定向输入
    呈现数据
    shell中的for、while、until(二)
    shell中的for、while、until
    C 指针疑虑
    结构化命令
    fdisk -c 0 350 1000 300命令
    PC机上的COM1口和COM2口
  • 原文地址:https://www.cnblogs.com/wei1228565493/p/4835745.html
Copyright © 2011-2022 走看看