版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

Eg:

一.  清单文件AdroidManifest.xml:

  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. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. <uses-permission android:name="android.permission.BLUETOOTH"/>
  17. <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
  18. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  19. </manifest>

二. 布局文件: main.xml:

  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:

  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. public class MainActivity extends Activity {
  13. private Button discoverButton = null;
  14. private Button scanButton = null;
  15. private BluetoothAdapter adapter = null;
  16. private BluetoothReceiver bluetoothReceiver = null;
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. adapter = BluetoothAdapter.getDefaultAdapter();
  23. discoverButton = (Button)findViewById(R.id.discoverButton);
  24. scanButton = (Button)findViewById(R.id.scanButton);
  25. //修改蓝牙设备的可见性
  26. discoverButton.setOnClickListener(new OnClickListener(){
  27. @Override
  28. public void onClick(View view) {
  29. Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  30. //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
  31. discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
  32. startActivity(discoverIntent);
  33. }
  34. });
  35. scanButton.setOnClickListener(new OnClickListener(){
  36. @Override
  37. public void onClick(View v) {
  38. //开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
  39. adapter.startDiscovery();
  40. }
  41. });
  42. //设定广播接收的filter
  43. IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  44. //创建蓝牙广播信息的receiver
  45. bluetoothReceiver = new BluetoothReceiver ();
  46. //注册广播接收器
  47. registerReceiver(bluetoothReceiver,intentFilter);
  48. }
  49. private class BluetoothReceiver extends BroadcastReceiver{
  50. @Override
  51. public void onReceive(Context context, Intent intent) {
  52. //获得扫描到的远程蓝牙设备
  53. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  54. System.out.println(device.getAddress());
  55. }
  56. }
  57. }

Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备的更多相关文章

  1. Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备

    一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 一.  清单文件AdroidManifest.xml: <uses-permission android:name="an ...

  2. Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) ...

  3. ZT android -- 蓝牙 bluetooth (二) 打开蓝牙

    android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...

  4. 【视频】零基础学Android开发:蓝牙聊天室APP(二)

    零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...

  5. Android开发艺术探索笔记——View(二)

    Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...

  6. 【视频】零基础学Android开发:蓝牙聊天室APP(四)

    零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...

  7. 【视频】零基础学Android开发:蓝牙聊天室APP(三)

    零基础学Android开发:蓝牙聊天室APP第三讲 3.1 ImageView.ImageButton控件具体解释 3.2 GridView控件具体解释 3.3 SimpleAdapter适配器具体解 ...

  8. 【视频】零基础学Android开发:蓝牙聊天室APP(一)

    零基础学Android开发:蓝牙聊天室APP第一讲 1. Android介绍与环境搭建:史上最高效Android入门学习 1.1 Google的大小战略 1.2 物联网与云计算 1.3 智能XX设备 ...

  9. Android开发之蓝牙--扫描已经配对的蓝牙设备

    一. 什么是蓝牙(Bluetooth)? 1.1  BuleTooth是目前使用最广泛的无线通信协议 1.2  主要针对短距离设备通讯(10m) 1.3  常用于连接耳机,鼠标和移动通讯设备等. 二. ...

随机推荐

  1. 关闭linux终端进程

    [root@linux-node1 ~]# w 22:16:45 up 24 days, 24 min, 2 users, load average: 0.28, 0.17, 0.15 USER TT ...

  2. 从A小程序跳转到B小程序

    从A小程序跳转到B小程序: A小程序 wxml: <navigator target="miniProgram" open-type="navigate" ...

  3. Java基础学习总结(30)——Java 内存溢出问题总结

    Java中OutOfMemoryError(内存溢出)的三种情况及解决办法 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各 ...

  4. Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决

    程序很easy.好长时间没有搞定.郁闷.... . .... . . . 在论坛咨询,最终找到答案. 描写叙述: 一个Activity:MainActivity.内部是一个Fragment:Fragm ...

  5. 轻松学习JavaScript二十二:DOM编程学习之节点操作

    DOM编程不只能够查找三种节点,也能够操作节点.那就是创建,插入,删除.替换和复制节点.先来看节点 操作方法: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  6. BAPC2014 K&amp;&amp;HUNNU11591:Key to Knowledge(中途相遇法)

    题意: 有N个学生.有M题目 然后相应N行分别有一个二进制和一个整数 二进制代表该同学给出的每道题的答案.整数代表该同学的答案与标准答案相符的个数 要求推断标准答案有几个,假设标准答案仅仅有一种.则输 ...

  7. extjs Combox 调用数据

    1方法一 从 json获取 var typeStore = new Ext.data.Store({                 proxy : new Ext.data.HttpProxy({u ...

  8. poj_3468,线段树成段更新

    参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include& ...

  9. 10.Intellij IDEA svn的使用详解

    转自:https://www.2cto.com/kf/201703/614858.html 首先提一句,IDEA对各种的版本控制工具的支持是非常好的,打开系统设置界面,就可以看到他有专门的一栏 Ver ...

  10. php7安装memcache 和 memcached 扩展

    php7安装memcache 和 memcached 扩展 标签(空格分隔): php memcache和memcached区别 memcache:http://pecl.php.net/packag ...