Android (安卓)蓝牙

Android (安卓)蓝牙

上一节: Android (安卓)铃声控制

上一节

下一节: Android (安卓)相机

下一节

Android (安卓)蓝牙

蓝牙

在许多方法中,蓝牙是一种在两个不同设备之间发送或接收数据的方法。Android平台包括对蓝牙框架的支持,该框架允许设备与其他蓝牙设备无线交换数据。Android提供了蓝牙API来执行这些不同的操作。

扫描其他蓝牙设备

获取已配对设备的列表

通过服务发现连接到其他设备

Android提供了BluetoothAdapter类来与蓝牙通信。通过调用静态方法getDefaultAdapter()创建此调用的对象。其语法如下。

private BluetoothAdapter BA;

BA = BluetoothAdapter.getDefaultAdapter();

为了启用设备的蓝牙,请使用以下蓝牙常量ACTION_REQUEST_ENABLE调用该意图。它的语法是。。

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(turnOn, 0);

除了此常量之外,API还提供了其他常量来支持不同的任务。它们在下面列出。

常量

说明

ACTION_REQUEST_DISCOVERABLE

该常数用于打开蓝牙的发现

ACTION_STATE_CHANGED

该常数将通知蓝牙状态已更改

ACTION_FOUND

此常数用于接收有关发现的每个设备的信息

启用蓝牙后,可以通过调用getBondedDevices()方法获取已配对设备的列表。它返回一组蓝牙设备。它的语法是。

private SetpairedDevices;

pairedDevices = BA.getBondedDevices();

除了被阻止的设备外,API中还有其他方法可以更好地控制蓝牙。它们在下面列出。

方法

说明

enable()

如果未启用,此方法将启用适配器

isEnabled()

如果启用了适配器,则此方法返回true

disable()

此方法禁用适配器

getName()

此方法返回蓝牙适配器的名称

setName(String name)

此方法更改蓝牙名称

getState()

此方法返回蓝牙适配器的当前状态。

startDiscovery()

此方法将在120秒内启动蓝牙的发现过程。

示例

此示例演示了BluetoothAdapter类的知识,该类可操作蓝牙并显示由蓝牙配对的设备的列表。要试验此示例,您需要在实际设备上运行它。

您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。

修改src/MainActivity.java文件以添加代码

修改res/layout/activity_main.xml文件如果需要,可添加任何GUI组件。

修改AndroidManifest.xml以添加必要的权限。

运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。

以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。该文件可以包括每个基本生命周期方法。

package com.jc2182.demo;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.Set;

public class MainActivity extends Activity {

Button b1,b2,b3,b4;

private BluetoothAdapter BA;

private Set pairedDevices;

ListView lv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button);

b2=(Button)findViewById(R.id.button2);

b3=(Button)findViewById(R.id.button3);

b4=(Button)findViewById(R.id.button4);

BA = BluetoothAdapter.getDefaultAdapter();

lv = (ListView)findViewById(R.id.listView);

}

public void on(View v){

if (BA != null){ // 设备不支持蓝牙

boolean isEnabled = BA.isEnabled();

if (isEnabled) {

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(turnOn, 0);

Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();

} else {

Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();

}

}else {

Log.i("Bluetooth:::", "Bluetooth not supported");

// Show proper message here

finish();

}

}

public void off(View v){

if (BA == null){

Log.i("Bluetooth:::", "Bluetooth not supported");

// Show proper message here

finish();

}

BA.disable();

Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();

}

public void visible(View v){

if (BA == null){

Log.i("Bluetooth:::", "Bluetooth not supported");

// Show proper message here

finish();

}

Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

startActivityForResult(getVisible, 0);

}

public void list(View v){

if (BA == null){

Log.i("Bluetooth:::", "Bluetooth not supported");

// Show proper message here

finish();

}

pairedDevices = BA.getBondedDevices();

ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());

Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

lv.setAdapter(adapter);

}

}

以下是res/layout/activity_main.xml文件的内容-

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:transitionGroup="true">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textview"

android:textSize="35dp"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="菜鸟教程"

android:id="@+id/textView"

android:layout_below="@+id/textview"

android:layout_centerHorizontal="true"

android:textColor="#ff7aff24"

android:textSize="35dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageView"

android:background="#22110033"

android:src="@drawable/logo"

android:layout_below="@+id/textView"

android:layout_centerHorizontal="true"

android:theme="@style/Base.TextAppearance.AppCompat" />

android:id="@+id/button"

android:layout_width="98dp"

android:layout_height="wrap_content"

android:layout_below="@+id/imageView"

android:layout_marginEnd="-48dp"

android:layout_marginRight="-48dp"

android:layout_toStartOf="@+id/imageView"

android:layout_toLeftOf="@+id/imageView"

android:clickable="true"

android:onClick="on"

android:text="打开" />

android:id="@+id/button2"

android:layout_width="98dp"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/button"

android:layout_centerHorizontal="true"

android:layout_marginBottom="4dp"

android:onClick="visible"

android:text="可见" />

android:id="@+id/button3"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:layout_below="@+id/imageView"

android:layout_marginStart="-51dp"

android:layout_marginLeft="-51dp"

android:layout_toEndOf="@+id/imageView"

android:layout_toRightOf="@+id/imageView"

android:onClick="list"

android:text="设备列表" />

android:id="@+id/button4"

android:layout_width="102dp"

android:layout_height="wrap_content"

android:layout_below="@+id/button"

android:layout_alignParentStart="true"

