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需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考! //先说 ...
随机推荐
- 局域网arpspoof欺骗获取cookie/图片/密码
开启路由转发功能 查看IP转发功能是否打开 默认是不开起,0,我这里是修改后的,显示1. 修改转发功能,1为允许. 修改成功后再进行Arpspoof欺骗 如果开始劫持后,自己电脑无法联网了 ??? 检 ...
- iframe子页面position的fixed
前言: 首先说一说我昨天天的苦逼经历.中午吃饭时一同事跟我说,他做的项目嵌套iframe后,子页面的position设置fixed失效了. 经过反复询问,得知他用了两层iframe,再加上最外的父页面 ...
- final发布视频展示博客
Part One [探路者]选题展示视频链接: http://v.youku.com/v_show/id_XMzIxMDM2MTQ1Ng==.html?spm=a2h3j.8428770.341605 ...
- [buaa-SE-2017]个人作业-回顾
个人作业-回顾 提问题的博客:[buaa-SE-2017]个人作业-Week1 Part1: 问题的解答和分析 1.1 问题:根据书中"除了前20的学校之外,计科和软工没有区别"所 ...
- python learning2.py
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] # 取前3个元素的笨方法 r = [] n = 3 for i in range(n): r.appe ...
- 25_IO_第25天(Properties、序列化流、打印流、CommonsIO)_讲义
今日内容介绍 1.Properties集合 2.序列化流与反序列化流 3.打印流 4.commons-IO 01Properties集合的特点 * A: Properties集合的特点 * a: Pr ...
- 深入理解JAVA集合系列二:ConcurrentHashMap源码解读
HashMap和Hashtable的区别 在正式开始这篇文章的主题之前,我们先来比较下HashMap和Hashtable之间的差异点: 1.Hashtable是线程安全的,它对外提供的所有方法都是都使 ...
- 安装/卸载 修改Config
参考地址:https://docs.microsoft.com/zh-cn/nuget/create-packages/source-and-config-file-transformations
- cmd 中运行testng代码
说明:classpath是jvm执行class时所加载的路径:--个人理解,如有不同:QQ:316567803 1.先下载插件 https://plugins.jetbrains.com/plugin ...
- [转帖]2018年JVM生态系统报告出炉
很多未解之谜终于有答案了——2018年JVM生态系统报告出炉 https://blog.csdn.net/hollis_chuang/article/details/84134298 2018年1 ...