Android蓝牙----打开,关闭操作
① 我们先在AndroidManifest里面增加我们的Bluetooth权限
<uses-permission android:name="android.permission.BLUETOOTH"/> //使用蓝牙所需要的权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> //使用扫描和设置蓝牙的权限(申明这一个权限必须申明上面一个权限)
② 写个简单的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="com.example.administrator.bluetoothdemo.MainActivity"> <Button
android:id="@+id/Btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:text="open Bluetooth"
android:textAllCaps="false"
/>
<Button
android:id="@+id/Btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="clase Bluetooth"
android:textAllCaps="false"
/>
</RelativeLayout>
③ 接下来写MainActivity类的代码
package com.example.administrator.bluetoothdemo; import android.bluetooth.BluetoothAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mBtn,mBtn1;
private BluetoothAdapter mBluetooth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView(); //查找控件
mBtn.setOnClickListener(this); //创建点击事件
mBtn1.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.Btn1:
{
if(!mBluetooth.isEnabled())
{
mBluetooth.enable();
}
Toast.makeText(MainActivity.this,"打开成功", Toast.LENGTH_SHORT).show();
}break; case R.id.Btn2:
{
if(mBluetooth.isEnabled())
{
mBluetooth.disable();
}
Toast.makeText(MainActivity.this,"关闭成功", Toast.LENGTH_SHORT).show();
}break;
}
}
public void findView()
{
mBtn = (Button)findViewById(R.id.Btn1);
mBtn1 = (Button) findViewById(R.id.Btn2);
mBluetooth = BluetoothAdapter.getDefaultAdapter(); //获取Bluetooth适配器
}
}
Android蓝牙----打开,关闭操作的更多相关文章
- 代码验证C#执行”文件打开关闭操作“耗时
2017-04-19 部门经理习惯用C#做数据清洗,遇到个需要验证的问题,在一个万次左右循环内对文件执行打开关闭操作,比在循环前打开文件.循环后关闭文件耗时多多少. using System; usi ...
- Android 蓝牙模块基础操作
之前没怎么接触过蓝牙模块,在学习的过程中借鉴了很多前辈的经验.本次主要包含以下功能: 1.检测是否存在蓝牙模块 2.蓝牙的开启与关闭 3.与本机已配对的蓝牙设备 4.本机蓝牙可见性设置 5.扫描周围蓝 ...
- Android 8 蓝牙打开过程
packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothEnabler.java @Override public boo ...
- android -- 蓝牙 bluetooth (二) 打开蓝牙
4.2的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点不同而已,后续的流程是一样的.先来看systemServe ...
- ZT android -- 蓝牙 bluetooth (二) 打开蓝牙
android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...
- Python文件操作:文件的打开关闭读取写入
Python文件操作:文件的打开关闭读取写入 一.文件的打开关闭 Python能以文本和二进制两种方式处理文件,本文主要讨论在Python3中文本文件的操作. 文件操作都分为以下几个步骤: 1.打开文 ...
- Android 蓝牙API详解
随着近两年可穿戴式产品逐渐进入人们的生活,蓝牙开发也成为了Android开发的一个重要模块,下面我们就来说一说蓝牙的这些API. 1.蓝牙开发有两个主要的API: BuletoothAdapter:本 ...
- Qt on Android 蓝牙开发
版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...
- android蓝牙打印机
您还未登录!|登录|注册|帮助 首页 业界 移动 云计算 研发 论坛 博客 下载 更多 reality_jie的专栏 编程的过程是一种微妙的享受 目录视图 摘要视图 订阅 CSDN2013 ...
随机推荐
- Java基础二(2020.1.14)
学习内容: 1.Java运算符:赋值运算符,算术运算符,关系运算符,逻辑运算符,条件运算符 2.java流程控制:顺序,选择 1.java输入 Scanner s = new Scanner(Syst ...
- ORB-SLAM2的编译运行以及TUM数据集测试
ORB-SLAM2的编译运行以及TUM数据集测试 徐大徐 2018.02.06 17:04 字数 1838 阅读 2167评论 0喜欢 2 近段时间一直在学习高翔博士的<视觉SLAM十四讲> ...
- swoole使用内存
//swoole直接操作系统的内存 单线程每秒可执行三百万次 主要用于进程间的数据通信 $swoole_table = new swoole_table(1024);//1024为内创建内存对象所能存 ...
- 使用iTextSharp來合併PDF檔
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Python语言学习前提:循环语句
一.循环语句 1.循环语句:允许执行下一个语句或语句组多次 2. 循环类型 3. 循环控制语句 4. while 循环语句 a.while循环语句:在某个条件下,循环执行某段程序,以处理需要重复处理的 ...
- Office 365 共享邮箱/日历
一.共享邮箱 Office 365共享邮箱对于客户通过电子邮箱提出的问题,共享邮箱是一个很好的处理方式,组织中的多人可以分担监控邮箱和回复的责任,使得客户的问题更快地得到答复,而相关电子邮件都存储在一 ...
- ubuntu 更新源 或者 apt-get install 出错404 not found ,Failed to fetch
1.考虑是不是能上网 2.用apt-get update ,然后再试试apt-get install 如果apt-get update 也出现很多 404 not found 或者 failed to ...
- 七、linux-mysql下mysql增量备份与恢复
1.备份的意义 运维工作:保护公司的数据 . 网站7*24小时服务 但相当来说,数据更加重要,而数据最核心的就是数据库数据,所以数据库的备份和恢复就显得十分重要. 2.备份的几个参数 mys ...
- springboot 整合thymeleaf 书笔记
pom.xml依赖添加 <!--引入thymeleaf--> <dependency> <groupId>org.springframework.boot</ ...
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...