android:layout_alignParentLeft="true"

android:onClick="off"

android:text="关闭" />

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/textView2"

android:layout_alignStart="@+id/button"

android:layout_alignLeft="@+id/button"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginStart="3dp"

android:layout_marginLeft="3dp"

android:layout_marginTop="31dp"

android:layout_marginBottom="-22dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="配对的设备:"

android:id="@+id/textView2"

android:textColor="#ff34ff06"

android:textSize="25dp"

android:layout_below="@+id/button4"

android:layout_alignLeft="@+id/listView"

android:layout_alignStart="@+id/listView" />

以下是res/layout/activity_main.xml文件的内容-

package="com.jc2182.demo">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-

您可以依次点击各个按钮来测试蓝牙的功能。

上一节: Android (安卓)铃声控制

上一节

下一节: Android (安卓)相机

下一节

查看笔记 分享笔记

笔记内容:

称呼:

Email:

站点:

分享笔记 重置

分类导航

前端

Ajax 教程

Angular 教程

Aurelia 教程

Bootstrap 教程

ChartJS 教程

CSS 教程

ES6 教程

FontAwesome 教程

HTML 教程

HTML 字符集 教程

HTML 游戏 教程

JavaScript 教程

jQuery 教程

Less 教程

React 教程

Sass 教程

Stylus 教程

TypeScript 教程

Unity 教程

Vue.js 教程

WebAssembly 教程

XAML 教程

颜色 教程

服务端

C# 教程

C++ 教程

COBOL 教程

C语言 教程

Fortran 教程

Go 教程

Groovy 教程

Java 教程

JSP 教程

JVM 教程

Kotlin 教程

Lisp 教程

Lua 教程

Node.js 教程

Pascal 教程

Perl 教程

PHP 教程

Python 教程

Python 3 教程

Ruby 教程

Rust 教程

Scala 教程

Spring 教程

Spring Boot 教程

Spring Cloud 教程

VB.Net 教程

移动端

Android 教程

IOS 教程

Objective-C 教程

React Native 教程

Swift 教程

小程序 教程

数据库

Access 教程

DB2 教程

Mariadb 教程

Memcached 教程

MongoDB 教程

MySQL 教程

Neo4j 教程

PL/SQL 教程

PostgreSQL 教程

Redis 教程

SQL 教程

SQL Server 教程

SQLite 教程

T-SQL 教程

数据格式

Jackson 教程

JSON 教程

SVG 教程

XML 教程

开发工具

ActiveMQ 教程

Ant 教程

Apache HttpClient 教程

Apache POI PPT 教程

AWS 教程

Docker 教程

ElasticSearch 教程

ExpressJS 教程

GIT 教程

GitLab 教程

Google Maps 教程

Gradle 教程

Java NIO 教程

JavaFX 教程

JavaMail 教程

JDBC 教程

jMeter 教程

JPA 教程

jsoup 教程

Junit 教程

KoaJS 教程

Kubernetes 教程

Log4j 教程

Logstash 教程

Lucene 教程

Makefile 教程

Maven 教程

RESTful 教程

Sed 教程

SEO 教程

Servlet 教程

SLF4J 教程

Socket.IO 教程

Struts 教程

SVN 教程

TestNG 教程

UML 教程

UNIX / LINUX 教程

WebSocket 教程

WPF 教程

xStream 教程

区块链 教程

数据处理

Flink 教程

Flume 教程

Hadoop 教程

Hbase 教程

Hive 教程

Kafka 教程

Kibana 教程

MapReduce 教程

MATLAB 教程

MyBatis 教程

Pig 教程

R语言 教程

Solr 教程

Spark 教程

Storm 教程

Zookeeper 教程

大数据分析 教程

数据仓库 教程

数据挖掘 教程

计算机基础

HTTP 教程

IPv4 教程

IPv6 教程

Ubantu 教程

WebServices 教程

嵌入式系统 教程

操作系统 教程

数据结构和算法 教程

汇编语言 教程

物联网 教程

电子电路基础 教程

编译器设计 教程

网站开发 教程

计算机 教程

计算机基础 教程

计算机网络 教程

设计模式 教程

AI

CNTK 教程

Keras 教程

PyTorch 教程

TensorFlow 教程

人工智能 教程

机器学习 教程

Python 技术

Django 教程

Flask 教程

NumPy 教程

Pandas 教程

Pillow 教程

PyGTK 教程

PyQt5 教程

PySpark 教程

pytest 教程

Python -数据科学 教程

Python MySQL 教程

Python 取证 教程

Python 数据结构 教程

Python 文本处理 教程

Python 网络编程 教程

Python 网页抓取 教程

Python 设计模式 教程

RxPY 教程

SciPy 教程

Seaborn 教程

SymPy 教程

wxPython 教程

框架

Laravel 教程

Web 图标Icon 教程

Web2py 教程

WebGL 教程

WebRTC 教程

WordPress 教程

Yii 教程

Zend Framework 教程

SAP

Crystal Reports 教程

🌸 相关推荐

超长续航千元机 华硕ZenFone飞马3全面评测
nowgoal365live score

超长续航千元机 华硕ZenFone飞马3全面评测

📅 07-12 👀 3496
时钟闹铃软件有哪些好用 十款常用时钟闹铃软件排行榜
识别人才方法有哪些?如何高效发现优秀人才
nowgoal365live score

识别人才方法有哪些?如何高效发现优秀人才

📅 08-07 👀 7002