Android开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
一. 修改本机蓝牙设备的可见性
二. 扫描周围可用的蓝牙设备
一. 清单文件AdroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
二. 布局文件: 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开发之蓝牙 --修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备的更多相关文章
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 Eg: 一. 清单文件AdroidManifest.xml: <?xml v ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(四)
零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(二)
零基础学Android开发:蓝牙聊天室APP第二讲 2.1 课程内容应用场景 2.2 Android UI设计 2.3 组件布局:LinearLayout和RelativeLayout 2.4 Tex ...
- 【视频】零基础学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 常用于连接耳机,鼠标和移动通讯设备等. 二. ...
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) ...
- macbook Android开发环境搭建,真机调试
买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...
- android开发关于和使用本机内存、内置存储卡和外置存储卡 (转)
转自:http://www.2cto.com/kf/201304/204729.html 关于android存储器简介: android开发常常需要涉及数据缓存,这就 ...
随机推荐
- 21: Arithmetic Sequence--HZAU(dp)
http://acm.hzau.edu.cn/problem.php?id=21 题目大意: 给你一个序列问在数字最多的等比数列 分析: 刚开始看到题就知道是一个dp但是我dp实在是渣到不行 后来发 ...
- C# 基础(4)
Ref 按地址传递,引用传递:即可以往外返值,也可以对内传值. Out是用于传出值, Ref是双向的,即可以传入,也可以传出. 在传参过程中,如果参数有out,那么改变方法中参数的值,调用者方法中变量 ...
- jquery实现TODOList
html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- Exception in thread "http-apr-8080-exec-2"
设置了catalina.bat.catalina.sh都不起作用MyEclipse中选择菜单Windows---preferences---MyEclipse---Servers---Tomcat-- ...
- Dynamic CRM 2013学习笔记(二十)字段改变事件的二种实现方法
CRM里有二种方式实现字段change事件,一种是在form里,一种完全通过js来实现.本文介绍下二者的用途及区别. 1. Form里用法 这种方式估计其实也是添加一个js的function. 这种方 ...
- 解决dnu restore时的“Cannot handle address family”问题
前几天在使用基于 mono 的 dnx 中的 dnu restore 命令安装 nuget 包包时,遇到了 “Cannot handle address family” 错误,错误详情如下: Cann ...
- mongo(三)基本操作
mongo(三)基本操作 本文来自mongodb官方文档的部分翻译以及自己的理解. CRUD:增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delet ...
- JQuery ajax url传值与data传值的区别
url传中文,乱码,即便charset为 UTF-8, $.ajax({ type: "POST", cache: false, url: "/Prod ...
- java web图片显示到浏览器
今天研究了一下午,图片显示问题. jsp中获取绝对路径的方法:String contextPath = request.getContextPath();String path = request.g ...
- linux tar order
http://blog.csdn.net/imyang2007/article/details/7634470 把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案-x:解压-t:查 ...