① 我们先在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蓝牙----打开,关闭操作的更多相关文章

  1. 代码验证C#执行”文件打开关闭操作“耗时

    2017-04-19 部门经理习惯用C#做数据清洗,遇到个需要验证的问题,在一个万次左右循环内对文件执行打开关闭操作,比在循环前打开文件.循环后关闭文件耗时多多少. using System; usi ...

  2. Android 蓝牙模块基础操作

    之前没怎么接触过蓝牙模块,在学习的过程中借鉴了很多前辈的经验.本次主要包含以下功能: 1.检测是否存在蓝牙模块 2.蓝牙的开启与关闭 3.与本机已配对的蓝牙设备 4.本机蓝牙可见性设置 5.扫描周围蓝 ...

  3. Android 8 蓝牙打开过程

    packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothEnabler.java @Override public boo ...

  4. android -- 蓝牙 bluetooth (二) 打开蓝牙

    4.2的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点不同而已,后续的流程是一样的.先来看systemServe ...

  5. ZT android -- 蓝牙 bluetooth (二) 打开蓝牙

    android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...

  6. Python文件操作:文件的打开关闭读取写入

    Python文件操作:文件的打开关闭读取写入 一.文件的打开关闭 Python能以文本和二进制两种方式处理文件,本文主要讨论在Python3中文本文件的操作. 文件操作都分为以下几个步骤: 1.打开文 ...

  7. Android 蓝牙API详解

    随着近两年可穿戴式产品逐渐进入人们的生活,蓝牙开发也成为了Android开发的一个重要模块,下面我们就来说一说蓝牙的这些API. 1.蓝牙开发有两个主要的API: BuletoothAdapter:本 ...

  8. Qt on Android 蓝牙开发

    版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...

  9. android蓝牙打印机

    您还未登录!|登录|注册|帮助 首页 业界 移动 云计算 研发 论坛 博客 下载 更多 reality_jie的专栏 编程的过程是一种微妙的享受       目录视图 摘要视图 订阅 CSDN2013 ...

随机推荐

  1. mysql 不停机 短时间锁表 备份 主备同步 新增备份机器

    刷新数据   [root@localhost ~]# mysql -e 'flush tables with read lock;' 锁表刷新表数据   [root@localhost ~]# mys ...

  2. Lua与C++交互初探之C++调用Lua

    Lua与C++交互初探之C++调用Lua 自从学习了lua这个脚本语言之后,无时不想着将他与c/c++联系起来,看看他真正的威力.奈何水平有限,网上找的代码无论怎样都无法运行成功.我知道是我少了某一步 ...

  3. 二、NOSQL之Memcached缓存服务实战精讲第一部

    1.Memcached是一套数据缓存系统或软件. 用于在动态应用系统中缓存数据库的数据,减少数据库的访问压力,达到提升网站系统性能的目的:Memcached在企业应用场景中一般是用来作为数据库的cac ...

  4. 图遍历算法的应用(包括输出长度为l的路径最短最长路径)

    判断从顶点u到v是否有路径 void ExistPath(AdjGraph* G, int u, int v, bool& has) { int w; ArcNode* p; visit[u] ...

  5. Opencv笔记(十三)——图像的梯度

    目标 认识图像梯度.边界 学习函数cv2.Sobel(),cv2.Schar(),cv2.Laplacian() 原理 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个二维离散函数的求导.Op ...

  6. 爬虫笔记(十)——学会使用Fiddler

    Fiddler是一个常见的抓包分析软件,同时我们可以利用它详细地对HTTP请求进行分析,并模拟对应的HTTP请求. 为什么使用Fiddler软件? 网络爬虫是自动爬取网页的程序,在爬取的过程中必然涉及 ...

  7. Win10卸载python总是提示error2503失败各种解决办法

    最近win10的电脑装了python的3.4,然后想卸载,就总是提示error 2053,类似于这种: 下面是我的坎坷解决之路: 1.网上说,任务管理器 --> 详细信息 --> expl ...

  8. NSIS增加打包文件夹

    File /r "F:\rainwayDemo\exploer\RailwayLinesExploer\bin64\log" File  /r "models" ...

  9. Flink(六) —— 配置文件详解

    基础配置 #============================================================================== # Common #===== ...

  10. iOS头条新闻App、自动布局、省市区联动、登录按钮动画、Alert弹框效果等源码

    iOS精选源码 LEEAlert -- 优雅的Alert ActionSheet 登录按钮 省市区三级联动 JHViewCorner - 一行代码搞定圆角 JHFrameLayout - 一行代码实现 ...