之前没怎么接触过蓝牙模块,在学习的过程中借鉴了很多前辈的经验。本次主要包含以下功能:

  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>

蓝牙主要用的两个类BluetoothAdapterBluetoothDevice,在通讯方面会用到BluetoothServerSocket

目前了解的只有这么多了,还要继续努力。

备注:

1、转载请注明出去

2、代码虽然是我写的,但是他自己长歪了,有问题尽量别找我 ~~~~(>_<)~~~~ 。

3、谢谢阅读

Android 蓝牙模块基础操作的更多相关文章

  1. 深入selenium模块基础操作

    selenium模块的基本操作 一.模拟浏览器 ​ 谷歌.Firefox.Safari等浏览器 browser=webdriver.Chrome() browser=webdriver.Firefox ...

  2. Android蓝牙----打开,关闭操作

    ① 我们先在AndroidManifest里面增加我们的Bluetooth权限 <uses-permission android:name="android.permission.BL ...

  3. nltk模块基础操作

     几个基础函数 (1)搜索文本:text.concordance(word) 例如,在text1中搜索词”is”在文本中出现的次数以及上下文的词:text1.concordance("is& ...

  4. android 蓝牙连接与通讯(Bluetooth)

    最近做了一个小项目,关于蓝牙的一个智能硬件.其中涉及到了蓝牙模块的操作.特记下蓝牙模块的操作过程.只记录下关于蓝牙部分的操作,具体业务逻辑不涉及其中.重点是记录下蓝牙的扫描.链接.通讯. 在使用蓝牙模 ...

  5. Android蓝牙串口通讯【转】

    本文转载自:http://blog.sina.com.cn/s/blog_631e3f2601012ixi.html Android蓝牙串口通讯 闲着无聊玩起了Android蓝牙模块与单片机蓝牙模块的 ...

  6. 【Espruino】NO.13 蓝牙模块

    http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...

  7. 深入了解Android蓝牙Bluetooth——《基础篇》

    什么是蓝牙?   也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用"蓝牙"技术,能够有效地简化掌上电脑.笔记本电 ...

  8. 【Arduino】开发入门【十】Arduino蓝牙模块与Android实现通信

    [Arduino]开发入门[十]蓝牙模块 首先show一下新入手的蓝牙模块 蓝牙参数特点 1.蓝牙核心模块使用HC-06从模块,引出接口包括VCC,GND,TXD,RXD,预留LED状态输出脚,单片机 ...

  9. android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目

      2017.10.20 之前参加一个大三学长的创业项目,做一个智能的车锁App,用到嵌入式等技术,App需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考!             //先说 ...

随机推荐

  1. Python 自动爬取B站视频

    文件名自定义(文件格式为.py),脚本内容: #!/usr/bin/env python #-*-coding:utf-8-*- import requests import random impor ...

  2. tty命令详解

    基础命令学习目录首页 原文链接:http://blog.chinaunix.net/uid-9525959-id-2001836.html [功能]  打印连接到标准输入的终端的文件名. [描述]  ...

  3. CocoaPods did not set the base configuration of your project because your project already has a custom config set.

    今天在封装自己的消息推送SDK的时候,pod install 的时候,突然报这个错误,解决方式如下: $ pod install Analyzing dependencies Downloading ...

  4. 搭建gitpage博客

    http://blog.csdn.net/jzooo/article/details/46781805

  5. PyCharm配置SFTP远程调试Django应用

    http://www.ithao123.cn/content-41747.html http://www.th7.cn/system/lin/201703/205998.shtml

  6. 作业 20181127-3 互评Beta版本

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2448 组名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙赛佳 ...

  7. 【数据预处理】TIMIT语料库WAV文件转换

    1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...

  8. BufferedWriter与BufferedRead --------------------------Test2

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  9. HDU 4514 湫湫系列故事——设计风景线 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4514 湫湫系列故事--设计风景线 Time Limit: 5000/2000 MS (Java/Ot ...

  10. APP案例分析——Steam

    本次作业的分析对象是Steam,一款全球最大最广泛的游戏平台.之所以选择Steam是因为我已经在这上面挥洒了大量的青春,对它也有了很深的感情. 调研.评测 个人第一次上手体验 打开首页就可以看到琳琅满 ...