android 蓝牙4.0广播功能应用
一、添加蓝牙权限
在AndroidManifest.xml文件中添加如下红色部分代码,添加蓝牙打开权限,以及LE接口调用权限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.administrator.myapplication"><uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"
二、添加对应的包头以及调用接口,并添加button clock事件开启关闭广播功能。
package com.example.administrator.myapplication;import android.bluetooth.BluetoothGatt; import android.support.v7.app.AppCompatActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothManager; import android.bluetooth.le.BluetoothLeAdvertiser; import android.bluetooth.le.AdvertiseSettings; import android.bluetooth.le.AdvertiseData; import android.bluetooth.le.AdvertiseCallback; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button enableBt = (Button)findViewById(R.id.button2);Button openAdv = (Button)findViewById(R.id.button);
//获取蓝牙适配器对象
final BluetoothAdapter BtAdapter = BluetoothAdapter.getDefaultAdapter();
//获取蓝牙LE广播对象
final BluetoothLeAdvertiser BtAdv = BtAdapter.getBluetoothLeAdvertiser();
//广播参数设置,此处设置为空
final AdvertiseSettings AdvSetting = new AdvertiseSettings.Builder().build();
//广播数据设置为空
final AdvertiseData AdvData = new AdvertiseData.Builder().build();
//设置广播回调函数
final AdvertiseCallback AdvCBack = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); Toast.makeText(getApplicationContext(),"start Advertise ok",Toast.LENGTH_SHORT).show(); } }; enableBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(BtAdapter.isEnabled() == false){
//启动蓝牙功能
BtAdapter.enable(); Toast.makeText(getApplicationContext(),"enable bluetooth",Toast.LENGTH_SHORT).show(); }else{
//关闭蓝牙功能
BtAdapter.disable(); Toast.makeText(getApplicationContext(),"disable bluetooth",Toast.LENGTH_SHORT).show(); } } }); openAdv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(BtAdapter.isEnabled()) {
//启动蓝牙LE广播功能
BtAdv.startAdvertising(AdvSetting, AdvData, AdvCBack); Toast.makeText(getApplicationContext(),"start Advertise",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(),"Bluetooth is not enable!",Toast.LENGTH_SHORT).show(); } } }); }}
android 蓝牙4.0广播功能应用相关推荐
- ym——物联网入口之中的一个Android蓝牙4.0
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...
- ym——物联网入口之一Android蓝牙4.0
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 如果还有同学不知道蓝牙4.0可以做什么请查看Android+蓝牙 4.0 将带来什么? ...
- Android 蓝牙相关的广播
2019独角兽企业重金招聘Python工程师标准>>> Android 蓝牙相关的广播 监听蓝牙相关的广播并获得相关的信息,蓝牙相关的广播主要集中在BluetoothAdapter和 ...
- android蓝牙广播自定义,Android 蓝牙相关的广播
Android 蓝牙相关的广播 监听蓝牙相关的广播并获得相关的信息,蓝牙相关的广播主要集中在BluetoothAdapter和BluetoothDevice类中, 可以通过在AndroidManife ...
- android蓝牙4.0 BLE低功耗应用
转自 http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html 谈谈几个月以来开发android蓝 ...
- 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受
谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受,注明下时间:2012-10-17写的博客,后期更新的也注明了时间 ...
- 开发android蓝牙4.0 BLE低功耗应用的感受
文章转自: http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html 谈谈几个月以来开发android蓝 ...
- android 蓝牙相关广播,Android 蓝牙相关的广播
Android 蓝牙相关的广播 监听蓝牙相关的广播并获得相关的信息,蓝牙相关的广播主要集中在BluetoothAdapter和BluetoothDevice类中, 可以通过在AndroidManife ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
最新文章
- 使用Feign时如何设置Feign的Header信息
- HDU 5119 Happy Matt Friends(递推)
- 用python结束exe进程
- Custom PuTTY Color Themes
- 如何使用COMPUTER VISION将LEPRECHAUN-HATS放入您的网站
- Visual Leak Detector 2 2 3 Visual C++内存检测工具
- 【java】java基础之SPI框架实现-整体设计
- PDF 与 Word互转工具。 在线的 和安装软件
- 上传图片至fastdfs显示连接超时
- iMazing v2021绿色便携版iOS设备数据管理工具
- 手机html在哪个文件里,手机相册在哪个文件夹,教您手机图片存放在哪里
- 【Python】基础语法之一:变量、字符串、数、注释
- 错过了淘宝、拼多多,今年的电商风口抖音小店无货源你能抓住吗?
- 面试时如何更好的介绍自己的项目
- 店宝宝:巨头PK精品电商
- 曲面着色器初试--地面轨迹模拟(部分细节不完善)
- Windows 解决teamview远程必须mstsc连接
- veket linux能运行qq么,【veket系统】Veket Linux系统下载 v8.07 官方正式版-开心电玩...
- python爬取身份证信息、爬取ip代理池
- x3100服务器设置linux启动,IBM服务器X3100-M5系统安装Windows2012教程
热门文章
- php报错Notice: A session had already been started - ignoring session_start() 解决方案
- Vector space
- C#交错数组和参数数组
- 烤仔的朋友们丨详解数字美元白皮书:双层架构、通证化、汇款通道
- es ik 多字段查询_SpringBoot使用注解的方式构建Elasticsearch查询语句,实现多条件的复杂查询...
- 让文化与大数据 离婚吧
- 自适应流媒体传输(四)——深入理解MPD
- web项目中上传的图片项目重新部署之后被清空如何解决?
- 微信开发实战(2)—微信公众平台接口调试工具
- vscode如何自动格式化代码