zoukankan      html  css  js  c++  java
  • Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备

    一. 修改本机蓝牙设备的可见性

    二. 扫描周围可用的蓝牙设备

    Eg:

    一.  清单文件AdroidManifest.xml:

    [java] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.       package="com.se7en"  
    4.       android:versionCode="1"  
    5.       android:versionName="1.0">  
    6.     <uses-sdk android:minSdkVersion="8" />  
    7.   
    8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    9.         <activity android:name=".MainActivity"  
    10.                   android:label="@string/app_name">  
    11.             <intent-filter>  
    12.                 <action android:name="android.intent.action.MAIN" />  
    13.                 <category android:name="android.intent.category.LAUNCHER" />  
    14.             </intent-filter>  
    15.         </activity>  
    16.     </application>  
    17.     <uses-permission android:name="android.permission.BLUETOOTH"/>  
    18.       
    19.     <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->  
    20.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>  
    21. </manifest>  

    二. 布局文件: main.xml:

    [java] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <TextView    
    8.         android:layout_width="fill_parent"   
    9.         android:layout_height="wrap_content"   
    10.         android:text="@string/hello"  
    11.         />  
    12.     <Button   
    13.         android:id="@+id/discoverButton"  
    14.         android:layout_width="fill_parent"  
    15.         android:layout_height="wrap_content"  
    16.         android:text="设置可见性"/>  
    17.     <Button   
    18.         android:id="@+id/scanButton"  
    19.         android:layout_width="fill_parent"  
    20.         android:layout_height="wrap_content"  
    21.         android:text="开始扫描"/>  
    22. </LinearLayout>  

    三. MainActivity:
    [java] view plain copy
     
    1. import android.app.Activity;  
    2. import android.bluetooth.BluetoothAdapter;  
    3. import android.bluetooth.BluetoothDevice;  
    4. import android.content.BroadcastReceiver;  
    5. import android.content.Context;  
    6. import android.content.Intent;  
    7. import android.content.IntentFilter;  
    8. import android.os.Bundle;  
    9. import android.view.View;  
    10. import android.view.View.OnClickListener;  
    11. import android.widget.Button;  
    12.   
    13. public class MainActivity extends Activity {  
    14.     private Button discoverButton = null;  
    15.     private Button scanButton = null;  
    16.     private BluetoothAdapter adapter = null;  
    17.     private BluetoothReceiver bluetoothReceiver = null;  
    18.     /** Called when the activity is first created. */  
    19.     @Override  
    20.     public void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.         setContentView(R.layout.main);  
    23.           
    24.         adapter = BluetoothAdapter.getDefaultAdapter();  
    25.           
    26.         discoverButton = (Button)findViewById(R.id.discoverButton);  
    27.         scanButton = (Button)findViewById(R.id.scanButton);  
    28.         //修改蓝牙设备的可见性  
    29.         discoverButton.setOnClickListener(new OnClickListener(){  
    30.             @Override  
    31.             public void onClick(View view) {  
    32.             Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
    33.   
    34. //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300  
    35. discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);  
    36. startActivity(discoverIntent);  
    37.             }  
    38.         });  
    39.           
    40.         scanButton.setOnClickListener(new OnClickListener(){  
    41.             @Override  
    42.             public void onClick(View v) {  
    43.         //开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息  
    44.                 adapter.startDiscovery();  
    45.             }  
    46.         });  
    47.           
    48.         //设定广播接收的filter  
    49.         IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
    50.         //创建蓝牙广播信息的receiver  
    51.         bluetoothReceiver = new BluetoothReceiver ();  
    52.         //注册广播接收器  
    53.         registerReceiver(bluetoothReceiver,intentFilter);  
    54.               
    55.     }  
    56.       
    57.     private class BluetoothReceiver extends BroadcastReceiver{  
    58.         @Override  
    59.         public void onReceive(Context context, Intent intent) {  
    60.             //获得扫描到的远程蓝牙设备  
    61.             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
    62.             System.out.println(device.getAddress());  
    63.         }  
    64.           
    65.     }  
    66. }  
  • 相关阅读:
    为什么使用Redis
    [Apache Pulsar] 企业级分布式消息系统-Pulsar快速上手
    [Apache Pulsar] 企业级分布式消息系统-Pulsar入门基础
    JDK源码分析系列---ArrayList和LinkList
    JDK源码分析系列---String,StringBuilder,StringBuffer
    单点登录
    单例模式的几种实现方式
    Bloom’S Taxonomy
    Python 字符串多替换时性能基准测试
    软件开发的生产力vs质量
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6798532.html
Copyright © 2011-2022 走看看