ANDROID_MARS学习笔记_S03_002_设置可见性及扫描蓝牙设备
一、代码
1.xml
(1)AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
2.java
(1)MainActivity.java
package com.bluetooth2; 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; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 v) {
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
System.out.println("设置蓝牙可见性-->");
startActivity(discoverIntent);//android自带的activity
}
}); scanButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
System.out.println("开始扫描周围蓝牙设备-->");
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("BluetoothReceiver收到广播---->" + device);
}
}
}
ANDROID_MARS学习笔记_S03_002_设置可见性及扫描蓝牙设备的更多相关文章
- WPF 学习笔记-设置属性使窗口不可改变大小
原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...
- Ionic2学习笔记(10):扫描二维码
作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5575843.html 时间:6/11/2016 说明: 在本文发表的时候(2016-06-1 ...
- ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置
一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...
- JAVA并发编程学习笔记------对象的可见性及发布逸出
一.非原子的64位操作: 当线程在没有同步的情况下读取变量时,可能会得到一个失效值,但至少这个值是由之前某个线程设置的值,而不是一个随机值,这种安全性保证被称为最低安全性.最低安全性适用于绝大多数变量 ...
- 【小梅哥SOPC学习笔记】设置Eclipse在编译(build)前自动保存源代码文件
设置Eclipse在编译(build)前自动保存源代码文件 Eclipse 常用设置之让Eclipse在编译(build)前自动保存源代码文件 一.让Eclipse在编译(build)前自动保存源代码 ...
- HTML学习笔记——标签设置格式
1>标签设置格式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- LR学习笔记---参数设置 (转 温故而知新)
LR在录制程序运行的过程中,VuGen(脚本生成器) 自动生成了包含录制过程中实际用到的数值的脚本,如果你企图在录制的脚本中使用不同的数值执行脚本的活动(如查询.提交等等),那么你必须用参数值取代录制 ...
- [moka学习笔记]yii2设置语言和时区
1.在web/index.php中 (new yii\web\Application($config))->run(); $app = new \yii\web\Application($con ...
- ANDROID_MARS学习笔记_S01_011ProgressBar
文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...
随机推荐
- HDU-1018(简单数学)
Big Number Problem Description In many applications very large integers numbers are required. Some o ...
- mysql自增
主键设置自增,同时主键需要是int类型
- javaweb常用工具类及配置文件备份
Javaweb常用工具类及配置文件备份 做一个代码备份,以后常用到的. hibernate工具类备份 package com.dly.service; /* * hibernate获取sessi ...
- CAS Server 单点登录开源项目
https://www.apereo.org/cas/download This project contains code that can extent an existing ASP.NET w ...
- Oracle hextoraw和rawtohex
Oracle hextoraw和rawtohex [日期:2012-07-17] 来源:Linux社区 作者:adrain_001 [字体:大 中 小] HEXTORAW 语法: HEXT ...
- Spring Mvc 笔记二之异常和文件上传
spring mvc的异常与文件上传 1.异常: spring注解版的异常有局部异常和全局异常 1.局部异常对单个controller有效;(在controller类写一 ...
- 新建DragonBones动画文件
本篇文章由:http://www.sollyu.com/new-dragonbones-animation-file/ 说明 我在网上找了很久都没找到关于怎么创建一个DragonBones动画的文章, ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- (转)Objective-C中的instancetype和id区别
有一个相同两个不同.相同 Written by Mattt Thompson on Dec 10th, Objective-C is a rapidly evolving language, in a ...
- c# winform 隐藏tabcontrol标签
Apperarance 属性:Faltbuttons SizeMode属性:Fixed 各个TabPage的Text :空 ItemSize : Width=0;Height=1;