① 我们先在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. MyBatis从入门到精通(第5章):MyBatis代码生成器

    jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.Eclipse Version: 2019-12 M2 (4.14.0) MyBatis从入门到精通(第5章):MyBatis代码 ...

  2. Go-简介-发展

    01-Go语言介绍 目录 Go语言介绍 Go语言特性 Go语言发展(版本/特性) Go语言应用 谁在用 应用领域 Go语言项目 Go语架构 Go语言发展前景 Go语言介绍 Go 即Golang,是Go ...

  3. SVN的import和export的使用

    import 上传到服务器 export 下载到本地 import这个命令在使用的时候,都要多加一级目录 比如 目录结构是 E:\\1\\2  在1目录下执行import  只会把2目录提交上去,1的 ...

  4. C/S 和 B/S架构

    C/S 和 B/S架构 一.单机架构 应用领域: 植物大战僵尸 office 二.C/S架构 [ 应用领域: QQ 大型网络游戏 计算机发展初期用户去取数据,直接就去主机拿,从这里开始就分出了客户端和 ...

  5. 剑指offer【13】- 链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第k个结点. /* public class ListNode { int val; ListNode next = null; ListNode(int val) { ...

  6. vue-resource HTTP API基础

    vue-resource特点 vue-resource插件具有以下特点: 1. 体积小 vue-resource非常小巧,在压缩以后只有大约12KB,服务端启用gzip压缩后只有4.5KB大小,这远比 ...

  7. 一种循环buffer结构

    最新数据循环在buffer[H] -> buffer[L] 放置,记录最新放置Index,对外接口获取数据时,进行两次数据拷贝,Index-H ,index-L 拷贝到数组里

  8. [Algo] 253. Longest Substring Without Repeating Characters

    Given a string, find the longest substring without any repeating characters and return the length of ...

  9. 吴裕雄--天生自然 JAVA开发学习:继承

    class 父类 { } class 子类 extends 父类 { } public class Penguin { private String name; private int id; pub ...

  10. gitlab安装教程

    gitlab安装教程     安装教程 官网安装方法 https://about.gitlab.com/downloads/#centos7 1.准备 sudo yum install curl po ...