蓝牙基本操作

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;import java.util.Set;public class BluetoothActivity extends Activity {private static final int REQUEST_ENABLE_BT = 0x11;private static final int REQUEST_ENABLE_DISCOVERY = 0x12;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bluetooth);// 得到蓝牙adapterBluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter == null) {// 设备不支持蓝牙}// 打开蓝牙 - 用户是否打开蓝牙,可在onActivityResult()中判断if (!bluetoothAdapter.isEnabled()) {Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);}// 查找已配对过的设备Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();if (pairedDevices.size() > 0) {for (BluetoothDevice device : pairedDevices) {// 得到设备对象device}}// 发现周围的蓝牙设备 - 异步扫描周围的蓝牙设备,使用监听广播的方式,得到扫描到的设备// 请见对象deviceReceiverbluetoothAdapter.startDiscovery();// 注册广播IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(deviceReceiver, filter);// 关闭发现周围的蓝牙设备bluetoothAdapter.cancelDiscovery();// 当前蓝牙设备能被发现,在300秒内,可在onActivityResult()中判断用户是否同意,该方法会自动打开蓝牙Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);startActivityForResult(discoverableIntent, REQUEST_ENABLE_DISCOVERY);// 得到蓝牙mac地址bluetoothAdapter.getAddress();// 通过蓝牙mac地址,得到蓝牙设备BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);}private final BroadcastReceiver deviceReceiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {String action = intent.getAction();if (BluetoothDevice.ACTION_FOUND.equals(action)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// 拿到发现得蓝牙设备对象device,注意同一台设备可能会有多次返回}}};@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == RESULT_CANCELED) {if (requestCode == REQUEST_ENABLE_BT) {// 用户没有打开蓝牙} else if (requestCode == REQUEST_ENABLE_DISCOVERY) {// 用户不想让设备被扫描到}} else {if ((requestCode == REQUEST_ENABLE_BT)) {// 用户打开了蓝牙} else if (requestCode == REQUEST_ENABLE_DISCOVERY) {// 用户同意设备被扫描到}}}@Overrideprotected void onDestroy() {// 注销广播unregisterReceiver(deviceReceiver);super.onDestroy();}
}

蓝牙传输文件

使用默认的系统蓝牙应用,发送文件给另一台支持蓝牙设备

