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需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考! //先说 ...
随机推荐
- Tomcat java zabbix 监控
排除汤姆猫错误的步骤 ps-ef | grep java或jps –lvm 查看java pid进程 netstat –lntup | grep java 查看java 端口有没有启动 查看 tomc ...
- linux cat显示若干行
[一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1000 [二]显示1000行到3000行 cat ...
- 分布式高并发下全局ID生成策略
数据在分片时,典型的是分库分表,就有一个全局ID生成的问题.单纯的生成全局ID并不是什么难题,但是生成的ID通常要满足分片的一些要求: 1 不能有单点故障. 2 以时间为序,或者ID里包含时间 ...
- git push remote: User permission denied
这种错误因为本地保存了一个错误的账号密码,只需要重新编辑成正确的账号密码 直接上方法
- 2018年第九届蓝桥杯【C++省赛B组】
2标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...
- Bing词典vs有道词典比对测试报告
功能篇 核心功能测评:http://www.cnblogs.com/C705/p/4075554.html 细节与用户体验:http://www.cnblogs.com/C705/p/4077112. ...
- android学习-1
所有的android应用都是由屏幕构成的一个集合,每个屏幕则由一个活动和一个布局组成. 活动--用户可以完成的一个确定的事. 布局--对屏幕外观的描述.(布局写为一个XML文件,回告诉android如 ...
- 20162325 金立清 S2 W9 C18
20162325 2017-2018-2 <程序设计与数据结构>第9周学习总结 教材学习内容概要 堆是一棵完全二叉树,其中每个元素大于等于其所有子结点的值. 向堆中添加一个元素的方法是,首 ...
- JavaScript实现大整数减法
继上一篇博文写了大整数加法之后,我又模拟上篇博文的算法,自己实现了大整数减法. 大整数减法相对于加法来说,稍微复杂一点.由于要考虑一些情况: 1. 两个数相减,可能会出现结果为正.负和0三种情况: 2 ...
- 图层损坏 E/ArcGIS﹕ The map or layer has been destroyed or recycled. 资源未释放
看到论坛上有个网友和我一样的问题: The map or layer has been destroyed or recyled t Hello, I have a problem when the ...