Android 蓝牙模块基础操作
之前没怎么接触过蓝牙模块,在学习的过程中借鉴了很多前辈的经验。本次主要包含以下功能:
1、检测是否存在蓝牙模块
2、蓝牙的开启与关闭
3、与本机已配对的蓝牙设备
4、本机蓝牙可见性设置
5、扫描周围蓝牙设备
关于蓝牙设备之间如何通讯下次再整理。下面开始介绍。
1.1、首先要在配置文件中加入操作蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
第一个权限可以控制蓝牙模块的检测、开启与关闭。如果需要扫描周围蓝牙设备等更多功能则需要第二个权限。
1.2、具体代码
btn_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BluetoothAdapter bltadapter = BluetoothAdapter.getDefaultAdapter();//本机适配器
if(bltadapter == null)
{
Toast.makeText(getApplicationContext(), "本机没有蓝牙设备", 0).show();
tv_result.setText("本机没有蓝牙设备");
}
else
{
Toast.makeText(getApplicationContext(), "本机拥有蓝牙设备", 0).show();
tv_result.setText("本机拥有蓝牙设备");
if(!bltadapter.isEnabled())//检测蓝牙是否打开
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);//开启蓝牙
startActivity(intent);
}
Set <BluetoothDevice> bltDevices = bltadapter.getBondedDevices();
String address = "已配对蓝牙设备";
if(bltDevices.size()>0)
{
for(Iterator iterator = bltDevices.iterator();iterator.hasNext();)//迭代器收集已适配的蓝牙地址并打印
{
BluetoothDevice bltdevice = (BluetoothDevice)iterator.next();
address = address+"\n"+bltdevice.getAddress().toString();
}
tv_result.setText(address);
}
}
}
});
1.3结果截图
a)初始状态蓝牙未打开

b)请求打开蓝牙


c)显示已配对设备

2.1、设置蓝牙可见性,这里需要说明的是,根据官方介绍
The current default is 120 seconds, and requests over 300 seconds will be capped. These values could change.
即设备可见时间默认为120s,最大为300s,如果参数大于300则等于300。
代码如下
btn_discover.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);//400即为设置的可见时间超过300则等于300
startActivity(intent);
}
});
2.2、结果截图如下

3、扫描附近蓝牙。蓝牙本身采用广播机制。代码如下:
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);//过滤器
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
registerReceiver(bluetoothReceiver, intentFilter);//注册接受者
private class BluetoothReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice bltdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
result1 = result1+"\n"+bltdevice.getAddress().toString();
tv_result.setText(result1);
}
} }
btn_scan.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
bltadapter.startDiscovery();
}
});
3.2、结果截图如下