在Android系统中,使用蓝牙可以分享文件给另一台蓝牙设备,这样一种传输方式,使用的就是OBEX协议。按我的理解,所有蓝牙设备都支持OBEX协议。
在文件管理器中,可以分享文件,但是会弹出很多的分享目标:QQ,微信,蓝牙等…,若想实现这个功能,就需要直接调用蓝牙分享,下面代码使用的就是这个思路,摘录自:http://www.2cto.com/kf/201401/270305.html;

    private void sendFile(Activity activity, File file) {PackageManager localPackageManager = activity.getPackageManager();Intent localIntent = null;HashMap<String, ActivityInfo> localHashMap = null;try {localIntent = new Intent();localIntent.setAction(Intent.ACTION_SEND); // 设置为分享ACTIONlocalIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); // 添加传输文件// localIntent.putExtra(Intent.EXTRA_STREAM,// Uri.fromFile(new File(localApplicationInfo.sourceDir)));localIntent.setType("*/*"); // 设置应用的打开方式List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0); // 查找所有可以处理该intent的localHashMap = new HashMap<String, ActivityInfo>();Iterator<ResolveInfo> localIterator1 = localList.iterator();while (localIterator1.hasNext()) {ResolveInfo resolveInfo = (ResolveInfo) localIterator1.next();ActivityInfo localActivityInfo2 = resolveInfo.activityInfo;String str = localActivityInfo2.applicationInfo.processName;if (str.contains("bluetooth"))localHashMap.put(str, localActivityInfo2); // 添加所有包含bluetooth关键字的程序}} catch (Exception e) {Toast.makeText(BluetoothShareActivity.this, "蓝牙分享异常:" + e.getMessage(), Toast.LENGTH_SHORT).show();}if (localHashMap.size() == 0)Toast.makeText(BluetoothShareActivity.this, "无蓝牙设备分享", Toast.LENGTH_SHORT).show();ActivityInfo localActivityInfo1 = (ActivityInfo) localHashMap.get("com.android.bluetooth");if (localActivityInfo1 == null) {localActivityInfo1 = (ActivityInfo) localHashMap.get("com.mediatek.bluetooth");}if (localActivityInfo1 == null) {Iterator<ActivityInfo> localIterator2 = localHashMap.values().iterator();if (localIterator2.hasNext())localActivityInfo1 = (ActivityInfo) localIterator2.next();}if (localActivityInfo1 != null) {localIntent.setComponent(new ComponentName(localActivityInfo1.packageName, localActivityInfo1.name));activity.startActivityForResult(localIntent, 4098); // 请求处理intentreturn;}}

android蓝牙操作相关推荐

  1. Android 蓝牙操作--读取远程已配对的蓝牙设备

    1>在AndroidManifest.xml文件中添加相关权限 <?xml version="1.0" encoding="utf-8"?> ...

  2. Android蓝牙操作笔记

    蓝牙是一种支持设备短距离传输数据的无线技术.android在2.0以后提供了这方面的支持.  从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器):  1.设置权限  在manifest中 ...

  3. 基于html5+的nativejs实现android蓝牙串口通讯

    ##开发工具 基于hbuilder打包的webapp. ##所需知识 了解基本的html,css,js,vue.js 了解原生android的开发 了解android蓝牙的开发 了解hbuilder的 ...

  4. Android开发--蓝牙操作

    首先,由于模拟器上没有蓝牙装置,所以我们需要一个含有蓝牙装置的Android系统 其次,要操作设备上的蓝牙装置,需要在AndroidManifest中声明两个权限: <uses-permissi ...

  5. Android开发蓝牙操作

    一.打开蓝牙权限 操作蓝牙之前必须先要注册蓝牙权限.在AndroidManifest.xml文件中注册权限: <uses-permission android:name="androi ...

  6. [转]Android蓝牙开发浅谈

    转自:http://www.eoeandroid.com/thread-18993-7-1.html 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少    ...

  7. Android 蓝牙启动流程(以及设置蓝牙为作为sink模式 接收端模式)

    本文以Android 7.1为基础 最近在做蓝牙开发,研究了一下蓝牙的启动流程,总结一下 Google在Android源码中推出了它和博通公司一起开发的BlueDroid以替代BlueZ.BlueZ的 ...

  8. 【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359 参考 :  -- 官方文档 : https://develope ...

  9. Android蓝牙串口程序开发

    本文主要介绍了针对android的蓝牙串口上位机开发. 程序下载地址:点击打开链接 一.帧定义 androidclient依照一定的数据帧格式通过蓝牙串口发送数据到连接到MCU的蓝牙从机.MCU接收到 ...

最新文章

  1. Laya 位图字体制作(失败...)
  2. python如何安装pip3_如何在安装pip3以及第三方python库
  3. IntelliJ IDEA内存优化最佳实践(转)
  4. 《黑天鹅》读书笔记(part2)--我们从重复中学习,但忽略了从未发生过的事件
  5. [note]抽象类和接口的相同点和不同点
  6. 改善Python程序的91个建议(二)
  7. 【字符串】面试题之奇偶字符串分离
  8. WIN32汇编 状态栏的使用
  9. C语言程序设计基础学习笔记简介
  10. python进制转换问题“输入十进制整数和欲转换的进制数r,将十进制转换为r进制数(r>=2)”
  11. RecyclerView的 overScrollMode,scrollbars属性含义
  12. 计算机重新启动后打印机脱机,重启电脑后打印机脱机怎么办
  13. 如何正确认识和提升自己的格局
  14. 银联基于OpenStack 的“五高”生产金融云技术白皮书
  15. 分布式卷积神经网络计算平台(通用神经网络数据处理卡 Kintex Ultra Scale 系列 KU115)
  16. 个人网站建设(装逼)指南
  17. Android简易计分器
  18. 《黑镜》(Black Mirror)
  19. Mysql实现数据脱敏
  20. unity3d实现场景右下角人物小地图(可显示出地图上人物的位置以及boss或者其他重要坐标的小图标)

热门文章

  1. 符号测执行软件测试,基于符号执行与模糊测试的混合测试方法
  2. sql语句创建表时的注意事项
  3. shell 列表操作
  4. cvcvtcolor,opencv函数cvcvtcolor和cvtcolor之间的区别是什么
  5. Warning: Packets out of order. Expected 8 received 0
  6. 自学还是辞职大数据_说到数据库,为什么“宝贝,我无法辞职”
  7. m1芯片 mysql_mac m1芯片搭建php开发环境 mnmp开发环境
  8. AD16 PIN类型错误
  9. Vue中遍历不规律的json对象(递归)
  10. 再度学习大数据技术与应用(厦门大学林子雨)