Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载。
一. 修改本机蓝牙设备的可见性
二. 扫描周围可用的蓝牙设备
Eg:
一. 清单文件AdroidManifest.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.se7en"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.BLUETOOTH"/>
- <!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
- </manifest>
二. 布局文件: main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/discoverButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="设置可见性"/>
- <Button
- android:id="@+id/scanButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="开始扫描"/>
- </LinearLayout>
三. MainActivity:
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button discoverButton = null;
- private Button scanButton = null;
- private BluetoothAdapter adapter = null;
- private BluetoothReceiver bluetoothReceiver = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- adapter = BluetoothAdapter.getDefaultAdapter();
- discoverButton = (Button)findViewById(R.id.discoverButton);
- scanButton = (Button)findViewById(R.id.scanButton);
- //修改蓝牙设备的可见性
- discoverButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View view) {
- Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- //设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
- discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
- startActivity(discoverIntent);
- }
- });
- scanButton.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- //开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
- adapter.startDiscovery();
- }
- });
- //设定广播接收的filter
- IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- //创建蓝牙广播信息的receiver
- bluetoothReceiver = new BluetoothReceiver ();
- //注册广播接收器
- registerReceiver(bluetoothReceiver,intentFilter);
- }
- private class BluetoothReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- //获得扫描到的远程蓝牙设备
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- System.out.println(device.getAddress());
- }
- }
- }
Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备的更多相关文章
- Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 一. 清单文件AdroidManifest.xml: <uses-permission android:name="an ...
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) ...
- ZT android -- 蓝牙 bluetooth (二) 打开蓝牙
android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(二)
零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...
- Android开发艺术探索笔记——View(二)
Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(四)
零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(三)
零基础学Android开发:蓝牙聊天室APP第三讲 3.1 ImageView.ImageButton控件具体解释 3.2 GridView控件具体解释 3.3 SimpleAdapter适配器具体解 ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(一)
零基础学Android开发:蓝牙聊天室APP第一讲 1. Android介绍与环境搭建:史上最高效Android入门学习 1.1 Google的大小战略 1.2 物联网与云计算 1.3 智能XX设备 ...
- Android开发之蓝牙--扫描已经配对的蓝牙设备
一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) 1.3 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
随机推荐
- CSS常用样式--background
CSS background 属性 参考:W3school- CSS background 所有浏览器都支持 background 属性,其简写形式,在一个声明中设置所有的背景属性,各属性需按顺序,语 ...
- BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)
题解 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #in ...
- 升级glibc的感慨,
1. 直接升级 glibc是gnu发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc.glibc除了封装linux操作系统所提供的系统服务 ...
- caioj 1161 欧拉函数3:可见点数
(x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...
- 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)
很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...
- 紫书 习题8-6 UVa 1611 (构造法)
这道题和例题8-1相当的像. 例题8-1https://blog.csdn.net/qq_34416123/article/details/80112017 一开始我还以为用归并的思想, 用交换把局部 ...
- Vue中如何监控某个属性值的变化?
比如现在需要监控data中, obj.a 的变化.Vue中监控对象属性的变化你可以这样: deep属性表示深层遍历,但是这么写会监控obj的所有属性变化,并不是我们想要的效果,所以做点修改: 还有一种 ...
- Android设置头像,手机拍照或从本地相冊选取图片作为头像
[Android设置头像,手机拍照或从本地相冊选取图片作为头像] 像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相冊之类的图片库中已 ...
- mysql-高级语言语法
一.注释 从#字符到行尾 从'-- '序列到行尾.两个破折号之后至少要有一个空格符或制表符. 二.设置变量 用户变量的形式为@var_name,其中变量名varname可以有当前字符集的文字数字字符. ...
- hdu_1394,线段树求逆序数
http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include< ...