① 我们先在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. java类为什么要实现Serializable接口

    什么是Serializable接口? 一个对象序列化的接口.一个类只有实现了Serializable接口,它的对象才能被序列化. 什么是序列化? 将对象的状态信息转换为可以存储或传输的形式的过程. 在 ...

  2. callable和runnable的区别

    Runnable接口源码 @FunctionalInterface public interface Runnable { /** * When an object implementing inte ...

  3. Flink(三) —— 运行架构

    Flink运行时组件 JobManager 作业管理器 TaskManager 任务管理器 ResourceManager 资源管理器 Dispatcher 分发器 任务提交流程 任务调度原理 Job ...

  4. Java/Oracle/mySQL 日期格式

    Java: yyyy-MM-dd HH:mm:ss.SSS Oracel: yyyy-MM-dd HH24:mi:ss select * from to_pub_report where report ...

  5. Git log 中文乱码

    以下三条命令搞定(系统是centos  7.4) git config --global i18n.commitencoding utf-8 git config --global i18n.logo ...

  6. $identify 的 “identify” 表示一个Perl标识符,即 identifier

    $identify 的 “identify” 表示一个Perl标识符,即 identifier

  7. 能够伪装为 win 10 的 kali 体验与中文设置

    前言 作为习惯性捣鼓各类操作系统,时长也会使用 Kali 系统,之前看到有新的版本发行 传闻这个版本和之前的版本在系统界面和壁纸上都做了更新,还能一键设置 win 10 的系统界面 对此决定下载体验一 ...

  8. Java分层架构的使用规则

    原文章引用地址:http://blog.csdn.net/ygzk123/article/details/7816511 三层结构的程序不是说把项目分成DAL, BLL, WebUI三个模块就叫三层了 ...

  9. arm汇编几个经典例题

    这几个例题来自我们的上机实验,通过这几个例题基本上能掌握arm汇编一些最基本的操作 arm汇编实现1-100的加法 12345678910111213 AREA Example1,CODE,READO ...

  10. [Algo] 281. Remove Spaces

    Given a string, remove all leading/trailing/duplicated empty spaces. Assumptions: The given string i ...