4、最后附上更改后的全部代码,欢迎批评指正。
java代码
package com.example.bluetooth; import java.util.Iterator;
import java.util.Set;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
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;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { TextView tv_result;
Button btn_check;
Button btn_scan;
Button btn_discover;
String result1 = "周围蓝牙设备";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final BluetoothAdapter bltadapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
registerReceiver(bluetoothReceiver, intentFilter);
tv_result = (TextView)findViewById(R.id.CheckResult);
btn_check = (Button)findViewById(R.id.CheckBlt);
btn_scan = (Button)findViewById(R.id.Scan);
btn_discover = (Button)findViewById(R.id.Visable);
btn_check.setOnClickListener(new OnClickListener() { @SuppressLint("ShowToast")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BluetoothAdapter bltadapter = BluetoothAdapter.getDefaultAdapter();
if(bltadapter == null)
{
Toast.makeText(getApplicationContext(), "本机没有蓝牙设备", 0).show();
tv_result.setText("本机没有蓝牙设备");
}
else
{
Toast.makeText(getApplicationContext(), "本机拥有蓝牙设备", 0).show();
tv_result.setText("本机拥有蓝牙设备");
if(!bltadapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
Set <BluetoothDevice> bltDevices = bltadapter.getBondedDevices();
String address = "已配对蓝牙设备";
if(bltDevices.size()>0)
{
for(Iterator<BluetoothDevice> iterator = bltDevices.iterator();iterator.hasNext();)
{
BluetoothDevice bltdevice = (BluetoothDevice)iterator.next();
address = address+"\n"+bltdevice.getAddress().toString();
}
tv_result.setText(address);
}
}
}
});
btn_discover.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400);
startActivity(intent);
}
});
btn_scan.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
bltadapter.startDiscovery();
}
}); }
private class BluetoothReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice bltdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
result1 = result1+"\n"+bltdevice.getAddress().toString();
tv_result.setText(result1);
}
} } }
xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
> <Button
android:id="@+id/CheckBlt"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="检查本机蓝牙"
/>
<Button
android:id="@+id/Visable"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="设置可见性"
/>
<Button
android:id="@+id/Scan"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="扫描周围蓝牙"
/>
<TextView
android:id="@+id/CheckResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
蓝牙主要用的两个类BluetoothAdapter和BluetoothDevice,在通讯方面会用到BluetoothServerSocket。
目前了解的只有这么多了,还要继续努力。
备注:
1、转载请注明出去
2、代码虽然是我写的,但是他自己长歪了,有问题尽量别找我 ~~~~(>_<)~~~~ 。
3、谢谢阅读
Android 蓝牙模块基础操作的更多相关文章
- 深入selenium模块基础操作
selenium模块的基本操作 一.模拟浏览器 谷歌.Firefox.Safari等浏览器 browser=webdriver.Chrome() browser=webdriver.Firefox ...
- Android蓝牙----打开,关闭操作
① 我们先在AndroidManifest里面增加我们的Bluetooth权限 <uses-permission android:name="android.permission.BL ...
- nltk模块基础操作
几个基础函数 (1)搜索文本:text.concordance(word) 例如,在text1中搜索词”is”在文本中出现的次数以及上下文的词:text1.concordance("is& ...
- android 蓝牙连接与通讯(Bluetooth)
最近做了一个小项目,关于蓝牙的一个智能硬件.其中涉及到了蓝牙模块的操作.特记下蓝牙模块的操作过程.只记录下关于蓝牙部分的操作,具体业务逻辑不涉及其中.重点是记录下蓝牙的扫描.链接.通讯. 在使用蓝牙模 ...
- Android蓝牙串口通讯【转】
本文转载自:http://blog.sina.com.cn/s/blog_631e3f2601012ixi.html Android蓝牙串口通讯 闲着无聊玩起了Android蓝牙模块与单片机蓝牙模块的 ...
- 【Espruino】NO.13 蓝牙模块
http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...
- 深入了解Android蓝牙Bluetooth——《基础篇》
什么是蓝牙? 也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用"蓝牙"技术,能够有效地简化掌上电脑.笔记本电 ...
- 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信
[Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...
- android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目
2017.10.20 之前参加一个大三学长的创业项目,做一个智能的车锁App,用到嵌入式等技术,App需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考! //先说 ...
随机推荐
- which命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/jkin/p/10289085.html Linux which命令用于查找文件. which指令会在环境变量$PATH ...
- linux简单命令常用随记
//查看网络信息 ifconfig //修改ip地址 ifconfig eth0 123.123.123.123 netmask 255.255.255.0 //网关设置 route add defa ...
- Daily Scrum8 11.12
昨天的任务已完成. 今日任务: 徐钧鸿:个人作业 张艺:构建带有用户管理的框架,并将后端移植好的代码连结. 黄可嵩:完成搜索移植 徐方宇:研究httpclient如何运作,如何利用它实现服务器和客户端 ...
- A Survey on the Security of Stateful SDN Data Planes
论文摘要: 本文为读者提供新兴的SDN带状态数据平面,集中关注SDN数据平面编程性带来的隐患. I部分 介绍 A.带状态SDN数据平面的兴起 B.带状态数据平面带来的安全隐患 引出带状态数据平面的安全 ...
- iOS- 本地文本容错搜索引擎2-->如何实现英文(英文首字母,汉语拼音)对中文的搜索?
1.前言 先闲说几句,最近北京的雾霾真是大,呛的我这攻城师都抗不住了.各位攻城师们一定要爱护好自己的身体!空气好时,少坐多动. 如果条件好的话,最好让你们BOSS搞个室内空气净化器.因为那几天一般 ...
- [转帖]Linux 下 DD 命令的使用详解
https://blog.csdn.net/noviblue/article/details/56012275 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注 ...
- Git Github使用错误汇总
Git使用常见错误 error:failed to push some refs to 'xxx' 本地仓库没有Readme文件,先PULL下远程仓库 git pull --rebase origin ...
- sql学习. case + group by 都干了啥子事情
select case pref_name when 'fudao' then 'siguo' when 'xiangchuan' then 'siguo' when 'aiyuan' then 's ...
- MacOS & dock 工具栏 & 外接显示器 & 主屏
MacOS & dock 工具栏 & 外接显示器 & 主屏 macos 如何将 dock工具栏从外接显示器拖回主屏 https://support.apple.com/zh-c ...
- 计算机网络【10】—— Cookie与Session
一.cookie 和session 的区别 a.cookie数据存放在客户的浏览器上,session数据放在服务器上. b.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKI ...