Android 常用代码---蓝牙

Android 蓝牙和BLE应用开发经验参考 本文是一篇关于Android蓝牙和BLE的开发技术经验总结分享,希望能对想从事Android蓝牙和BLE开发的同学有所帮助。由于本人能力有限,本文可能会有些内容写的不好的地方,还请大家在文章后面的讨论里面进行批评指正。 阅读详情
1验证是否支持蓝牙
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码// Trying to get the adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
      // Bluetooth is not supported, do something here to warn the user
      return;
}
2连接至设备
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码/*
 * This code is loose here but you will
 * likely use it inside a thread
 * 
 * Make sure you have the 'device' variable (BluetoothDevice)
 * at the point you insert this code
 */
// UUID for your application
UUID MY_UUID = UUID.fromString("yourdata"); 
// Get the adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
// The socket
BluetoothSocket socket = null;
try {
    // Your app UUID string (is also used by the server) 
    socket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
// For performance reasons
btAdapter.cancelDiscovery();
try {
    // Be aware that this is a blocking operation. You probably want to use this in a thread
    socket.connect();
} catch (IOException connectException) {
    // Unable to connect; close the socket and get out
    try {
     socket.close();
    } catch (IOException closeException) {
     // Deal with it
    }
    return;
}
// Now manage your connection (in a separate thread)
myConnectionManager(socket);
3启用蓝牙
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码/* This number is used to identify this request ("Enable Bluetooth") 
 * when the callback method onActivityResult() is called. Your
 * interaction with the Bluetooth stack will probably start there. 
 * 
 * You probably want to insert this as a global variable 
 */
int ENABLE_BLUETOOTH = 1;
// Get the adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
    return;
}
// If Bluetooth is not yet enabled, enable it
if (!btAdapter.isEnabled()) {
    Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBluetooth, ENABLE_BLUETOOTH);
    // Now implement the onActivityResult() and wait for it to be invoked with ENABLE_BLUETOOTH 
}
4获取成对设备
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码// Get the adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
// Get the paired devices
Set<BluetoothDevice> devices = btAdapter.getBondedDevices();
// If there are paired devices, do whatever you're supposed to do
if (devices.size() > 0) {
    for (BluetoothDevice pairedDevice : devices) {
        // do something useful with the device
    }
}
5确保可检测此设备
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码//Get the adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) { 
    return;
}
// If Bluetooth is not discoverable, make it discoverable
if (btAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
 Intent makeDiscoverable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    makeDiscoverable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);
 // In a real situation you would probably use startActivityForResult to get the user decision.
    startActivity(makeDiscoverable);
}
6等待传入连接
权限<uses-permission android:name="android.permission.BLUETOOTH"/>
代码// UUID for your application
UUID MY_UUID = UUID.fromString("yourdata"); 
// SDP record name used when creating the server socket
String NAME = "BluetoothExample";
// The server socket
BluetoothServerSocket btServerSocket =  null;
// The adapter
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
// The socket
BluetoothSocket socket = null;
try {
 btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
 // This operation is blocking: you will wait until it returns on an error occurs
    socket = btServerSocket.accept();
} catch (IOException e) { }
 // Deal with it
}
if (socket != null) {
 /* The connection was accepted. Do what you want to do.
 * For example, get the streams 
 */
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
    } catch (IOException e) {
        // Deal with it
    }  
}
7远程设备检测寄存器
权限 <uses-permission android:name="android.permission.BLUETOOTH"/>
代码// Register for notification upon discovery of a Bluetooth device
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(bluetoothReceiver, intentFilter);
// Register for notification upon completion of the device discovery process
intentFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(bluetoothReceiver, intentFilter);
// BroadcastReceiver that is notified as each device is found and when the discovery 
process completes 
// Should be an internal class or in a separate .java file
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // Device was discovered
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = 
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                // device is not already paired. Do something useful here.
            }
        // Discovery is finished
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
         // do something useful here    
        }
    }
};
安卓APP蓝牙上位机的编写一——APP结构 安卓APP蓝牙上位机开发的第一步 阅读详情

相关推荐

Android模拟蓝牙蓝牙键盘——适配Android和Windows

学校寒假有个程序设计比赛,我也一直想要去写一个安卓模拟的蓝牙键盘,这样无论到哪里,比如班班通和没有键盘的电脑设备,有手机就可以操作它,也比USB方便一些。忙活了一个寒假,也走了不少歪路,终于整成了,下面分享一些经验。优快云上大佬真的很多!

FengyunTHU的博客 6981

android -- 蓝牙 bluetooth (一) 入门

前段时间在 网上看了一些关于android蓝牙的文章,发现大部分是基于老版本(4.1以前含4.1)的源码,虽然无碍了解蓝牙的基本原理和工作流程,但对着4.2.2的代码看起来总是有些遗憾。所以针对4.2.2版本代码整理下相关知识,当然蓝牙工作的主干流程是没有变的,上电、加载驱动这些动作少不了的,只是这些功能的实现代码位置变了不少。希望本文可以让大家对android4.2的蓝牙部分代码有一个初步的了解

liwei405499的专栏 769

安卓蓝牙文件传输完整指南

安卓蓝牙文件传输是指安卓设备能够在两个蓝牙设备之间无线传输文件,如照片、视频、文档等。这种文件传输方法简化了通过蓝牙发送文件的过程,无需额外应用程序。它与许多设备兼容,但不支持iOS或ChromeOS。支持此功能的安卓设备有一个名为“蓝牙共享”的设置。Chrome OS 89为Chromebook和安卓设备引入了“附近共享”功能。此外,与USB数据线、OTG数据线、U盘、个人热点、媒体中心、WiFi Direct、云存储/电子邮件和文件传输应用程序相比,蓝牙具有优势。

Digitally的博客 3251

ZT android -- 蓝牙 bluetooth (一) 入门

android -- 蓝牙 bluetooth (一) 入门 分类: Android的原生应用分析 2013-05-19 21:44 4543人阅读 评论(37) 收藏 举报 bluetooth4.2.2源码android 目录(?)[-] 代码分布 整体结构 常用类和名词解释 后续分析 前段时间在 网上看了一些关于android蓝牙的文...

weixin_30825199的博客 239

安卓APP蓝牙上位机的编写二——搜索附近蓝牙设备

安卓APP蓝牙上位机的编写二——搜索附近蓝牙设备 安卓开发之搜索附近所有的蓝牙设备,源代码附送。

weixin_42119432的博客 3978

Android蓝牙遥控器(通过手机蓝牙与蓝牙模块通信)

关于Android的蓝牙编程,与下位机通信demo ,还有详细注释。若真需要源代码工程文件,请在评论处留言。

Android 小生的博客 2万+

Android Bluetooth(一) — 蓝牙模块代码整体架构

Android Bluetooth(一) — 蓝牙模块代码整体架构0. 前言1. 代码分布2. 整体结构3. 常用类和名词解释4. 以蓝牙 打开 enable 为例简单说明下蓝牙工作的整个过程:5. 后续分析 0. 前言   我之前提过,我目前接触的最多的Android 版本是 Android kk(4.4)与Android P(9.0),因为最近在Android kk上做了一个wifi模组的移植适配工作,因此对WiFi&BT模块有了些了解,本系列主要基于Android kk进行了解分析,也许其他版

一只立志于养老婆的程序猿 3950

安卓蓝牙开发总结(一)—蓝牙开启与关闭

蓝牙开发总结1、引言2、布局文件3、蓝牙打开与关闭  3.1 通过对按钮监听方法  3.2 通过设置点击事件4、结果展示及总结5、参考链接 1、引言   最近在学习如何在安卓手机上对蓝牙进行操控,作为初学者发现大多数博客对安卓开发的初学者极为不友好,特别是安卓和java都不熟悉的童鞋,因此现对我近期所学习内容做一个总结,希望对大家有帮助。 2、布局文件   控件的种类,数量及用途如下表所示,AS中控件的使用方法可参考B站up主(longway777)讲得很到位。 控件种类 控件数量 作用

ProgrammersFighting的博客 4366

兔子-蓝牙 bluetooth (一) 入门

前段时间在 网上看了一些关于android蓝牙的文章,发现大部分是基于老版本(4.1以前含4.1)的源码,虽然无碍了解蓝牙的基本原理和工作流程,但对着4.2.2的代码看起来总是有些遗憾。所以针对4.2.2版本代码整理下相关知识,当然蓝牙工作的主干流程是没有变的,上电、加载驱动这些动作少不了的,只是这些功能的实现代码位置变了不少。希望本文可以让大家对android4.2的蓝牙部分代码有一个初步的了解

IT技术专栏 828

安卓经典蓝牙开发,包括自动连接开发板蓝牙并通信

安卓经典蓝牙开发,包括自动连接开发板蓝牙并通信蓝牙开发首先弄清楚两点:手机蓝牙连接手机蓝牙与手机蓝牙连接外设蓝牙(比如开发板上的蓝牙),这两者连接方式要区分开来,前者需要两个手机同时安装开发的APP,APP里面的代码指定了唯一的UUID用于通信,而后者只能在手机APP里面通过反射的方式获取外设蓝牙的UUID,然后连接通信。本文是使用后者。第二是要知道经典蓝牙开发与低功耗蓝牙开发(即Ble)二者是不...

Crook的专栏 4428

安卓基于BLE的蓝牙开发入门

BLE蓝牙开发简单入门BLE背景介绍引言BLE简介Gatt协议以及必备知识蓝牙开发涉及的API介绍BLE蓝牙开发准备 BLE背景介绍 引言 如今,蓝牙的使用在生活中是越来越常见了,各色各样的运动手环、体脂称以及无线耳机都用到了蓝牙功能,稍稍有些好奇如今火热的keep、小米运动等app是如何通过手环简单的蓝牙连接进行交互的,通过本博客对安卓的蓝牙开发部分做一个简单的入门 BLE简介 蓝牙低能耗(Bluetooth Low Energy,或称Bluetooth LE、BLE,旧商标Bluetooth Smart

Takitohru的博客 1510

Bluetooth--- android -- 蓝牙 bluetooth

android -- 蓝牙 bluetooth (一) 入门 分类: Android的原生应用分析2013-05-19 21:44 6229人阅读 评论(37) 收藏 举报 bluetooth4.2.2源码android 目录(?)[+]         前段时间在 网上看了一些关于android蓝牙的文章,发现大部分是基于老版本(4.1以前含4.1)的源码,虽

yyb.ye专栏 3965

Android蓝牙常用ADB调试命令汇总

Android蓝牙ADB调试命令主要用于通过ADB(Android Debug Bridge)工具来控制和调试Android设备的蓝牙功能。本文整理一些常用的Android蓝牙ADB调试命令。

byte轻骑兵的技术小窝 13万+

android -- 蓝牙 bluetooth (一) 入门 .

http://blog.youkuaiyun.com/baimy1985/article/details/8892410   android -- 蓝牙 bluetooth (一) 入门 分类: Android的原生应用分析 2013-05-19 21:44 4849人阅读 评论(37) 收藏 举报 bluetooth4.2.2源码android 目录(?)[+]

zgyrelax的专栏 1059

安卓手机与蓝牙模块联合调试(五)-- 编写自己的蓝牙控制界面控制单片机(上篇,Android 代码实现)

(1)安卓手机与蓝牙模块联合调试(一)—— 蓝牙模块的串口通讯 (2)安卓手机与蓝牙模块联合调试(二)—— 单片机蓝牙控制LED灯亮灭(上) (3)安卓手机与蓝牙模块联合调试(三)—— 单片机蓝牙控制LED灯亮灭(下) (4)安卓手机与蓝牙模块联合调试(四)—— 单片机数据上传至蓝牙(STC89C52 + DS18b20) 本教程的项目地址:1989Jiangtao/BluetoothS.........

菜鸟江涛的博客 1万+

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1606

QT蓝牙开发源代码安卓:开启安卓蓝牙通信新篇章

QT蓝牙开发源代码安卓:开启安卓蓝牙通信新篇章 去发现同类优质开源项目:https://gitcode.com/ 项目介绍 在现代智能设备的开发中,蓝牙通信技术因其便捷性和广泛应用而备受青睐。今天,我们将为您推荐一个开源项目——QT蓝牙开发源代码安卓。此项目专为安卓平台打造,提供了使用QT框架进行蓝牙通信的完整源代码。通过该代码,开发者可以轻松实现安卓设备之间的蓝牙串口通信,为各类智能设备提供高效...

gitblog_06706的博客 952

iSLIP算法实战:如何用Python模拟交换机优先级匹配(附完整代码)

本文详细介绍了如何使用Python实现iSLIP算法,模拟交换机优先级匹配过程。通过核心的指针滑动逻辑和迭代循环策略,构建了一个完整的iSLIP算法模拟器,并加入可视化模块以直观展示匹配过程。文章还提供了完整的代码框架和性能优化建议,适合网络工程师和Python开发者直接集成到测试环境中。

weixin_29313239的博客 249
上一篇: Android 常用代码---资源文件
下一篇: Android 常用代码---日志
wd_cloud
博客等级 码龄19年 733粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值